[Config][DependencyInjection] Uniformize trailing slash handling

This commit is contained in:
Kévin Dunglas 2021-04-23 00:49:37 +02:00 committed by Nicolas Grekas
parent 2dd8445198
commit dc50aa3b55
8 changed files with 56 additions and 4 deletions

View File

@ -82,8 +82,8 @@ abstract class FileLoader extends Loader
$excluded = [];
foreach ((array) $exclude as $pattern) {
foreach ($this->glob($pattern, true, $_, false, true) as $path => $info) {
// normalize Windows slashes
$excluded[str_replace('\\', '/', $path)] = true;
// normalize Windows slashes and remove trailing slashes
$excluded[rtrim(str_replace('\\', '/', $path), '/')] = true;
}
}

View File

@ -127,6 +127,28 @@ class FileLoaderTest extends TestCase
$this->assertCount(2, $loadedFiles);
$this->assertNotContains('ExcludeFile.txt', $loadedFiles);
}
/**
* @dataProvider excludeTrailingSlashConsistencyProvider
*/
public function testExcludeTrailingSlashConsistency(string $exclude)
{
$loader = new TestFileLoader(new FileLocator(__DIR__.'/../Fixtures'));
$loadedFiles = $loader->import('ExcludeTrailingSlash/*', null, false, null, $exclude);
$this->assertCount(2, $loadedFiles);
$this->assertNotContains('baz.txt', $loadedFiles);
}
public function excludeTrailingSlashConsistencyProvider(): iterable
{
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/'];
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo'];
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/*'];
yield [__DIR__.'/../Fixtures/*/ExcludeToo'];
yield [__DIR__.'/../Fixtures/*/ExcludeToo/'];
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/*'];
yield [__DIR__.'/../Fixtures/Exclude/ExcludeToo/AnotheExcludedFile.txt'];
}
}
class TestFileLoader extends FileLoader

View File

@ -177,6 +177,7 @@ class GlobResourceTest extends TestCase
$expected = [
$dir.'/Exclude/ExcludeToo/AnotheExcludedFile.txt',
$dir.'/ExcludeTrailingSlash/exclude/baz.txt',
$dir.'/foo.xml',
];

View File

@ -166,8 +166,8 @@ abstract class FileLoader extends BaseFileLoader
$excludePrefix = $resource->getPrefix();
}
// normalize Windows slashes
$excludePaths[str_replace('\\', '/', $path)] = true;
// normalize Windows slashes and remove trailing slashes
$excludePaths[rtrim(str_replace('\\', '/', $path), '/')] = true;
}
}

View File

@ -239,6 +239,35 @@ class FileLoaderTest extends TestCase
'yaml/*'
);
}
/**
* @dataProvider excludeTrailingSlashConsistencyProvider
*/
public function testExcludeTrailingSlashConsistency(string $exclude)
{
$container = new ContainerBuilder();
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath.'/Fixtures'));
$loader->registerClasses(
new Definition(),
'Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\\',
'Prototype/*',
$exclude
);
$this->assertTrue($container->has(Foo::class));
$this->assertFalse($container->has(DeeperBaz::class));
}
public function excludeTrailingSlashConsistencyProvider(): iterable
{
yield ['Prototype/OtherDir/AnotherSub/'];
yield ['Prototype/OtherDir/AnotherSub'];
yield ['Prototype/OtherDir/AnotherSub/*'];
yield ['Prototype/*/AnotherSub'];
yield ['Prototype/*/AnotherSub/'];
yield ['Prototype/*/AnotherSub/*'];
yield ['Prototype/OtherDir/AnotherSub/DeeperBaz.php'];
}
}
class TestFileLoader extends FileLoader