Skip to content

Commit 6591016

Browse files
committed
adding exclude feature
1 parent a4c4c62 commit 6591016

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ in the top-level composer.json file:
122122
(optional, see [merge-scripts](#merge-scripts) below)
123123

124124

125+
### exclude
126+
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.
127+
125128
### require
126129

127130
The `require` setting is identical to [`include`](#include) except when

src/MergePlugin.php

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ public function onInit(BaseEvent $event)
193193
// so assume it is false. The dev section will be merged later when
194194
// the other events fire.
195195
$this->state->setDevMode(false);
196-
$this->mergeFiles($this->state->getIncludes(), false);
197-
$this->mergeFiles($this->state->getRequires(), true);
196+
197+
$include_files = $this->findFiles($this->state->getIncludes(), false);
198+
$exclude_files = $this->findFiles($this->state->getExcludes(), false);
199+
200+
$this->mergeFiles(array_diff($include_files, $exclude_files));
201+
$this->mergeFiles($this->findFiles($this->state->getRequires(), true));
198202
}
199203

200204
/**
@@ -208,8 +212,12 @@ public function onInstallUpdateOrDump(ScriptEvent $event)
208212
{
209213
$this->state->loadSettings();
210214
$this->state->setDevMode($event->isDevMode());
211-
$this->mergeFiles($this->state->getIncludes(), false);
212-
$this->mergeFiles($this->state->getRequires(), true);
215+
216+
$include_files = $this->findFiles($this->state->getIncludes(), false);
217+
$exclude_files = $this->findFiles($this->state->getExcludes(), false);
218+
219+
$this->mergeFiles(array_diff($include_files, $exclude_files));
220+
$this->mergeFiles($this->findFiles($this->state->getRequires(), true));
213221

214222
if ($event->getName() === ScriptEvents::PRE_AUTOLOAD_DUMP) {
215223
$this->state->setDumpAutoloader(true);
@@ -224,17 +232,26 @@ public function onInstallUpdateOrDump(ScriptEvent $event)
224232
* Find configuration files matching the configured glob patterns and
225233
* merge their contents with the master package.
226234
*
227-
* @param array $patterns List of files/glob patterns
228-
* @param bool $required Are the patterns required to match files?
235+
* @param array $files List of files
229236
* @throws MissingFileException when required and a pattern returns no
230237
* results
231238
*/
232-
protected function mergeFiles(array $patterns, $required = false)
239+
protected function mergeFiles(array $files)
233240
{
234241
$root = $this->composer->getPackage();
242+
foreach ($files as $path) {
243+
$this->mergeFile($root, $path);
244+
}
245+
}
235246

236-
$files = array_map(
237-
static function ($files, $pattern) use ($required) {
247+
/**
248+
* @param array $patterns A list for files/patterns
249+
* @param bool $required Are the patterns required to match files?
250+
*/
251+
protected function findFiles(array $patterns, bool $required = false)
252+
{
253+
$found_files = array_map(
254+
function ($files, $pattern) use ($required) {
238255
if ($required && !$files) {
239256
throw new MissingFileException(
240257
"merge-plugin: No files matched required '{$pattern}'"
@@ -245,10 +262,7 @@ static function ($files, $pattern) use ($required) {
245262
array_map('glob', $patterns),
246263
$patterns
247264
);
248-
249-
foreach (array_reduce($files, 'array_merge', []) as $path) {
250-
$this->mergeFile($root, $path);
251-
}
265+
return array_reduce($found_files, 'array_merge', array());
252266
}
253267

254268
/**
@@ -295,8 +309,8 @@ protected function mergeFile(RootPackageInterface $root, $path)
295309
}
296310

297311
if ($this->state->recurseIncludes()) {
298-
$this->mergeFiles($package->getIncludes(), false);
299-
$this->mergeFiles($package->getRequires(), true);
312+
$this->mergeFiles($this->findFiles($package->getIncludes(), false));
313+
$this->mergeFiles($this->findFiles($package->getRequires(), true));
300314
}
301315
}
302316

src/PluginState.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* This file is part of the Composer Merge plugin.
45
*
@@ -35,6 +36,11 @@ class PluginState
3536
*/
3637
protected $includes = [];
3738

39+
/**
40+
* @var array $includes
41+
*/
42+
protected $excludes = [];
43+
3844
/**
3945
* @var array $requires
4046
*/
@@ -160,6 +166,7 @@ public function loadSettings()
160166
$config = array_merge(
161167
[
162168
'include' => [],
169+
'exclude' => [],
163170
'require' => [],
164171
'recurse' => true,
165172
'replace' => false,
@@ -175,6 +182,8 @@ public function loadSettings()
175182

176183
$this->includes = (is_array($config['include'])) ?
177184
$config['include'] : [$config['include']];
185+
$this->excludes = (is_array($config['exclude'])) ?
186+
$config['exclude'] : [$config['exclude']];
178187
$this->requires = (is_array($config['require'])) ?
179188
$config['require'] : [$config['require']];
180189
$this->recurse = (bool)$config['recurse'];
@@ -197,6 +206,16 @@ public function getIncludes()
197206
return $this->includes;
198207
}
199208

209+
/**
210+
* Get list of filenames and/or glob patterns to excludes
211+
*
212+
* @return array
213+
*/
214+
public function getExcludes()
215+
{
216+
return $this->excludes;
217+
}
218+
200219
/**
201220
* Get list of filenames and/or glob patterns to require
202221
*

0 commit comments

Comments
 (0)