Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 29 additions & 15 deletions src/MergePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand All @@ -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);
Expand All @@ -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}'"
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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));
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/PluginState.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* This file is part of the Composer Merge plugin.
*
Expand Down Expand Up @@ -35,6 +36,11 @@ class PluginState
*/
protected $includes = [];

/**
* @var array $includes
*/
protected $excludes = [];

/**
* @var array $requires
*/
Expand Down Expand Up @@ -160,6 +166,7 @@ public function loadSettings()
$config = array_merge(
[
'include' => [],
'exclude' => [],
'require' => [],
'recurse' => true,
'replace' => false,
Expand All @@ -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'];
Expand All @@ -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
*
Expand Down