8000 bug #40917 [Config][DependencyInjection] Uniformize trailing slash ha… · symfony/symfony@0e738ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e738ef

Browse files
bug #40917 [Config][DependencyInjection] Uniformize trailing slash handling (dunglas)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [Config][DependencyInjection] Uniformize trailing slash handling | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a | License | MIT | Doc PR | n/a Currently, the handling of trailing slashes in file loaders exclusion rules is inconsistent, which can create hard to debug issues. Example: ```yaml services: App\: resource: '../src/' exclude: # This works - '../src/FooBar/DependencyInjection/' - '../src/FooBar/DependencyInjection' - '../src/FooBar/DependencyInjection/*' - '../src/*/DependencyInjection' - '../src/*/DependencyInjection/*' # This doesn't work - '../src/*/DependencyInjection/' ``` This PR fixes this subtle issue. Commits ------- dc50aa3 [Config][DependencyInjection] Uniformize trailing slash handling
2 parents fab61ee + dc50aa3 commit 0e738ef

File tree

8 files changed

+56
-4
lines changed

8 files changed

+56
-4
lines changed

src/Symfony/Component/Config/Loader/FileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public function import($resource, $type = null, $ignoreErrors = false, $sourceRe
8282
$excluded = [];
8383
foreach ((array) $exclude as $pattern) {
8484
foreach ($this->glob($pattern, true, $_, false, true) as $path => $info) {
85-
// normalize Windows slashes
86-
$excluded[str_replace('\\', '/', $path)] = true;
85+
// normalize Windows slashes and remove trailing slashes
86+
$excluded[rtrim(str_replace('\\', '/', $path), '/')] = true;
8787
}
8888
}
8989

src/Symfony/Component/Config/Tests/Fixtures/ExcludeTrailingSlash/bar.txt

Whitespace-only changes.

src/Symfony/Component/Config/Tests/Fixtures/ExcludeTrailingSlash/exclude/baz.txt

Whitespace-only changes.

src/Symfony/Component/Config/Tests/Fixtures/ExcludeTrailingSlash/foo.txt

Whitespace-only changes.

src/Symfony/Component/Config/Tests/Loader/FileLoaderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ public function testImportWithExclude()
127127
$this->assertCount(2, $loadedFiles);
128128
$this->assertNotContains('ExcludeFile.txt', $loadedFiles);
129129
}
130+
131+
/**
132+
* @dataProvider excludeTrailingSlashConsistencyProvider
133+
*/
134+
public function testExcludeTrailingSlashConsistency(string $exclude)
135+
{
136+
$loader = new TestFileLoader(new FileLocator(__DIR__.'/../Fixtures'));
137+
$loadedFiles = $loader->import('ExcludeTrailingSlash/*', null, false, null, $exclude);
138+
$this->assertCount(2, $loadedFiles);
139+
$this->assertNotContains('baz.txt', $loadedFiles);
140+
}
141+
142+
public function excludeTrailingSlashConsistencyProvider(): iterable
143+
{
144+
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/'];
145+
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo'];
146+
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/*'];
147+
yield [__DIR__.'/../Fixtures/*/ExcludeToo'];
148+
yield [__DIR__.'/../Fixtures/*/ExcludeToo/'];
149+
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/*'];
150+
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/AnotheExcludedFile.txt'];
151+
}
130152
}
131153

132154
class TestFileLoader extends FileLoader

src/Symfony/Component/Config/Tests/Resource/GlobResourceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public function testBraceFallback()
177177

178178
$expected = [
179179
$dir.'/Exclude/ExcludeToo/AnotheExcludedFile.txt',
180+
$dir.'/ExcludeTrailingSlash/exclude/baz.txt',
180181
$dir.'/foo.xml',
181182
];
182183

src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ private function findClasses(string $namespace, string $pattern, array $excludeP
166166
$excludePrefix = $resource->getPrefix();
167167
}
168168

169-
// normalize Windows slashes
170-
$excludePaths[str_replace('\\', '/', $path)] = true;
169+
// normalize Windows slashes and remove trailing slashes
170+
$excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true;
171171
}
172172
}
173173

src/Symfony/Component/DependencyInjection/Tests/Loader/FileLoaderTest.php

Lines changed: 29 additions & 0 deletions
Original file line number
Diff line numberDiff line change
@@ -239,6 +239,35 @@ public function testRegisterClassesWithIncompatibleExclude()
239239
'yaml/*'
240240
);
241241
}
242+
243+
/**
244+
* @dataProvider excludeTrailingSlashConsistencyProvider
245+
*/
246+
public function testExcludeTrailingSlashConsistency(string $exclude)
247+
{
248+
$container = new ContainerBuilder();
249+
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
250+
$loader->registerClasses(
251+
new Definition(),
252+
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
253+
'Prototype/*',
254+
$exclude
255+
);
256+
257+
$this->assertTrue($container->has(Foo::class));
258+
$this->assertFalse($container->has(DeeperBaz::class));
259+
}
260+
261+
public function excludeTrailingSlashConsistencyProvider(): iterable
262+
{
263+
yield ['Prototype/OtherDir/AnotherSub/'];
264+
yield ['Prototype/OtherDir/AnotherSub'];
265+
yield ['Prototype/OtherDir/AnotherSub/*'];
266+
yield ['Prototype/*/AnotherSub'];
267+
yield ['Prototype/*/AnotherSub/'];
268+
yield ['Prototype/*/AnotherSub/*'];
269+
yield ['Prototype/OtherDir/AnotherSub/DeeperBaz.php'];
270+
}
242271
}
243272

244273
class TestFileLoader extends FileLoader

0 commit comments

Comments
 (0)
0