10000 bug #25989 [DI][Routing] Fix tracking of globbed resources (nicolas-g… · symfony/symfony@c57258b · GitHub
[go: up one dir, main page]

Skip to content

Commit c57258b

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 ad98c1f [DI][Routing] Fix tracking of globbed resources
2 parents 9a3aa07 + 945c753 commit c57258b

File tree

8 files changed

+182
-6
lines changed

8 files changed

+182
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<argument type="service" id="file_locator" />
3939
</service>
4040

41-
<service id="routing.loader.glob" class="Symfony\Component\Config\Loader\GlobFileLoader">
41+
<service id="routing.loader.glob" class="Symfony\Component\Routing\Loader\GlobFileLoader">
4242
<tag name="routing.loader" />
4343
<argument type="service" id="file_locator" />
4444
</service>

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"symfony/polyfill-mbstring": "~1.0",
2929
"symfony/filesystem": "~2.8|~3.0|~4.0",
3030
"symfony/finder": "~2.8|~3.0|~4.0",
31-
"symfony/routing": "~3.4|~4.0"
31+
"symfony/routing": "^3.4.5|^4.0.5"
3232
},
3333
"require-dev": {
3434
"doctrine/cache": "~1.0",
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 <fabien@symfony.com>
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 <p@tchwork.com>
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+
}
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 <fabien@symfony.com>
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 F438 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+
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
2323
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2424
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
25+
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
2526
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
2627
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
2728
use Symfony\Component\Filesystem\Filesystem;
@@ -32,7 +33,6 @@
3233
use Symfony\Component\HttpKernel\Config\FileLocator;
3334
use Symfony\Component\HttpKernel\DependencyInjection\MergeExtensionConfigurationPass;
3435
use Symfony\Component\HttpKernel\DependencyInjection\AddAnnotatedClassesToCachePass;
35-
use Symfony\Component\Config\Loader\GlobFileLoader;
3636
use Symfony\Component\Config\Loader\LoaderResolver;
3737
use Symfony\Component\Config\Loader\DelegatingLoader;
3838
use Symfony\Component\Config\ConfigCache;
@@ -883,7 +883,7 @@ protected function getContainerLoader(ContainerInterface $container)
883883
new YamlFileLoader($container, $locator),
884884
new IniFileLoader($container, $locator),
885885
new PhpFileLoader($container, $locator),
886-
new GlobFileLoader($locator),
886+
new GlobFileLoader($container, $locator),
887887
new DirectoryLoader($container, $locator),
888888
new ClosureLoader($container),
889889
));

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"symfony/config": "~2.8|~3.0|~4.0",
2929
"symfony/console": "~2.8|~3.0|~4.0",
3030
"symfony/css-selector": "~2.8|~3.0|~4.0",
31-
"symfony/dependency-injection": "~3.4|~4.0",
31+
"symfony/dependency-injection": "^3.4.5|^4.0.5",
3232
"symfony/dom-crawler": "~2.8|~3.0|~4.0",
3333
"symfony/expression-language": "~2.8|~3.0|~4.0",
3434
"symfony/finder": "~2.8|~3.0|~4.0",
@@ -45,7 +45,7 @@
4545
},
4646
"conflict": {
4747
"symfony/config": "<2.8",
48-
"symfony/dependency-injection": "<3.4",
48+
"symfony/dependency-injection": "<3.4.5|<4.0.5,>=4",
4949
"symfony/var-dumper": "<3.3",
5050
"twig/twig": "<1.34|<2.4,>=2"
5151
},
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Routing\Loader;
13+
14+
use Symfony\Component\Config\Loader\FileLoader;
15+
use Symfony\Component\Routing\RouteCollection;
16+
17+
/**
18+
* GlobFileLoader loads files from a glob pattern.
19+
*
20+
* @author Nicolas Grekas <p@tchwork.com>
21+
*/
22+
class GlobFileLoader extends FileLoader
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function load($resource, $type = null)
28+
{
29+
$collection = new RouteCollection();
30+
31+
foreach ($this->glob($resource, false, $globResource) as $path => $info) {
32+
$collection->addCollection($this->import($path));
33+
}
34+
35+
$collection->addResource($globResource);
36+
37+
return $collection;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function supports($resource, $type = null)
44+
{
45+
return 'glob' === $type;
46+
}
47+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Routing\Tests\Loader;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\Resource\GlobResource;
16+
use Symfony\Component\Config\FileLocator;
17+
use Symfony\Component\Routing\Loader\GlobFileLoader;
18+
use Symfony\Component\Routing\RouteCollection;
19+
20+
class GlobFileLoaderTest extends TestCase
21+
{
22+
public function testSupports()
23+
{
24+
$loader = new GlobFileLoader(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(new FileLocator());
33+
$collection = $loader->load(__DIR__.'/../Fixtures/directory/*.yml');
34+
35+
$this->assertEquals(new GlobResource(__DIR__.'/../Fixtures/directory', '/*.yml', false), $collection->getResources()[0]);
36+
}
37+
}
38+
39+
class GlobFileLoaderWithoutImport extends GlobFileLoader
40+
{
41+
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
42+
{
43+
return new RouteCollection();
44+
}
45+
}

0 commit comments

Comments
 (0)
0