Skip to content

Commit 4b3926a

Browse files
bug #25989 [DI][Routing] Fix tracking of globbed resources (nicolas-grekas, sroze)
This PR was merged into the 3.4 branch. Discussion ---------- [DI][Routing] Fix tracking of globbed resources | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25904 | License | MIT | Doc PR | - The current `GlobFileLoader` in `Config` misses resource tracking, so we can't use it and have to use a per-component one instead. (deps=high failures will be fixed after merging up to master.) Commits ------- 945c753 Add tests for glob loaders ad98c1fa [DI][Routing] Fix tracking of globbed resources
2 parents f0faac9 + a5be36f commit 4b3926a

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Loader/GlobFileLoader.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader;
13+
14+
/**
15+
* GlobFileLoader loads files from a glob pattern.
16+
*
17+
* @author Nicolas Grekas <[email protected]>
18+
*/
19+
class GlobFileLoader extends FileLoader
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function load($resource, $type = null)
25+
{
26+
foreach ($this->glob($resource, false, $globResource) as $path => $info) {
27+
$this->import($path);
28+
}
29+
30+
$this->container->addResource($globResource);
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function supports($resource, $type = null)
37+
{
38+
return 'glob' === $type;
39+
}
40+
}

Tests/Loader/GlobFileLoaderTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Loader;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\Resource\GlobResource;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
18+
use Symfony\Component\Config\FileLocator;
19+
20+
class GlobFileLoaderTest extends TestCase
21+
{
22+
public function testSupports()
23+
{
24+
$loader = new GlobFileLoader(new ContainerBuilder(), new FileLocator());
25+
26+
$this->assertTrue($loader->supports('any-path', 'glob'), '->supports() returns true if the resource has the glob type');
27+
$this->assertFalse($loader->supports('any-path'), '->supports() returns false if the resource is not of glob type');
28+
}
29+
30+
public function testLoadAddsTheGlobResourceToTheContainer()
31+
{
32+
$loader = new GlobFileLoaderWithoutImport($container = new ContainerBuilder(), new FileLocator());
33+
$loader->load(__DIR__.'/../Fixtures/config/*');
34+
35+
$this->assertEquals(new GlobResource(__DIR__.'/../Fixtures/config', '/*', false), $container->getResources()[1]);
36+
}
37+
}
38+
39+
class GlobFileLoaderWithoutImport extends GlobFileLoader
40+
{
41+
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
42+
{
43+
}
44+
}

0 commit comments

Comments
 (0)