diff --git a/README.md b/README.md index 771b3dc..080587b 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,9 @@ in the top-level composer.json file: (optional, see [merge-scripts](#merge-scripts) below) +### exclude +The `exclude` setting can stop a single or multiple files from being used in the [include](#include) setting. Similar to `include`, this setting takes a pattern that follows `glob` rules. + ### require The `require` setting is identical to [`include`](#include) except when diff --git a/src/MergePlugin.php b/src/MergePlugin.php index a9af5b6..20b0de0 100644 --- a/src/MergePlugin.php +++ b/src/MergePlugin.php @@ -193,8 +193,12 @@ public function onInit(BaseEvent $event) // so assume it is false. The dev section will be merged later when // the other events fire. $this->state->setDevMode(false); - $this->mergeFiles($this->state->getIncludes(), false); - $this->mergeFiles($this->state->getRequires(), true); + + $include_files = $this->findFiles($this->state->getIncludes(), false); + $exclude_files = $this->findFiles($this->state->getExcludes(), false); + + $this->mergeFiles(array_diff($include_files, $exclude_files)); + $this->mergeFiles($this->findFiles($this->state->getRequires(), true)); } /** @@ -208,8 +212,12 @@ public function onInstallUpdateOrDump(ScriptEvent $event) { $this->state->loadSettings(); $this->state->setDevMode($event->isDevMode()); - $this->mergeFiles($this->state->getIncludes(), false); - $this->mergeFiles($this->state->getRequires(), true); + + $include_files = $this->findFiles($this->state->getIncludes(), false); + $exclude_files = $this->findFiles($this->state->getExcludes(), false); + + $this->mergeFiles(array_diff($include_files, $exclude_files)); + $this->mergeFiles($this->findFiles($this->state->getRequires(), true)); if ($event->getName() === ScriptEvents::PRE_AUTOLOAD_DUMP) { $this->state->setDumpAutoloader(true); @@ -224,17 +232,26 @@ public function onInstallUpdateOrDump(ScriptEvent $event) * Find configuration files matching the configured glob patterns and * merge their contents with the master package. * - * @param array $patterns List of files/glob patterns - * @param bool $required Are the patterns required to match files? + * @param array $files List of files * @throws MissingFileException when required and a pattern returns no * results */ - protected function mergeFiles(array $patterns, $required = false) + protected function mergeFiles(array $files) { $root = $this->composer->getPackage(); + foreach ($files as $path) { + $this->mergeFile($root, $path); + } + } - $files = array_map( - static function ($files, $pattern) use ($required) { + /** + * @param array $patterns A list for files/patterns + * @param bool $required Are the patterns required to match files? + */ + protected function findFiles(array $patterns, bool $required = false) + { + $found_files = array_map( + function ($files, $pattern) use ($required) { if ($required && !$files) { throw new MissingFileException( "merge-plugin: No files matched required '{$pattern}'" @@ -245,10 +262,7 @@ static function ($files, $pattern) use ($required) { array_map('glob', $patterns), $patterns ); - - foreach (array_reduce($files, 'array_merge', []) as $path) { - $this->mergeFile($root, $path); - } + return array_reduce($found_files, 'array_merge', array()); } /** @@ -295,8 +309,8 @@ protected function mergeFile(RootPackageInterface $root, $path) } if ($this->state->recurseIncludes()) { - $this->mergeFiles($package->getIncludes(), false); - $this->mergeFiles($package->getRequires(), true); + $this->mergeFiles($this->findFiles($package->getIncludes(), false)); + $this->mergeFiles($this->findFiles($package->getRequires(), true)); } } diff --git a/src/PluginState.php b/src/PluginState.php index 1987883..5a97a29 100644 --- a/src/PluginState.php +++ b/src/PluginState.php @@ -1,4 +1,5 @@ [], + 'exclude' => [], 'require' => [], 'recurse' => true, 'replace' => false, @@ -175,6 +182,8 @@ public function loadSettings() $this->includes = (is_array($config['include'])) ? $config['include'] : [$config['include']]; + $this->excludes = (is_array($config['exclude'])) ? + $config['exclude'] : [$config['exclude']]; $this->requires = (is_array($config['require'])) ? $config['require'] : [$config['require']]; $this->recurse = (bool)$config['recurse']; @@ -197,6 +206,16 @@ public function getIncludes() return $this->includes; } + /** + * Get list of filenames and/or glob patterns to excludes + * + * @return array + */ + public function getExcludes() + { + return $this->excludes; + } + /** * Get list of filenames and/or glob patterns to require *