Collect the patches from a package.

@param \Composer\Package\PackageInterface $package the package to be inspected

@return \ComposerPatcher\PatchCollection
421940
protected function collectPatchesFromPackage(PackageInterface $package)
    {
        $collectedPatches = new PatchCollection();
        $extra = $package->getExtra();
        if (isset($extra['patches'])) {
            $this->io->write('<info>Gathering patches from '.$package->getName().' (extra.patches).</info>');
            $collectedPatches->merge($this->resolveExplicitList($package, $extra['patches']));
        }
        if (isset($extra['patches-file'])) {
            $this->io->write('<info>Gathering patches from '.$package->getName().' (extra.patches-file).</info>');
            $collectedPatches->merge($this->resolveJsonFile($package, $extra['patches-file']));
        }

        return $collectedPatches;
    }
1125393
public function getInstallPath(PackageInterface $package): string
    {
        $this->vendorPath = parent::getInstallPath($package);
        return $this->vendorPath;
    }
1120953
protected function buildPackage(array $package) {
    $info = [
      'download' => [
        'type' => 'git',
        'url' => str_replace('git@github.com:', 'https://github.com/', $package['source']['url']),
        'branch' => $package['version'],
        'revision' => $package['source']['reference'],
      ],
    ];

    if (isset($package['extra']['patches_applied'])) {
      $info['patch'] = array_values($package['extra']['patches_applied']);
    }
    return $info;
  }
493956
public function isSharedPackage(PackageInterface $package)
    {
        $prettyName = $package->getPrettyName();

        // Avoid putting this package into dependencies folder, because on the first installation the package won't be
        // installed in dependencies folder but in the vendor folder.
        // So I prefer keeping this behavior for further installs.
        if (SharedPackageInstaller::PACKAGE_PRETTY_NAME === $prettyName) {
            return false;
        }

        if ($this->areAllShared || SharedPackageInstaller::PACKAGE_TYPE === $package->getType()) {
            return true;
        }

        foreach ($this->packageCallbacks as $equalityCallback) {
            if ($equalityCallback($prettyName)) {
                return true;
            }
        }

        return false;
    }
90106
public function findPackageConfiguration($package)
    {
        $paths = [
            $this->paths->getBasePath().'vendor/%s/src/config',
            $this->paths->getBasePath().'vendor/%s/config',
            $this->paths->getRocketeerPath().'/vendor/%s/src/config',
            $this->paths->getRocketeerPath().'/vendor/%s/config',
            $this->paths->getUserHomeFolder().'/.composer/vendor/%s/src/config',
            $this->paths->getUserHomeFolder().'/.composer/vendor/%s/config',
        ];

        // Check for the first configuration path that exists
        $paths = array_map(function ($path) use ($package) {
            return sprintf($path, $package);
        }, $paths);

        return $paths;
    }
32865
protected function mustUninstallPackage(PackageInterface $package)
    {
        if ($package instanceof AliasPackage) {
            return false;
        }
        $packageExtra = $package->getExtra();
        $packageHasAppliedPatches = !empty($packageExtra['patches_applied']);
        $packagePatches = $this->getPatchCollection()->getPatchesFor($package);
        $packageHasPatches = !$packagePatches->isEmpty();
        if ($packageHasAppliedPatches === false && $packageHasPatches === false) {
            return false;
        }
        if ($packageHasAppliedPatches === true && $packageHasPatches === true) {
            $installedHash = empty($packageExtra['patches_applied']['hash']) ? '' : $packageExtra['patches_applied']['hash'];
            if ($installedHash === '') {
                return true;
            }
            $patchesHash = $packagePatches->getHash();
            if ($installedHash !== $patchesHash) {
                return true;
            }
        }

        return false;
    }
1063844
protected function extractPatchData(PackageInterface $package, $patchData)
    {
        $levels = null;
        if (is_string($patchData)) {
            $path = $patchData;
        } else {
            if (!is_array($patchData) || !isset($patchData['path']) || is_string($patchData['path'])) {
                throw new Exception\InvalidPackageConfigurationValue($package, 'extra.patches.[...]', $package, "The value of a patch must be a string or an array with a 'path' node value.");
            }
            $path = $patchData['path'];
            if (isset($patchData['levels'])) {
                $levels = $patchData['levels'];
                if (!is_array($levels) || empty($levels)) {
                    throw new Exception\InvalidPackageConfigurationValue($package, 'extra.patches.[...].levels', $package, 'The patch levels must be an array of strings.');
                }
            }
        }
        if ($levels === null) {
            $levels = $this->getDefaultPatchLevels();
        }

        return array($path, $levels);
    }
866407
public function removePackageUsage(PackageInterface $package)
    {
        $usageData = $this->getPackageUsage($package);
        $newUsageData = array();
        $projectName = $this->composer->getPackage()->getName();

        foreach ($usageData as $usage) {
            if ($projectName !== $usage) {
                $newUsageData[] = $usage;
            }
        }

        $this->updatePackageUsageFile($package, $newUsageData);
    }
275441
protected function mustInstallPatchesForPackage(PackageInterface $package)
    {
        $result = false;
        $patches = $this->getPatchCollection()->getPatchesFor($package);
        if ($patches->isEmpty()) {
            if ($this->io->isVerbose()) {
                $this->io->write('<info>No patches found for '.$package->getName().'.</info>');
            }
        } else {
            $result = true;
        }

        return $result;
    }
