[Routing] refactored loaders
* refactored the import mechanism for better flexibility * added two methods to LoaderInterface: supports() and setResolver() * added a LoaderResolver interface * added a Loader base class * added new loaders: DelegatingLoader, ClosureLoader, and PhpFileLoader * changed the Router constructor signature (now takes a Loader)
This commit is contained in:
parent
e6cbfd7292
commit
14cecd5231
48
src/Symfony/Components/Routing/Loader/ClosureLoader.php
Normal file
48
src/Symfony/Components/Routing/Loader/ClosureLoader.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Resource\FileResource;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
* The Closure must return a RouteCollection instance.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* ClosureLoader loads routes from a PHP closure.
|
||||
*
|
||||
* @package Symfony
|
||||
* @subpackage Components_Routing
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
class ClosureLoader extends Loader
|
||||
{
|
||||
/**
|
||||
* Loads a Closure.
|
||||
*
|
||||
* @param \Closure $resource The resource
|
||||
*/
|
||||
public function load($closure)
|
||||
{
|
||||
return call_user_func($closure);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this class supports the given resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Boolean true if this class supports the given resource, false otherwise
|
||||
*/
|
||||
public function supports($resource)
|
||||
{
|
||||
return $resource instanceof \Closure;
|
||||
}
|
||||
}
|
70
src/Symfony/Components/Routing/Loader/DelegatingLoader.php
Normal file
70
src/Symfony/Components/Routing/Loader/DelegatingLoader.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Components\Routing\Loader;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* DelegatingLoader delegates route loading to other loaders using a loader resolver.
|
||||
*
|
||||
* @package Symfony
|
||||
* @subpackage Components_Routing
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
class DelegatingLoader extends Loader
|
||||
{
|
||||
protected $resolver;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \Symfony\Components\Routing\Loader\LoaderResolverInterface $resolver A LoaderResolverInterface instance
|
||||
*/
|
||||
public function __construct(LoaderResolverInterface $resolver)
|
||||
{
|
||||
$this->resolver = $resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return \Symfony\Components\Routing\RouteCollection A RouteCollection instance
|
||||
*/
|
||||
public function load($resource)
|
||||
{
|
||||
$loader = $this->resolver->resolve($resource);
|
||||
|
||||
if (false === $loader) {
|
||||
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" routing resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE')));
|
||||
}
|
||||
|
||||
return $loader->load($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this class supports the given resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Boolean true if this class supports the given resource, false otherwise
|
||||
*/
|
||||
public function supports($resource)
|
||||
{
|
||||
foreach ($this->resolver->getLoaders() as $loader) {
|
||||
if ($loader->supports($resource)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -18,8 +18,9 @@ namespace Symfony\Components\Routing\Loader;
|
||||
* @subpackage Components_Routing
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
abstract class FileLoader implements LoaderInterface
|
||||
abstract class FileLoader extends Loader
|
||||
{
|
||||
protected $currentDir;
|
||||
protected $paths;
|
||||
|
||||
/**
|
||||
@ -32,10 +33,27 @@ abstract class FileLoader implements LoaderInterface
|
||||
if (!is_array($paths)) {
|
||||
$paths = array($paths);
|
||||
}
|
||||
|
||||
$this->paths = $paths;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds routes from a resource.
|
||||
*
|
||||
* @param mixed $resource A Resource
|
||||
*
|
||||
* @return Symfony\Components\Routing\RouteCollection A RouteCollection instance
|
||||
*/
|
||||
public function import($resource)
|
||||
{
|
||||
$loader = $this->resolve($resource);
|
||||
|
||||
if ($loader instanceof FileLoader && null !== $this->currentDir) {
|
||||
$resource = $this->getAbsolutePath($resource, $this->currentDir);
|
||||
}
|
||||
|
||||
return $loader->load($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException When file is not found
|
||||
*/
|
||||
|
79
src/Symfony/Components/Routing/Loader/Loader.php
Normal file
79
src/Symfony/Components/Routing/Loader/Loader.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Components\Routing\Loader;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Loader is the abstract class used by all built-in loaders.
|
||||
*
|
||||
* @package Symfony
|
||||
* @subpackage Components_Routing
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
abstract class Loader implements LoaderInterface
|
||||
{
|
||||
protected $resolver;
|
||||
|
||||
/**
|
||||
* Gets the loader resolver.
|
||||
*
|
||||
* @return \Symfony\Components\Routing\Loader\LoaderResolver A LoaderResolver instance
|
||||
*/
|
||||
public function getResolver()
|
||||
{
|
||||
return $this->resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the loader resolver.
|
||||
*
|
||||
* @param \Symfony\Components\Routing\Loader\LoaderResolver $resolver A LoaderResolver instance
|
||||
*/
|
||||
public function setResolver(LoaderResolver $resolver)
|
||||
{
|
||||
$this->resolver = $resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds definitions and parameters from a resource.
|
||||
*
|
||||
* @param mixed $resource A Resource
|
||||
*/
|
||||
public function import($resource)
|
||||
{
|
||||
$this->resolve($resource)->load($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a loader able to load an imported resource
|
||||
*
|
||||
* @param mixed $resource A Resource
|
||||
*
|
||||
* @return Symfony\Components\Routing\Loader\LoaderInterface A LoaderInterface instance
|
||||
*
|
||||
* @throws \InvalidArgumentException if no loader is found
|
||||
*/
|
||||
public function resolve($resource)
|
||||
{
|
||||
$loader = false;
|
||||
if ($this->supports($resource)) {
|
||||
$loader = $this;
|
||||
} elseif (null !== $this->resolver) {
|
||||
$loader = $this->resolver->resolve($resource);
|
||||
}
|
||||
|
||||
if (false === $loader) {
|
||||
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" routing resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE')));
|
||||
}
|
||||
|
||||
return $loader;
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Symfony\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Loader\LoaderResolver;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
@ -28,4 +30,27 @@ interface LoaderInterface
|
||||
* @return RouteCollection A RouteCollection instance
|
||||
*/
|
||||
function load($resource);
|
||||
|
||||
/**
|
||||
* Returns true if this class supports the given resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Boolean true if this class supports the given resource, false otherwise
|
||||
*/
|
||||
function supports($resource);
|
||||
|
||||
/**
|
||||
* Gets the loader resolver.
|
||||
*
|
||||
* @return \Symfony\Components\Routing\Loader\LoaderResolver A LoaderResolver instance
|
||||
*/
|
||||
function getResolver();
|
||||
|
||||
/**
|
||||
* Sets the loader resolver.
|
||||
*
|
||||
* @param \Symfony\Components\Routing\Loader\LoaderResolver $resolver A LoaderResolver instance
|
||||
*/
|
||||
function setResolver(LoaderResolver $resolver);
|
||||
}
|
||||
|
76
src/Symfony/Components/Routing/Loader/LoaderResolver.php
Normal file
76
src/Symfony/Components/Routing/Loader/LoaderResolver.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Components\Routing\Loader;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoaderResolver selects a loader for a given resource..
|
||||
*
|
||||
* @package Symfony
|
||||
* @subpackage Components_Routing
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
class LoaderResolver implements LoaderResolverInterface
|
||||
{
|
||||
protected $loaders;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \Symfony\Components\Routing\Loader\LoaderInterface[] $loaders An array of loaders
|
||||
*/
|
||||
public function __construct(array $loaders = array())
|
||||
{
|
||||
$this->loaders = array();
|
||||
foreach ($loaders as $loader) {
|
||||
$this->addLoader($loader);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a loader able to load the resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Symfony\Components\Routing\Loader\LoaderInterface A LoaderInterface instance
|
||||
*/
|
||||
public function resolve($resource)
|
||||
{
|
||||
foreach ($this->loaders as $loader) {
|
||||
if ($loader->supports($resource)) {
|
||||
return $loader;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a loader.
|
||||
*
|
||||
* @param \Symfony\Components\Routing\Loader\LoaderInterface $loader A LoaderInterface instance
|
||||
*/
|
||||
public function addLoader(LoaderInterface $loader)
|
||||
{
|
||||
$this->loaders[] = $loader;
|
||||
$loader->setResolver($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the registered loaders.
|
||||
*
|
||||
* @return \Symfony\Components\Routing\Loader\LoaderInterface[] A array of LoaderInterface instances
|
||||
*/
|
||||
public function getLoaders()
|
||||
{
|
||||
return $this->loaders;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Components\Routing\Loader;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* LoaderResolverInterface selects a loader for a given resource.
|
||||
*
|
||||
* @package Symfony
|
||||
* @subpackage Components_Routing
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
interface LoaderResolverInterface
|
||||
{
|
||||
/**
|
||||
* Returns a loader able to load the resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Symfony\Components\Routing\Loader\LoaderInterface A LoaderInterface instance
|
||||
*/
|
||||
function resolve($resource);
|
||||
}
|
56
src/Symfony/Components/Routing/Loader/PhpFileLoader.php
Normal file
56
src/Symfony/Components/Routing/Loader/PhpFileLoader.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Resource\FileResource;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* This source file is subject to the MIT license that is bundled
|
||||
* with this source code in the file LICENSE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* PhpFileLoader loads routes from a PHP file.
|
||||
*
|
||||
* The file must return a RouteCollection instance.
|
||||
*
|
||||
* @package Symfony
|
||||
* @subpackage Components_Routing
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*/
|
||||
class PhpFileLoader extends FileLoader
|
||||
{
|
||||
/**
|
||||
* Loads an array of PHP files.
|
||||
*
|
||||
* @param mixed $resource The resource
|
||||
*/
|
||||
public function load($file)
|
||||
{
|
||||
$loader = $this;
|
||||
|
||||
$path = $this->findFile($file);
|
||||
|
||||
$collection = include $path;
|
||||
$this->currentDir = dirname($path);
|
||||
$collection->addResource(new FileResource($path));
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this class supports the given resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Boolean true if this class supports the given resource, false otherwise
|
||||
*/
|
||||
public function supports($resource)
|
||||
{
|
||||
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION);
|
||||
}
|
||||
}
|
@ -53,7 +53,10 @@ class XmlFileLoader extends FileLoader
|
||||
$this->parseRoute($collection, $node, $path);
|
||||
break;
|
||||
case 'import':
|
||||
$this->parseImport($collection, $node, $path);
|
||||
$resource = (string) $node->getAttribute('resource');
|
||||
$prefix = (string) $node->getAttribute('prefix');
|
||||
$this->currentDir = dirname($path);
|
||||
$collection->addCollection($this->import($resource), $prefix);
|
||||
break;
|
||||
default:
|
||||
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
|
||||
@ -63,6 +66,18 @@ class XmlFileLoader extends FileLoader
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this class supports the given resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Boolean true if this class supports the given resource, false otherwise
|
||||
*/
|
||||
public function supports($resource)
|
||||
{
|
||||
return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
protected function parseRoute(RouteCollection $collection, $definition, $file)
|
||||
{
|
||||
$defaults = array();
|
||||
@ -94,27 +109,6 @@ class XmlFileLoader extends FileLoader
|
||||
$collection->addRoute((string) $definition->getAttribute('id'), $route);
|
||||
}
|
||||
|
||||
protected function parseImport(RouteCollection $collection, $node, $file)
|
||||
{
|
||||
$class = null;
|
||||
if ($node->hasAttribute('class') && $import->getAttribute('class') !== get_class($this)) {
|
||||
$class = (string) $node->getAttribute('class');
|
||||
} else {
|
||||
// try to detect loader with the extension
|
||||
switch (pathinfo((string) $node->getAttribute('resource'), PATHINFO_EXTENSION)) {
|
||||
case 'yml':
|
||||
$class = 'Symfony\\Components\\Routing\\Loader\\YamlFileLoader';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$loader = null === $class ? $this : new $class($this->paths);
|
||||
|
||||
$importedFile = $this->getAbsolutePath((string) $node->getAttribute('resource'), dirname($file));
|
||||
|
||||
$collection->addCollection($loader->load($importedFile), (string) $node->getAttribute('prefix'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException When loading of XML file returns error
|
||||
*/
|
||||
|
@ -45,7 +45,9 @@ class YamlFileLoader extends FileLoader
|
||||
|
||||
foreach ($config as $name => $config) {
|
||||
if (isset($config['resource'])) {
|
||||
$this->parseImport($collection, $name, $config, $path);
|
||||
$prefix = isset($config['prefix']) ? $config['prefix'] : null;
|
||||
$this->currentDir = dirname($path);
|
||||
$collection->addCollection($this->import($config['resource']), $prefix);
|
||||
} elseif (isset($config['pattern'])) {
|
||||
$this->parseRoute($collection, $name, $config, $path);
|
||||
} else {
|
||||
@ -56,6 +58,18 @@ class YamlFileLoader extends FileLoader
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this class supports the given resource.
|
||||
*
|
||||
* @param mixed $resource A resource
|
||||
*
|
||||
* @return Boolean true if this class supports the given resource, false otherwise
|
||||
*/
|
||||
public function supports($resource)
|
||||
{
|
||||
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException When config pattern is not defined for the given route
|
||||
*/
|
||||
@ -74,34 +88,6 @@ class YamlFileLoader extends FileLoader
|
||||
$collection->addRoute($name, $route);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \InvalidArgumentException When import resource is not defined
|
||||
*/
|
||||
protected function parseImport(RouteCollection $collection, $name, $import, $file)
|
||||
{
|
||||
if (!isset($import['resource'])) {
|
||||
throw new \InvalidArgumentException(sprintf('You must define a "resource" when importing (%s).', $name));
|
||||
}
|
||||
|
||||
$class = null;
|
||||
if (isset($import['class']) && $import['class'] !== get_class($this)) {
|
||||
$class = $import['class'];
|
||||
} else {
|
||||
// try to detect loader with the extension
|
||||
switch (pathinfo($import['resource'], PATHINFO_EXTENSION)) {
|
||||
case 'xml':
|
||||
$class = 'Symfony\\Components\\Routing\\Loader\\XmlFileLoader';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$loader = null === $class ? $this : new $class($this->paths);
|
||||
|
||||
$importedFile = $this->getAbsolutePath($import['resource'], dirname($file));
|
||||
|
||||
$collection->addCollection($loader->load($importedFile), isset($import['prefix']) ? $import['prefix'] : null);
|
||||
}
|
||||
|
||||
protected function loadFile($file)
|
||||
{
|
||||
return Yaml::load($file);
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
namespace Symfony\Components\Routing;
|
||||
|
||||
use Symfony\Components\Routing\Loader\LoaderInterface;
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony framework.
|
||||
*
|
||||
@ -28,6 +30,7 @@ class Router implements RouterInterface
|
||||
protected $context;
|
||||
protected $loader;
|
||||
protected $collection;
|
||||
protected $resource;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -37,16 +40,18 @@ class Router implements RouterInterface
|
||||
* * cache_dir: The cache directory (or null to disable caching)
|
||||
* * debug: Whether to enable debugging or not (false by default)
|
||||
*
|
||||
* @param mixed $loader A PHP callable that returns a RouteCollection instance
|
||||
* @param Symfony\Components\Routing\Loader\LoaderInterface $loader A LoaderInterface instance
|
||||
* @param mixed $resource The main resource to load
|
||||
* @param array $options An array of options
|
||||
* @param array $context The context
|
||||
* @param array $defaults The default values
|
||||
*
|
||||
* @throws \InvalidArgumentException When unsupported option is provided
|
||||
*/
|
||||
public function __construct($loader, array $options = array(), array $context = array(), array $defaults = array())
|
||||
public function __construct(LoaderInterface $loader, $resource, array $options = array(), array $context = array(), array $defaults = array())
|
||||
{
|
||||
$this->loader = $loader;
|
||||
$this->resource = $resource;
|
||||
$this->context = $context;
|
||||
$this->defaults = $defaults;
|
||||
$this->options = array(
|
||||
@ -78,7 +83,7 @@ class Router implements RouterInterface
|
||||
public function getRouteCollection()
|
||||
{
|
||||
if (null === $this->collection) {
|
||||
$this->collection = call_user_func($this->loader);
|
||||
$this->collection = $this->loader->load($this->resource);
|
||||
}
|
||||
|
||||
return $this->collection;
|
||||
|
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Loader\LoaderResolver;
|
||||
use Symfony\Components\Routing\Loader\ClosureLoader;
|
||||
use Symfony\Components\Routing\Route;
|
||||
use Symfony\Components\Routing\RouteCollection;
|
||||
|
||||
class ClosureLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\ClosureLoader::supports
|
||||
*/
|
||||
public function testSupports()
|
||||
{
|
||||
$loader = new ClosureLoader();
|
||||
|
||||
$this->assertTrue($loader->supports(function ($container) {}), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\ClosureLoader::load
|
||||
*/
|
||||
public function testLoad()
|
||||
{
|
||||
$loader = new ClosureLoader();
|
||||
|
||||
$route = new Route('/');
|
||||
$routes = $loader->load(function () use ($route)
|
||||
{
|
||||
$routes = new RouteCollection();
|
||||
|
||||
$routes->addRoute('foo', $route);
|
||||
|
||||
return $routes;
|
||||
});
|
||||
|
||||
$this->assertEquals($route, $routes->getRoute('foo'), '->load() loads a \Closure resource');
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Loader\LoaderResolver;
|
||||
use Symfony\Components\Routing\Loader\DelegatingLoader;
|
||||
use Symfony\Components\Routing\Loader\XmlFileLoader;
|
||||
use Symfony\Components\Routing\Loader\ClosureLoader;
|
||||
use Symfony\Components\Routing\RouteCollection;
|
||||
use Symfony\Components\Routing\Route;
|
||||
|
||||
class DelegatingLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\DelegatingLoader::__construct
|
||||
*/
|
||||
public function testConstructor()
|
||||
{
|
||||
$resolver = new LoaderResolver();
|
||||
$loader = new DelegatingLoader($resolver);
|
||||
$this->assertTrue(true, '__construct() takes a loader resolver as its first argument');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\DelegatingLoader::getResolver
|
||||
* @covers Symfony\Components\Routing\Loader\DelegatingLoader::setResolver
|
||||
*/
|
||||
public function testGetSetResolver()
|
||||
{
|
||||
$resolver = new LoaderResolver();
|
||||
$loader = new DelegatingLoader($resolver);
|
||||
$this->assertSame($resolver, $loader->getResolver(), '->getResolver() gets the resolver loader');
|
||||
$loader->setResolver($resolver = new LoaderResolver());
|
||||
$this->assertSame($resolver, $loader->getResolver(), '->setResolver() sets the resolver loader');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\DelegatingLoader::supports
|
||||
*/
|
||||
public function testSupports()
|
||||
{
|
||||
$resolver = new LoaderResolver(array(
|
||||
$ini = new XmlFileLoader(array()),
|
||||
));
|
||||
$loader = new DelegatingLoader($resolver);
|
||||
|
||||
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\DelegatingLoader::load
|
||||
*/
|
||||
public function testLoad()
|
||||
{
|
||||
$resolver = new LoaderResolver(array(
|
||||
new ClosureLoader(),
|
||||
));
|
||||
$loader = new DelegatingLoader($resolver);
|
||||
|
||||
$route = new Route('/');
|
||||
$routes = $loader->load(function () use ($route)
|
||||
{
|
||||
$routes = new RouteCollection();
|
||||
|
||||
$routes->addRoute('foo', $route);
|
||||
|
||||
return $routes;
|
||||
});
|
||||
|
||||
$this->assertSame($route, $routes->getRoute('foo'), '->load() loads a resource using the loaders from the resolver');
|
||||
|
||||
try {
|
||||
$loader->load('foo.foo');
|
||||
$this->fail('->load() throws an \InvalidArgumentException if the resource cannot be loaded');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('\InvalidArgumentException', $e, '->load() throws an \InvalidArgumentException if the resource cannot be loaded');
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Loader\FileLoader;
|
||||
|
||||
class FileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\FileLoader::__construct
|
||||
*/
|
||||
public function testConstructor()
|
||||
{
|
||||
$loader = new ProjectLoader(__DIR__);
|
||||
$this->assertEquals(array(__DIR__), $loader->paths, '__construct() takes a path as its second argument');
|
||||
|
||||
$loader = new ProjectLoader(array(__DIR__, __DIR__));
|
||||
$this->assertEquals(array(__DIR__, __DIR__), $loader->paths, '__construct() takes an array of paths as its second argument');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\FileLoader::GetAbsolutePath
|
||||
* @covers Symfony\Components\Routing\Loader\FileLoader::isAbsolutePath
|
||||
*/
|
||||
public function testGetAbsolutePath()
|
||||
{
|
||||
$loader = new ProjectLoader(array(__DIR__.'/../Fixtures'));
|
||||
$this->assertEquals('/foo.xml', $loader->getAbsolutePath('/foo.xml'), '->getAbsolutePath() return the path unmodified if it is already an absolute path');
|
||||
$this->assertEquals('c:\\\\foo.xml', $loader->getAbsolutePath('c:\\\\foo.xml'), '->getAbsolutePath() return the path unmodified if it is already an absolute path');
|
||||
$this->assertEquals('c:/foo.xml', $loader->getAbsolutePath('c:/foo.xml'), '->getAbsolutePath() return the path unmodified if it is already an absolute path');
|
||||
$this->assertEquals('\\server\\foo.xml', $loader->getAbsolutePath('\\server\\foo.xml'), '->getAbsolutePath() return the path unmodified if it is already an absolute path');
|
||||
|
||||
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'FileLoaderTest.php', $loader->getAbsolutePath('FileLoaderTest.php', __DIR__), '->getAbsolutePath() returns an absolute filename if the file exists in the current path');
|
||||
|
||||
$this->assertEquals(__DIR__.'/../Fixtures/foo.xml', $loader->getAbsolutePath('foo.xml', __DIR__), '->getAbsolutePath() returns an absolute filename if the file exists in one of the paths given in the constructor');
|
||||
|
||||
$this->assertEquals('foobar.xml', $loader->getAbsolutePath('foobar.xml', __DIR__), '->getAbsolutePath() returns the path unmodified if it is unable to find it in the given paths');
|
||||
}
|
||||
}
|
||||
|
||||
class ProjectLoader extends FileLoader
|
||||
{
|
||||
public $paths;
|
||||
|
||||
public function load($resource)
|
||||
{
|
||||
}
|
||||
|
||||
public function supports($resource)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getAbsolutePath($file, $currentPath = null)
|
||||
{
|
||||
return parent::getAbsolutePath($file, $currentPath);
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Loader\LoaderResolver;
|
||||
use Symfony\Components\Routing\Loader\ClosureLoader;
|
||||
|
||||
class LoaderResolverTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\LoaderResolver::__construct
|
||||
*/
|
||||
public function testConstructor()
|
||||
{
|
||||
$resolver = new LoaderResolver(array(
|
||||
$loader = new ClosureLoader(),
|
||||
));
|
||||
|
||||
$this->assertEquals(array($loader), $resolver->getLoaders(), '__construct() takes an array of loaders as its first argument');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\LoaderResolver::resolve
|
||||
*/
|
||||
public function testResolve()
|
||||
{
|
||||
$resolver = new LoaderResolver(array(
|
||||
$loader = new ClosureLoader(),
|
||||
));
|
||||
|
||||
$this->assertFalse($resolver->resolve('foo.foo'), '->resolve() returns false if no loader is able to load the resource');
|
||||
$this->assertEquals($loader, $resolver->resolve(function () {}), '->resolve() returns the loader for the given resource');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\LoaderResolver::getLoaders
|
||||
* @covers Symfony\Components\Routing\Loader\LoaderResolver::addLoader
|
||||
*/
|
||||
public function testLoaders()
|
||||
{
|
||||
$resolver = new LoaderResolver();
|
||||
$resolver->addLoader($loader = new ClosureLoader());
|
||||
|
||||
$this->assertEquals(array($loader), $resolver->getLoaders(), 'addLoader() adds a loader');
|
||||
}
|
||||
}
|
64
tests/Symfony/Tests/Components/Routing/Loader/LoaderTest.php
Normal file
64
tests/Symfony/Tests/Components/Routing/Loader/LoaderTest.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Tests\Components\Routing\Loader;
|
||||
|
||||
use Symfony\Components\Routing\Loader\LoaderResolver;
|
||||
use Symfony\Components\Routing\Loader\Loader;
|
||||
use Symfony\Components\Routing\Loader\XmlFileLoader;
|
||||
|
||||
class LoaderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\Loader::getResolver
|
||||
* @covers Symfony\Components\Routing\Loader\Loader::setResolver
|
||||
*/
|
||||
public function testGetSetResolver()
|
||||
{
|
||||
$resolver = new LoaderResolver();
|
||||
$loader = new ProjectLoader1();
|
||||
$loader->setResolver($resolver);
|
||||
$this->assertSame($resolver, $loader->getResolver(), '->setResolver() sets the resolver loader');
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Components\Routing\Loader\Loader::resolve
|
||||
*/
|
||||
public function testResolve()
|
||||
{
|
||||
$resolver = new LoaderResolver(array(
|
||||
$ini = new XmlFileLoader(array()),
|
||||
));
|
||||
$loader = new ProjectLoader1();
|
||||
$loader->setResolver($resolver);
|
||||
|
||||
$this->assertSame($ini, $loader->resolve('foo.xml'), '->resolve() finds a loader');
|
||||
$this->assertSame($loader, $loader->resolve('foo.foo'), '->resolve() finds a loader');
|
||||
|
||||
try {
|
||||
$loader->resolve(new \stdClass());
|
||||
$this->fail('->resolve() throws an \InvalidArgumentException if the resource cannot be loaded');
|
||||
} catch (\Exception $e) {
|
||||
$this->assertInstanceOf('\InvalidArgumentException', $e, '->resolve() throws an \InvalidArgumentException if the resource cannot be loaded');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ProjectLoader1 extends Loader
|
||||
{
|
||||
public function load($resource)
|
||||
{
|
||||
}
|
||||
|
||||
public function supports($resource)
|
||||
{
|
||||
return is_string($resource) && 'foo' === pathinfo($resource, PATHINFO_EXTENSION);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user