Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DI][Routing] recursive directory loading
Issue #11045 For now, the Routing DirectoryLoader requires the type `directory` to be specified so it does not conflict with `AnnotationDirectoryLoader`. However, this could be refactored.
- Loading branch information
1 parent
78cf382
commit 73f0ee2
Showing
16 changed files
with
305 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/DependencyInjection/Loader/DirectoryLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader; | ||
|
||
use Symfony\Component\Config\Resource\DirectoryResource; | ||
|
||
/** | ||
* DirectoryLoader is a recursive loader to go through directories | ||
* | ||
* @author Sebastien Lavoie <seb@wemakecustom.com> | ||
*/ | ||
class DirectoryLoader extends FileLoader | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load($file, $type = null) | ||
{ | ||
$file = rtrim($file, '/'); | ||
$path = $this->locator->locate($file); | ||
$this->container->addResource(new DirectoryResource($path)); | ||
|
||
foreach (scandir($path) as $dir) { | ||
if ($dir[0] !== '.') { | ||
if (is_dir($path.'/'.$dir)) { | ||
$dir .= '/'; // append / to allow recursion | ||
} | ||
|
||
$this->setCurrentDir($path); | ||
|
||
$this->import($dir, null, false, $path); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($resource, $type = null) | ||
{ | ||
if ('directory' === $type) { | ||
return true; | ||
} | ||
|
||
if (null === $type) { | ||
return preg_match('/\/$/', $resource) === 1; | ||
} | ||
|
||
return false; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/DependencyInjection/Tests/Fixtures/directory/import/import.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
imports: | ||
- { resource: ../recurse/ } |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/DependencyInjection/Tests/Fixtures/directory/recurse/simple.ini
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[parameters] | ||
ini = ini |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/DependencyInjection/Tests/Fixtures/directory/recurse/simple.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
parameters: | ||
yaml: yaml |
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/DependencyInjection/Tests/Fixtures/directory/simple.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
$container->setParameter('php', 'php'); |
85 changes: 85 additions & 0 deletions
85
src/Symfony/Component/DependencyInjection/Tests/Loader/DirectoryLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Loader; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; | ||
use Symfony\Component\DependencyInjection\Loader\IniFileLoader; | ||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | ||
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader; | ||
use Symfony\Component\Config\Loader\LoaderResolver; | ||
use Symfony\Component\Config\FileLocator; | ||
|
||
class DirectoryLoaderTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private static $fixturesPath; | ||
|
||
private $container; | ||
private $loader; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/'); | ||
} | ||
|
||
protected function setUp() | ||
{ | ||
$locator = new FileLocator(self::$fixturesPath); | ||
$this->container = new ContainerBuilder(); | ||
$this->loader = new DirectoryLoader($this->container, $locator); | ||
$resolver = new LoaderResolver(array( | ||
new PhpFileLoader($this->container, $locator), | ||
new IniFileLoader($this->container, $locator), | ||
new YamlFileLoader($this->container, $locator), | ||
$this->loader, | ||
)); | ||
$this->loader->setResolver($resolver); | ||
} | ||
|
||
public function testDirectoryCanBeLoadedRecursively() | ||
{ | ||
$this->loader->load('directory/'); | ||
$this->assertEquals(array('ini' => 'ini', 'yaml' => 'yaml', 'php' => 'php'), $this->container->getParameterBag()->all(), '->load() takes a single directory'); | ||
} | ||
|
||
public function testImports() | ||
{ | ||
$this->loader->resolve('directory/import/import.yml')->load('directory/import/import.yml'); | ||
$this->assertEquals(array('ini' => 'ini', 'yaml' => 'yaml'), $this->container->getParameterBag()->all(), '->load() takes a single file that imports a directory'); | ||
} | ||
|
||
/** | ||
* @covers Symfony\Component\DependencyInjection\Loader\DirectoryLoader::__construct | ||
* @covers Symfony\Component\DependencyInjection\Loader\DirectoryLoader::load | ||
* | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage The file "foo" does not exist (in: | ||
*/ | ||
public function testExceptionIsRaisedWhenDirectoryDoesNotExist() | ||
{ | ||
$this->loader->load('foo/'); | ||
} | ||
|
||
/** | ||
* @covers Symfony\Component\DependencyInjection\Loader\DirectoryLoader::supports | ||
*/ | ||
public function testSupports() | ||
{ | ||
$loader = new DirectoryLoader(new ContainerBuilder(), new FileLocator()); | ||
|
||
$this->assertTrue($loader->supports('directory/'), '->supports("directory/") returns true'); | ||
$this->assertTrue($loader->supports('directory/', 'directory'), '->supports("directory/", "directory") returns true'); | ||
$this->assertFalse($loader->supports('directory'), '->supports("directory") returns false'); | ||
$this->assertTrue($loader->supports('directory', 'directory'), '->supports("directory", "directory") returns true'); | ||
$this->assertFalse($loader->supports('directory', 'foo'), '->supports("directory, "foo") returns false'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Loader; | ||
|
||
use Symfony\Component\Config\Loader\FileLoader; | ||
use Symfony\Component\Routing\RouteCollection; | ||
use Symfony\Component\Config\Resource\DirectoryResource; | ||
|
||
class DirectoryLoader extends FileLoader | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load($file, $type = null) | ||
{ | ||
$path = $this->locator->locate($file); | ||
|
||
$collection = new RouteCollection(); | ||
$collection->addResource(new DirectoryResource($path)); | ||
|
||
foreach (scandir($path) as $dir) { | ||
if ($dir[0] !== '.') { | ||
$this->setCurrentDir($path); | ||
$subPath = $path.'/'.$dir; | ||
$subType = null; | ||
|
||
if (is_dir($subPath)) { | ||
$subPath .= '/'; | ||
$subType = 'directory'; | ||
} | ||
|
||
$subCollection = $this->import($subPath, $subType, false, $path); | ||
$collection->addCollection($subCollection); | ||
} | ||
} | ||
|
||
return $collection; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($resource, $type = null) | ||
{ | ||
// only when type is forced to directory, not to conflict with AnnotationLoader | ||
|
||
return 'directory' === $type; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/Routing/Tests/Fixtures/directory/recurse/routes1.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
route1: | ||
path: /route/1 |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/Routing/Tests/Fixtures/directory/recurse/routes2.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
route2: | ||
path: /route/2 |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Component/Routing/Tests/Fixtures/directory/routes3.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
route3: | ||
path: /route/3 |
3 changes: 3 additions & 0 deletions
3
src/Symfony/Component/Routing/Tests/Fixtures/directory_import/import.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
_directory: | ||
resource: "../directory" | ||
type: directory |
74 changes: 74 additions & 0 deletions
74
src/Symfony/Component/Routing/Tests/Loader/DirectoryLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Routing\Tests\Loader; | ||
|
||
use Symfony\Component\Routing\Loader\DirectoryLoader; | ||
use Symfony\Component\Routing\Loader\YamlFileLoader; | ||
use Symfony\Component\Routing\Loader\AnnotationFileLoader; | ||
use Symfony\Component\Config\Loader\LoaderResolver; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
class DirectoryLoaderTest extends AbstractAnnotationLoaderTest | ||
{ | ||
private $loader; | ||
private $reader; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$locator = new FileLocator(); | ||
$this->reader = $this->getReader(); | ||
$this->loader = new DirectoryLoader($locator); | ||
$resolver = new LoaderResolver(array( | ||
new YamlFileLoader($locator), | ||
new AnnotationFileLoader($locator, $this->getClassLoader($this->reader)), | ||
$this->loader, | ||
)); | ||
$this->loader->setResolver($resolver); | ||
} | ||
|
||
public function testLoadDirectory() | ||
{ | ||
$collection = $this->loader->load(__DIR__.'/../Fixtures/directory', 'directory'); | ||
$this->verifyCollection($collection); | ||
} | ||
|
||
public function testImportDirectory() | ||
{ | ||
$collection = $this->loader->load(__DIR__.'/../Fixtures/directory_import', 'directory'); | ||
$this->verifyCollection($collection); | ||
} | ||
|
||
private function verifyCollection(RouteCollection $collection) | ||
{ | ||
$routes = $collection->all(); | ||
|
||
$this->assertCount(3, $routes, 'Three routes are loaded'); | ||
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes); | ||
|
||
for ($i = 1; $i <= 3; $i++) { | ||
$this->assertSame('/route/'.$i, $routes["route".$i]->getPath()); | ||
} | ||
} | ||
|
||
public function testSupports() | ||
{ | ||
$fixturesDir = __DIR__.'/../Fixtures'; | ||
|
||
$this->assertFalse($this->loader->supports($fixturesDir), '->supports(*) returns false'); | ||
|
||
$this->assertTrue($this->loader->supports($fixturesDir, 'directory'), '->supports(*, "directory") returns true'); | ||
$this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports(*, "foo") returns false'); | ||
} | ||
} |