Skip to content

Commit a44d8c8

Browse files
committed
[Routing][Config] Allow patterns of resources to be excluded from config loading
1 parent 8adb559 commit a44d8c8

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* added a way to exclude patterns of resources from being imported by the `import()` method
8+
49
4.3.0
510
-----
611

Loader/FileLoader.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,37 @@ public function getLocator()
5959
/**
6060
* Imports a resource.
6161
*
62-
* @param mixed $resource A Resource
63-
* @param string|null $type The resource type or null if unknown
64-
* @param bool $ignoreErrors Whether to ignore import errors or not
65-
* @param string|null $sourceResource The original resource importing the new resource
62+
* @param mixed $resource A Resource
63+
* @param string|null $type The resource type or null if unknown
64+
* @param bool $ignoreErrors Whether to ignore import errors or not
65+
* @param string|null $sourceResource The original resource importing the new resource
66+
* @param string|string[]|null $exclude Glob patterns to exclude from the import
6667
*
6768
* @return mixed
6869
*
6970
* @throws LoaderLoadException
7071
* @throws FileLoaderImportCircularReferenceException
7172
* @throws FileLocatorFileNotFoundException
7273
*/
73-
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
74+
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null/*, $exclude = null*/)
7475
{
76+
if (\func_num_args() < 5 && __CLASS__ !== \get_class($this) && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface) {
77+
@trigger_error(sprintf('The "%s()" method will have a new "$exclude = null" argument in version 5.0, not defining it is deprecated since Symfony 4.4.', __METHOD__), E_USER_DEPRECATED);
78+
}
79+
$exclude = \func_num_args() >= 5 ? func_get_arg(4) : null;
80+
7581
if (\is_string($resource) && \strlen($resource) !== $i = strcspn($resource, '*?{[')) {
82+
$excluded = [];
83+
foreach ((array) $exclude as $pattern) {
84+
foreach ($this->glob($pattern, true, $_, false, true) as $path => $info) {
85+
// normalize Windows slashes
86+
$excluded[str_replace('\\', '/', $path)] = true;
87+
}
88+
}
89+
7690
$ret = [];
7791
$isSubpath = 0 !== $i && false !== strpos(substr($resource, 0, $i), '/');
78-
foreach ($this->glob($resource, false, $_, $ignoreErrors || !$isSubpath) as $path => $info) {
92+
foreach ($this->glob($resource, false, $_, $ignoreErrors || !$isSubpath, false, $excluded) as $path => $info) {
7993
if (null !== $res = $this->doImport($path, $type, $ignoreErrors, $sourceResource)) {
8094
$ret[] = $res;
8195
}

Tests/Fixtures/Include/ExcludeFile.txt

Whitespace-only changes.

Tests/Fixtures/Include/IncludeAnotherFile.txt

Whitespace-only changes.

Tests/Fixtures/Include/IncludeFile.txt

Whitespace-only changes.

Tests/Loader/FileLoaderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ public function testImportWithSimpleGlob()
9292

9393
$this->assertSame(__FILE__, strtr($loader->import('FileLoaderTest.*'), '/', \DIRECTORY_SEPARATOR));
9494
}
95+
96+
public function testImportWithExclude()
97+
{
98+
$loader = new TestFileLoader(new FileLocator(__DIR__.'/../Fixtures'));
99+
$loadedFiles = $loader->import('Include/*', null, false, null, __DIR__.'/../Fixtures/Include/{ExcludeFile.txt}');
100+
$this->assertCount(2, $loadedFiles);
101+
$this->assertNotContains('ExcludeFile.txt', $loadedFiles);
102+
}
95103
}
96104

97105
class TestFileLoader extends FileLoader

0 commit comments

Comments
 (0)