308912
private function getPackages(RepositoryInterface $repo)
    {
        $packages = [];

        if ($this->hasFilterForPackages()) {
            // apply package filter if defined
            foreach ($this->packagesFilter as $filter) {
                $packages += $repo->findPackages($filter);
            }
        } else {
            // process other repos directly
            $packages = $repo->getPackages();
        }

        return $packages;
    }
673396
private function getRequired(PackageInterface $package, bool $isRoot)
    {
        $required = [];

        if ($this->requireDependencies) {
            $required = $package->getRequires();
        }
        if (($isRoot || !$this->requireDependencyFilter) && $this->requireDevDependencies) {
            $required = array_merge($required, $package->getDevRequires());
        }

        return $required;
    }
1167276
public function getLoaderFilePackagePath(PackageInterface $package)
    {
        $installPath = $this->getInstallPath($package);

        if ($installPath === false) {
            return false;
        }

        $config      = $this->getInstallerConfig($package);
        $packagePath = $installPath . $config['loader'];

        return $packagePath;
    }
222227
public function patch(
        EnvironmentInterface $environment
    ) {
        foreach ($this->patchers as $patcher) {
            $patcher->patch($environment);
        }
    }
1352935
public function checkPatches(Event $event)
    {
        if (!$this->isPatchingEnabled()) {
            return;
        }

        try {
            $repositoryManager = $this->composer->getRepositoryManager();
            $localRepository = $repositoryManager->getLocalRepository();
            $installationManager = $this->composer->getInstallationManager();
            $packages = $localRepository->getPackages();

            $tmp_patches = $this->grabPatches();
            foreach ($packages as $package) {
                $extra = $package->getExtra();
                if (isset($extra['patches'])) {
                    $this->installedPatches[$package->getName()] = $extra['patches'];
                }
                $patches = isset($extra['patches']) ? $extra['patches'] : array();
                $tmp_patches = Util::arrayMergeRecursiveDistinct($tmp_patches, $patches);
            }

            if ($tmp_patches === false) {
                $this->io->write('<info>No patches supplied.</info>');
                return;
            }

            // Remove packages for which the patch set has changed.
            foreach ($packages as $package) {
                if (!($package instanceof AliasPackage)) {
                    $package_name = $package->getName();
                    $extra = $package->getExtra();
                    $has_patches = isset($tmp_patches[$package_name]);
                    $has_applied_patches = isset($extra['patches_applied']);
                    if (($has_patches && !$has_applied_patches)
                        || (!$has_patches && $has_applied_patches)
                        || ($has_patches && $has_applied_patches &&
                            $tmp_patches[$package_name] !== $extra['patches_applied'])) {
                        $uninstallOperation = new UninstallOperation(
                            $package,
                            'Removing package so it can be re-installed and re-patched.'
                        );
                        $this->io->write('<info>Removing package ' .
                            $package_name .
                            ' so that it can be re-installed and re-patched.</info>');
                        $installationManager->uninstall($localRepository, $uninstallOperation);
                    }
                }
            }
        } catch (\LogicException $e) {
            // If the Locker isn't available, then we don't need to do this.
            // It's the first time packages have been installed.
            return;
        }
    }
501689
protected function installGitignore(PackageInterface $package)
    {
        $installPath = $this->getInstallPath($package) . '/.gitignore';
        $templatePath = $this->getTempPath($package) . '/.gitignore.template';
        
        $data = ['APP_PUBLIC' => $this->plugin->getPublicDirectory()];
        
        if (! file_exists($installPath)) {
            return $this->compileTemplate($templatePath, $installPath, $data);
        }
        
        $downloadGitignore = $this->compileTemplate($templatePath, $data);
        $installGitignore = file_get_contents($installPath);
        
        $gitignore = preg_split('/\r?\n/', $downloadGitignore . PHP_EOL . '# User rules' . PHP_EOL . $installGitignore);
        $group = 'user rules';
        $groups = [];
        $rules = [];
        
        $groups['user rules'] = [];
        
        foreach ($gitignore as $rule) {
            if (preg_match('/^#\s*(.*)/', $rule, $matches)) {
                $group = strtolower($matches[1]);
                
                if (! isset($groups[$group])) {
                    $groups[$group] = [];
                }
            } elseif (! preg_match('/^\s*$/', $rule) && ! in_array($rule, $rules)) {
                $groups[$group][] = $rules[] = $rule;
            }
        }
        
        $gitignore = [];
        
        foreach ($groups as $group => $rules) {
            if (! count($rules) || $group == 'user rules') {
                continue;
            }
            
            $this->sortRules($rules);
            
            $gitignore[] = '# ' . ucfirst($group);
            $gitignore = array_merge($gitignore, $rules);
            $gitignore[] = '';
        }
        
        $this->sortRules($groups['user rules']);
        
        $gitignore[] = '# User rules';
        $gitignore = array_merge($gitignore, $groups['user rules']);
        $gitignore[] = '';
        
        file_put_contents($installPath, implode(PHP_EOL, $gitignore));
    }
968599
public function isForPackage(PackageInterface $package)
    {
        if ($this->getForPackage() !== $package->getName()) {
            return false;
        }
        $fpv = $this->getForPackageVersion();
        if ($fpv !== null) {
            $pv = new Constraint('=', $package->getVersion());
            if (!$fpv->matches($pv)) {
                return false;
            }
        }

        return true;
    }
732808
public function setPackage($package)
    {
        $this->package = implode('\\', array_map(function ($piece) {
            return Inflector::classify(Inflector::underscore($piece));
        }, explode('.', $package)));
        
        return $this;
    }