diff --git a/README.md b/README.md index 845c3e0..0927853 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Simply install the plugin with composer: `composer require drupal-composer/prese ## Configuration -For configuring the paths you need to set `preserve-paths` within the `extra` of your root `composer.json`. +For configuring the paths you need to set `preserve-paths` within the `extra` of your `composer.json`. ```json { @@ -31,6 +31,24 @@ For configuring the paths you need to set `preserve-paths` within the `extra` of } ``` +In case you need to set the `preserve-paths` option in a dependent package, you must also set `preserve-paths-as-dependency` in the `composer.json` of the dependency. + +```json +{ + "extra": { + "preserve-paths-as-dependency": true, + "preserve-paths": [ + "web/sites/all/modules/contrib", + "web/sites/all/themes/contrib", + "web/sites/all/libraries", + "web/sites/all/drush" + ] + } +} +``` + +You can disable this setting in your root `composer.json` by setting `preserve-paths-ignore-dependencies` to true. + ## Example An example composer.json using [composer/installers](https://packagist.org/packages/composer/installers): diff --git a/src/PluginWrapper.php b/src/PluginWrapper.php index 3d387db..db5a7c9 100644 --- a/src/PluginWrapper.php +++ b/src/PluginWrapper.php @@ -64,7 +64,7 @@ public function prePackage(PackageEvent $event) $preserver = new PathPreserver( $paths, - $this->getPreservePaths(), + $this->getPreservePaths($packages), $this->composer->getConfig()->get('cache-dir'), $this->filesystem, $this->io @@ -158,13 +158,47 @@ protected function getUniqueNameFromPackages(array $packages) } /** - * Get preserve paths from root configuration. + * Get preserve paths. + * + * @param \Composer\Package\PackageInterface[] $packages * * @return string[] */ - protected function getPreservePaths() + protected function getPreservePaths($packages) { $extra = $this->composer->getPackage()->getExtra(); + $paths = array(); + + /* + * Root package allows that preserve paths from dependencies are also evaluated. + * Negative check as this feature is developed for situations where the root package + * can not be adjusted, but paths must be preserved. + */ + if (!isset($extra['preserve-paths-ignore-dependencies']) || !$extra['preserve-paths-ignore-dependencies']) { + foreach ($packages as $package) { + $paths = array_merge($paths, $this->extractPathsFromExtra($package->getExtra(), false)); + } + } + + $paths = array_unique(array_merge($paths, $this->extractPathsFromExtra($extra))); + + return $this->absolutePaths($paths); + } + + /** + * Extract the paths from the extra data. + * + * @param array $extra + * @param boolean $root + * + * @return string[] + */ + protected function extractPathsFromExtra($extra, $root = true) + { + // Package does not explicitly allow to preserve paths if used as dependency. + if (!$root && (!isset($extra['preserve-paths-as-dependency']) || $extra['preserve-paths-as-dependency'] != true)) { + return array(); + } if (!isset($extra['preserve-paths'])) { $paths = array(); @@ -174,7 +208,7 @@ protected function getPreservePaths() $paths = array_values((array) $extra['preserve-paths']); } - return $this->absolutePaths($paths); + return $paths; } /**