Skip to content

Commit 3247e1b

Browse files
bug #61667 [Config] Fix GlobResource pattern with trailing slash (HypeMC)
This PR was merged into the 6.4 branch. Discussion ---------- [Config] Fix `GlobResource` pattern with trailing slash | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT I've noticed that when using exclude patterns in service imports, a trailing slash doesn't always work: * `../src/Foo/` - works with the trailing slash * `../src/**/Foo/` - doesn't work with the trailing slash Commits ------- ba0712da49a [Config] Fix `GlobResource` pattern with trailing slash
2 parents 3f1e308 + 82dca41 commit 3247e1b

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

Resource/GlobResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function getIterator(): \Traversable
111111
if (class_exists(Finder::class)) {
112112
$regex = Glob::toRegex($pattern);
113113
if ($this->recursive) {
114-
$regex = substr_replace($regex, '(/|$)', -2, 1);
114+
$regex = substr_replace($regex, str_ends_with($pattern, '/') ? '' : '(/|$)', -2, 1);
115115
}
116116
} else {
117117
$regex = null;

Tests/Resource/GlobResourceTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,22 @@ protected function tearDown(): void
2525
touch($dir.'/Resource/.hiddenFile');
2626
}
2727

28-
public function testIterator()
28+
/**
29+
* @testWith ["/Resource"]
30+
* ["/**\/Resource"]
31+
* ["/**\/Resource/"]
32+
*/
33+
public function testIterator(string $pattern)
2934
{
3035
$dir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
31-
$resource = new GlobResource($dir, '/Resource', true);
36+
$resource = new GlobResource($dir, $pattern, true);
3237

3338
$paths = iterator_to_array($resource);
3439

3540
$file = $dir.'/Resource'.\DIRECTORY_SEPARATOR.'ConditionalClass.php';
3641
$this->assertEquals([$file => new \SplFileInfo($file)], $paths);
3742
$this->assertInstanceOf(\SplFileInfo::class, current($paths));
3843
$this->assertSame($dir, $resource->getPrefix());
39-
40-
$resource = new GlobResource($dir, '/**/Resource', true);
41-
42-
$paths = iterator_to_array($resource);
43-
44-
$file = $dir.'/Resource'.\DIRECTORY_SEPARATOR.'ConditionalClass.php';
45-
$this->assertEquals([$file => $file], $paths);
46-
$this->assertInstanceOf(\SplFileInfo::class, current($paths));
47-
$this->assertSame($dir, $resource->getPrefix());
4844
}
4945

5046
public function testIteratorForExclusionDoesntIterateThroughSubfolders()

0 commit comments

Comments
 (0)