8000 [DependencyInjection][Config] Uniformize trailing slash handling · symfony/symfony@05e6278 · GitHub
[go: up one dir, main page]

Skip to content

Commit 05e6278

Browse files
committed
[DependencyInjection][Config] Uniformize trailing slash handling
1 parent 2dd8445 commit 05e6278

File tree

7 files changed

+56
-4
lines changed

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

132155
class TestFileLoader extends FileLoader

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 numberDiff 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): void
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