diff --git a/src/Capability/Patcher/DynamicPatchesPatcherProvider.php b/src/Capability/Patcher/DynamicPatchesPatcherProvider.php deleted file mode 100644 index f68cdf2..0000000 --- a/src/Capability/Patcher/DynamicPatchesPatcherProvider.php +++ /dev/null @@ -1,20 +0,0 @@ -composer, $this->io, $this->plugin), - new GitInitPatcher($this->composer, $this->io, $this->plugin), - new FreeformPatcher($this->composer, $this->io, $this->plugin) - ]; - } -} diff --git a/src/Patch.php b/src/Patch.php deleted file mode 100644 index 815d2a9..0000000 --- a/src/Patch.php +++ /dev/null @@ -1,17 +0,0 @@ -composer = $composer; - $this->io = $io; - $this->executor = new ProcessExecutor($this->io); - $this->patches = array(); - $this->installedPatches = array(); - $this->lockFile = new JsonFile( - static::getPatchesLockFilePath(), - null, - $this->io - ); - $this->locker = new Locker($this->lockFile); - $this->configuration = [ - 'disable-resolvers' => [ - 'type' => 'list', - 'default' => [], - ], - 'disable-downloaders' => [ - 'type' => 'list', - 'default' => [], - ], - 'disable-patchers' => [ - 'type' => 'list', - 'default' => [], - ], - 'default-patch-depth' => [ - 'type' => 'int', - 'default' => 1, - ], - 'package-depths' => [ - 'type' => 'list', - 'default' => [], - ], - 'patches-file' => [ - 'type' => 'string', - 'default' => 'patches.json', - ] - ]; - $this->configure($this->composer->getPackage()->getExtra(), 'composer-dynamic-patches'); - } - - /** - * @inheritDoc - */ - public function getCapabilities(): array { + public function getCapabilities(): array + { return [ - ResolverProvider::class => DynamicPatchesResolverProvider::class + ResolverProvider::class => DynamicPatchesResolverProvider::class ]; } - public function deactivate(Composer $composer, IOInterface $io){} - - public function uninstall(Composer $composer, IOInterface $io){} - + public function deactivate(Composer $composer, IOInterface $io) + { + } + public function uninstall(Composer $composer, IOInterface $io) + { + } } diff --git a/src/Resolver/DynamicPatchesFile.php b/src/Resolver/DynamicPatchesFile.php index cfb5dee..6e16c17 100644 --- a/src/Resolver/DynamicPatchesFile.php +++ b/src/Resolver/DynamicPatchesFile.php @@ -11,108 +11,116 @@ use Composer\Composer\InstalledVersions; use cweagans\Composer\Patch; use Composer\IO\IOInterface; +use Composer\Semver\Constraint\Constraint; use Composer\Util\HttpDownloader; use cweagans\Composer\PatchCollection; use rajeshreeputra\ComposerDynamicPatches\Resolver\DynamicPatchesResolverBase; use InvalidArgumentException; -class DynamicPatchesFile extends DynamicPatchesResolverBase { +class DynamicPatchesFile extends DynamicPatchesResolverBase +{ /** * {@inheritDoc} */ - public function resolve(PatchCollection $collection): void { + public function resolve(PatchCollection $collection): void + { $patches_file = $this->grabAllPatches(); - + $versionParser = new VersionParser(); foreach ($this->findPatchesInJson($patches_file) as $package => $patches) { - //print_r($patches); - $package_version = \Composer\InstalledVersions::getPrettyVersion($package); - foreach ($patches as $patch) { - if (!isset($patch->version) || - (version_compare($package_version, $patch->version) == 0)) { - /** @var Patch $patch */ - $collection->addPatch($patch); + $package_version = \Composer\InstalledVersions::getPrettyVersion($package); + $requiredConstraint = new Constraint('==', $package_version); + foreach ($patches as $patch) { + if (isset($patch->extra['version'])) { + $constraint = $versionParser->parseConstraints($patch->extra['version']); + if ( + $constraint->matches($requiredConstraint) || + (version_compare($package_version, $patch->extra['version']) == 0) + ) { + /** @var Patch $patch */ + $collection->addPatch($patch); + } + } else { + /** @var Patch $patch */ + $collection->addPatch($patch); + } } - - } } } /** * {@inheritDoc} */ - public function grabPatches($package) { - // First, try to get the patches from the root composer.json. - $extra = $package->getExtra(); - if (isset($extra['patches'])) { - $this->io->write('Gathering patches for root package.'); - $patches = $extra['patches']; - return $patches; - } - // If it's not specified there, look for a patches-file definition. - elseif (isset($extra['patches-file']) && is_string($extra['patches-file'])) { - $this->io->write('Gathering patches from patch file.'); - $patches = file_get_contents($extra['patches-file']); - $patches = json_decode($patches, TRUE); - $error = json_last_error(); - if ($error != 0) { - switch ($error) { - case JSON_ERROR_DEPTH: - $msg = ' - Maximum stack depth exceeded'; - break; - case JSON_ERROR_STATE_MISMATCH: - $msg = ' - Underflow or the modes mismatch'; - break; - case JSON_ERROR_CTRL_CHAR: - $msg = ' - Unexpected control character found'; - break; - case JSON_ERROR_SYNTAX: - $msg = ' - Syntax error, malformed JSON'; - break; - case JSON_ERROR_UTF8: - $msg = ' - Malformed UTF-8 characters, possibly incorrectly encoded'; - break; - default: - $msg = ' - Unknown error'; - break; + public function grabPatches($package) + { + // First, try to get the patches from the root composer.json. + $extra = $package->getExtra(); + if (isset($extra['patches'])) { + $this->io->write('Gathering patches for root package.'); + $patches = $extra['patches']; + return $patches; + } elseif (isset($extra['patches-file']) && is_string($extra['patches-file'])) { + // If it's not specified there, look for a patches-file definition. + $this->io->write('Gathering patches from patch file.'); + $patches = file_get_contents($extra['patches-file']); + $patches = json_decode($patches, true); + $error = json_last_error(); + if ($error != 0) { + switch ($error) { + case JSON_ERROR_DEPTH: + $msg = ' - Maximum stack depth exceeded'; + break; + case JSON_ERROR_STATE_MISMATCH: + $msg = ' - Underflow or the modes mismatch'; + break; + case JSON_ERROR_CTRL_CHAR: + $msg = ' - Unexpected control character found'; + break; + case JSON_ERROR_SYNTAX: + $msg = ' - Syntax error, malformed JSON'; + break; + case JSON_ERROR_UTF8: + $msg = ' - Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + $msg = ' - Unknown error'; + break; + } + throw new \Exception('There was an error in the supplied patches file:' . $msg); } - throw new \Exception('There was an error in the supplied patches file:' . $msg); - } - if (isset($patches['patches'])) { - $patches = $patches['patches']; - return $patches; - } - elseif(!$patches) { - throw new \Exception('There was an error in the supplied patch file'); + if (isset($patches['patches'])) { + $patches = $patches['patches']; + return $patches; + } elseif (!$patches) { + throw new \Exception('There was an error in the supplied patch file'); + } + } else { + return array(); } - } - else { - return array(); - } } /** * {@inheritDoc} */ - public function grabAllPatches() { - try { - $repositoryManager = $this->composer->getRepositoryManager(); - $localRepository = $repositoryManager->getLocalRepository(); - $installationManager = $this->composer->getInstallationManager(); - $packages = $localRepository->getPackages(); - $allPatches = []; - foreach ($packages as $package) { - $patches = $this->grabPatches($package); - if ($patches) { - $allPatches = $this->mergeDeep($allPatches, $patches); - } + public function grabAllPatches() + { + try { + $repositoryManager = $this->composer->getRepositoryManager(); + $localRepository = $repositoryManager->getLocalRepository(); + $installationManager = $this->composer->getInstallationManager(); + $packages = $localRepository->getPackages(); + $allPatches = []; + foreach ($packages as $package) { + $patches = $this->grabPatches($package); + if ($patches) { + $allPatches = $this->mergeDeep($allPatches, $patches); + } + } + return $allPatches; + } 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; } - return $allPatches; - } - // If the Locker isn't available, then we don't need to do this. - // It's the first time packages have been installed. - catch (\LogicException $e) { - return; - } } /** @@ -124,13 +132,37 @@ public function grabAllPatches() { * * Example: * @code - * $link_options_1 = array('fragment' => 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b'))); - * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd'))); + * $link_options_1 = [ + * 'fragment' => 'x', + * 'attributes' => [ + * 'title' => t('X'), + * 'class' => ['a', 'b'] + * ] + * ]; + * $link_options_1 = [ + * 'fragment' => 'x', + * 'attributes' => [ + * 'title' => t('X'), + * 'class' => ['c', 'd'] + * ] + * ]; * - * // This results in array('fragment' => array('x', 'y'), 'attributes' => array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', 'c', 'd'))). + * // This results in [ + * 'fragment' => ['x', 'y'], + * 'attributes' => [ + * 'title' => [t('X'), t('Y')], + * 'class' => ['a', 'b', 'c', 'd'] + * ] + * ]. * $incorrect = array_merge_recursive($link_options_1, $link_options_2); * - * // This results in array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))). + * // This results in [ + * 'fragment' => 'y', + * 'attributes' => [ + * 'title' => t('Y'), + * 'class' => ['a', 'b', 'c', 'd'] + * ] + * ]. * $correct = NestedArray::mergeDeep($link_options_1, $link_options_2); * @endcode * @@ -142,8 +174,9 @@ public function grabAllPatches() { * * @see NestedArray::mergeDeepArray() */ - public static function mergeDeep() { - return self::mergeDeepArray(func_get_args()); + public static function mergeDeep() + { + return self::mergeDeepArray(func_get_args()); } /** @@ -172,26 +205,25 @@ public static function mergeDeep() { * * @see NestedArray::mergeDeep() */ - public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) { - $result = []; - foreach ($arrays as $array) { - foreach ($array as $key => $value) { - // Renumber integer keys as array_merge_recursive() does unless - // $preserve_integer_keys is set to TRUE. Note that PHP automatically - // converts array keys that are integer strings (e.g., '1') to integers. - if (is_int($key) && !$preserve_integer_keys) { - $result[] = $value; - } - // Recurse when both values are arrays. - elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) { - $result[$key] = self::mergeDeepArray([$result[$key], $value], $preserve_integer_keys); - } - // Otherwise, use the latter value, overriding any previous value. - else { - $result[$key] = $value; - } + public static function mergeDeepArray(array $arrays, $preserve_integer_keys = false) + { + $result = []; + foreach ($arrays as $array) { + foreach ($array as $key => $value) { + // Renumber integer keys as array_merge_recursive() does unless + // $preserve_integer_keys is set to TRUE. Note that PHP automatically + // converts array keys that are integer strings (e.g., '1') to integers. + if (is_int($key) && !$preserve_integer_keys) { + $result[] = $value; + } elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) { + // Recurse when both values are arrays. + $result[$key] = self::mergeDeepArray([$result[$key], $value], $preserve_integer_keys); + } else { + // Otherwise, use the latter value, overriding any previous value. + $result[$key] = $value; + } + } } - } - return $result; + return $result; } } diff --git a/src/Resolver/DynamicPatchesResolverBase.php b/src/Resolver/DynamicPatchesResolverBase.php index 8d41de4..b10cef1 100644 --- a/src/Resolver/DynamicPatchesResolverBase.php +++ b/src/Resolver/DynamicPatchesResolverBase.php @@ -10,7 +10,7 @@ use Composer\Composer; use Composer\IO\IOInterface; use Composer\Plugin\PluginInterface; -use rajeshreeputra\ComposerDynamicPatches\Patch; +use cweagans\Composer\Patch; use cweagans\Composer\PatchCollection; use cweagans\Composer\Resolver\ResolverInterface; @@ -92,19 +92,18 @@ public function findPatchesInJson(array $patches): array IOInterface::VERBOSE ); - $temporary_patch_list = []; + $temp_patches = []; foreach ($patch_defs as $description => $url) { if (is_array($url)) { - foreach ($url as $patchdescription => $patchurl) { - $temporary_patch_list[] = $this->getPatches($package, $patchdescription, $patchurl, $description); - } - } - else { - $temporary_patch_list[] = $this->getPatches($package, $description, $url); + foreach ($url as $patchdescription => $patchurl) { + $temp_patches[] = $this->getPatches($package, $patchdescription, $patchurl, $description); + } + } else { + $temp_patches[] = $this->getPatches($package, $description, $url); } } - $patches[$package] = $temporary_patch_list; + $patches[$package] = $temp_patches; } } @@ -133,7 +132,7 @@ public function getPatches(string $package, string $description, string $url, st $patch->url = $url; $patch->description = $description; if (!empty($version)) { - $patch->version = $version; + $patch->extra['version'] = $version; } return $patch;