Laravel version update

Laravel version update
This commit is contained in:
Manish Verma
2018-08-06 18:48:58 +05:30
parent d143048413
commit 126fbb0255
13678 changed files with 1031482 additions and 778530 deletions

View File

@@ -12,11 +12,11 @@
namespace Symfony\Component\Routing\Loader;
use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
/**
* AnnotationClassLoader loads routing information from a PHP class and its methods.
@@ -57,9 +57,6 @@ use Symfony\Component\Config\Loader\LoaderResolverInterface;
*/
abstract class AnnotationClassLoader implements LoaderInterface
{
/**
* @var Reader
*/
protected $reader;
/**
@@ -72,11 +69,6 @@ abstract class AnnotationClassLoader implements LoaderInterface
*/
protected $defaultRouteIndex = 0;
/**
* Constructor.
*
* @param Reader $reader
*/
public function __construct(Reader $reader)
{
$this->reader = $reader;
@@ -127,6 +119,17 @@ abstract class AnnotationClassLoader implements LoaderInterface
}
}
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
foreach ($this->reader->getClassAnnotations($class) as $annot) {
if ($annot instanceof $this->routeAnnotationClass) {
$globals['path'] = '';
$globals['name'] = '';
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
}
}
}
return $collection;
}
@@ -136,10 +139,11 @@ abstract class AnnotationClassLoader implements LoaderInterface
if (null === $name) {
$name = $this->getDefaultRouteName($class, $method);
}
$name = $globals['name'].$name;
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
foreach ($method->getParameters() as $param) {
if (!isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
if (false !== strpos($globals['path'].$annot->getPath(), sprintf('{%s}', $param->getName())) && !isset($defaults[$param->getName()]) && $param->isDefaultValueAvailable()) {
$defaults[$param->getName()] = $param->getDefaultValue();
}
}
@@ -170,7 +174,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
*/
public function supports($resource, $type = null)
{
return is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || 'annotation' === $type);
}
/**
@@ -217,9 +221,14 @@ abstract class AnnotationClassLoader implements LoaderInterface
'methods' => array(),
'host' => '',
'condition' => '',
'name' => '',
);
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
if (null !== $annot->getName()) {
$globals['name'] = $annot->getName();
}
if (null !== $annot->getPath()) {
$globals['path'] = $annot->getPath();
}

View File

@@ -11,8 +11,8 @@
namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Routing\RouteCollection;
/**
* AnnotationDirectoryLoader loads routing information from annotations set
@@ -34,11 +34,21 @@ class AnnotationDirectoryLoader extends AnnotationFileLoader
*/
public function load($path, $type = null)
{
$dir = $this->locator->locate($path);
if (!is_dir($dir = $this->locator->locate($path))) {
return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
}
$collection = new RouteCollection();
$collection->addResource(new DirectoryResource($dir, '/\.php$/'));
$files = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY));
$files = iterator_to_array(new \RecursiveIteratorIterator(
new \RecursiveCallbackFilterIterator(
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
function (\SplFileInfo $current) {
return '.' !== substr($current->getBasename(), 0, 1);
}
),
\RecursiveIteratorIterator::LEAVES_ONLY
));
usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
return (string) $a > (string) $b ? 1 : -1;
});
@@ -66,16 +76,18 @@ class AnnotationDirectoryLoader extends AnnotationFileLoader
*/
public function supports($resource, $type = null)
{
if (!is_string($resource)) {
if ('annotation' === $type) {
return true;
}
if ($type || !\is_string($resource)) {
return false;
}
try {
$path = $this->locator->locate($resource);
return is_dir($this->locator->locate($resource));
} catch (\Exception $e) {
return false;
}
return is_dir($path) && (!$type || 'annotation' === $type);
}
}

View File

@@ -11,10 +11,10 @@
namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\RouteCollection;
/**
* AnnotationFileLoader loads routing information from annotations set
@@ -27,16 +27,11 @@ class AnnotationFileLoader extends FileLoader
protected $loader;
/**
* Constructor.
*
* @param FileLocatorInterface $locator A FileLocator instance
* @param AnnotationClassLoader $loader An AnnotationClassLoader instance
*
* @throws \RuntimeException
*/
public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader)
{
if (!function_exists('token_get_all')) {
if (!\function_exists('token_get_all')) {
throw new \RuntimeException('The Tokenizer extension is required for the routing annotation loaders.');
}
@@ -64,7 +59,7 @@ class AnnotationFileLoader extends FileLoader
$collection->addResource(new FileResource($path));
$collection->addCollection($this->loader->load($class, $type));
}
if (PHP_VERSION_ID >= 70000) {
if (\PHP_VERSION_ID >= 70000) {
// PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098
gc_mem_caches();
}
@@ -77,7 +72,7 @@ class AnnotationFileLoader extends FileLoader
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
return \is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'annotation' === $type);
}
/**
@@ -92,6 +87,11 @@ class AnnotationFileLoader extends FileLoader
$class = false;
$namespace = false;
$tokens = token_get_all(file_get_contents($file));
if (1 === \count($tokens) && T_INLINE_HTML === $tokens[0][0]) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain PHP code. Did you forgot to add the "<?php" start tag at the beginning of the file?', $file));
}
for ($i = 0; isset($tokens[$i]); ++$i) {
$token = $tokens[$i];
@@ -105,29 +105,29 @@ class AnnotationFileLoader extends FileLoader
if (true === $namespace && T_STRING === $token[0]) {
$namespace = $token[1];
while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_NS_SEPARATOR, T_STRING))) {
while (isset($tokens[++$i][1]) && \in_array($tokens[$i][0], array(T_NS_SEPARATOR, T_STRING))) {
$namespace .= $tokens[$i][1];
}
$token = $tokens[$i];
}
if (T_CLASS === $token[0]) {
// Skip usage of ::class constant
$isClassConstant = false;
// Skip usage of ::class constant and anonymous classes
$skipClassToken = false;
for ($j = $i - 1; $j > 0; --$j) {
if (!isset($tokens[$j][1])) {
break;
}
if (T_DOUBLE_COLON === $tokens[$j][0]) {
$isClassConstant = true;
if (T_DOUBLE_COLON === $tokens[$j][0] || T_NEW === $tokens[$j][0]) {
$skipClassToken = true;
break;
} elseif (!in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) {
} elseif (!\in_array($tokens[$j][0], array(T_WHITESPACE, T_DOC_COMMENT, T_COMMENT))) {
break;
}
}
if (!$isClassConstant) {
if (!$skipClassToken) {
$class = true;
}
}

View File

@@ -0,0 +1,81 @@
<?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\Configurator;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class CollectionConfigurator
{
use Traits\AddTrait;
use Traits\RouteTrait;
private $parent;
private $parentConfigurator;
public function __construct(RouteCollection $parent, $name, self $parentConfigurator = null)
{
$this->parent = $parent;
$this->name = $name;
$this->collection = new RouteCollection();
$this->route = new Route('');
$this->parentConfigurator = $parentConfigurator; // for GC control
}
public function __destruct()
{
$this->collection->addPrefix(rtrim($this->route->getPath(), '/'));
$this->parent->addCollection($this->collection);
}
/**
* Adds a route.
*
* @param string $name
* @param string $path
*
* @return RouteConfigurator
*/
final public function add($name, $path)
{
$this->collection->add($this->name.$name, $route = clone $this->route);
return new RouteConfigurator($this->collection, $route->setPath($path), $this->name, $this);
}
/**
* Creates a sub-collection.
*
* @return self
*/
final public function collection($name = '')
{
return new self($this->collection, $this->name.$name, $this);
}
/**
* Sets the prefix to add to the path of all child routes.
*
* @param string $prefix
*
* @return $this
*/
final public function prefix($prefix)
{
$this->route->setPath($prefix);
return $this;
}
}

View File

@@ -0,0 +1,49 @@
<?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\Configurator;
use Symfony\Component\Routing\RouteCollection;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ImportConfigurator
{
use Traits\RouteTrait;
private $parent;
public function __construct(RouteCollection $parent, RouteCollection $route)
{
$this->parent = $parent;
$this->route = $route;
}
public function __destruct()
{
$this->parent->addCollection($this->route);
}
/**
* Sets the prefix to add to the path of all child routes.
*
* @param string $prefix
*
* @return $this
*/
final public function prefix($prefix)
{
$this->route->addPrefix($prefix);
return $this;
}
}

View File

@@ -0,0 +1,34 @@
<?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\Configurator;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class RouteConfigurator
{
use Traits\AddTrait;
use Traits\RouteTrait;
private $parentConfigurator;
public function __construct(RouteCollection $collection, Route $route, $name = '', CollectionConfigurator $parentConfigurator = null)
{
$this->collection = $collection;
$this->route = $route;
$this->name = $name;
$this->parentConfigurator = $parentConfigurator; // for GC control
}
}

View File

@@ -0,0 +1,62 @@
<?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\Configurator;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\RouteCollection;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class RoutingConfigurator
{
use Traits\AddTrait;
private $loader;
private $path;
private $file;
public function __construct(RouteCollection $collection, PhpFileLoader $loader, $path, $file)
{
$this->collection = $collection;
$this->loader = $loader;
$this->path = $path;
$this->file = $file;
}
/**
* @return ImportConfigurator
*/
final public function import($resource, $type = null, $ignoreErrors = false)
{
$this->loader->setCurrentDir(\dirname($this->path));
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file);
if (!\is_array($imported)) {
return new ImportConfigurator($this->collection, $imported);
}
$mergedCollection = new RouteCollection();
foreach ($imported as $subCollection) {
$mergedCollection->addCollection($subCollection);
}
return new ImportConfigurator($this->collection, $mergedCollection);
}
/**
* @return CollectionConfigurator
*/
final public function collection($name = '')
{
return new CollectionConfigurator($this->collection, $name);
}
}

View File

@@ -0,0 +1,55 @@
<?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\Configurator\Traits;
use Symfony\Component\Routing\Loader\Configurator\RouteConfigurator;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
trait AddTrait
{
/**
* @var RouteCollection
*/
private $collection;
private $name = '';
/**
* Adds a route.
*
* @param string $name
* @param string $path
*
* @return RouteConfigurator
*/
final public function add($name, $path)
{
$parentConfigurator = $this instanceof RouteConfigurator ? $this->parentConfigurator : null;
$this->collection->add($this->name.$name, $route = new Route($path));
return new RouteConfigurator($this->collection, $route, '', $parentConfigurator);
}
/**
* Adds a route.
*
* @param string $name
* @param string $path
*
* @return RouteConfigurator
*/
final public function __invoke($name, $path)
{
return $this->add($name, $path);
}
}

View File

@@ -0,0 +1,131 @@
<?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\Configurator\Traits;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
trait RouteTrait
{
/**
* @var RouteCollection|Route
*/
private $route;
/**
* Adds defaults.
*
* @return $this
*/
final public function defaults(array $defaults)
{
$this->route->addDefaults($defaults);
return $this;
}
/**
* Adds requirements.
*
* @return $this
*/
final public function requirements(array $requirements)
{
$this->route->addRequirements($requirements);
return $this;
}
/**
* Adds options.
*
* @return $this
*/
final public function options(array $options)
{
$this->route->addOptions($options);
return $this;
}
/**
* Sets the condition.
*
* @param string $condition
*
* @return $this
*/
final public function condition($condition)
{
$this->route->setCondition($condition);
return $this;
}
/**
* Sets the pattern for the host.
*
* @param string $pattern
*
* @return $this
*/
final public function host($pattern)
{
$this->route->setHost($pattern);
return $this;
}
/**
* Sets the schemes (e.g. 'https') this route is restricted to.
* So an empty array means that any scheme is allowed.
*
* @param string[] $schemes
*
* @return $this
*/
final public function schemes(array $schemes)
{
$this->route->setSchemes($schemes);
return $this;
}
/**
* Sets the HTTP methods (e.g. 'POST') this route is restricted to.
* So an empty array means that any method is allowed.
*
* @param string[] $methods
*
* @return $this
*/
final public function methods(array $methods)
{
$this->route->setMethods($methods);
return $this;
}
/**
* Adds the "_controller" entry to defaults.
*
* @param callable|string $controller a callable or parseable pseudo-callable
*
* @return $this
*/
final public function controller($controller)
{
$this->route->addDefaults(array('_controller' => $controller));
return $this;
}
}

View File

@@ -11,7 +11,7 @@
namespace Symfony\Component\Routing\Loader\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\Routing\Loader\ObjectRouteLoader;
/**

View File

@@ -12,8 +12,8 @@
namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Config\Resource\DirectoryResource;
use Symfony\Component\Routing\RouteCollection;
class DirectoryLoader extends FileLoader
{

View File

@@ -0,0 +1,47 @@
<?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;
/**
* GlobFileLoader loads files from a glob pattern.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class GlobFileLoader extends FileLoader
{
/**
* {@inheritdoc}
*/
public function load($resource, $type = null)
{
$collection = new RouteCollection();
foreach ($this->glob($resource, false, $globResource) as $path => $info) {
$collection->addCollection($this->import($path));
}
$collection->addResource($globResource);
return $collection;
}
/**
* {@inheritdoc}
*/
public function supports($resource, $type = null)
{
return 'glob' === $type;
}
}

View File

@@ -45,7 +45,7 @@ abstract class ObjectRouteLoader extends Loader
public function load($resource, $type = null)
{
$parts = explode(':', $resource);
if (count($parts) != 2) {
if (2 != \count($parts)) {
throw new \InvalidArgumentException(sprintf('Invalid resource "%s" passed to the "service" route loader: use the format "service_name:methodName"', $resource));
}
@@ -54,20 +54,20 @@ abstract class ObjectRouteLoader extends Loader
$loaderObject = $this->getServiceObject($serviceString);
if (!is_object($loaderObject)) {
throw new \LogicException(sprintf('%s:getServiceObject() must return an object: %s returned', get_class($this), gettype($loaderObject)));
if (!\is_object($loaderObject)) {
throw new \LogicException(sprintf('%s:getServiceObject() must return an object: %s returned', \get_class($this), \gettype($loaderObject)));
}
if (!method_exists($loaderObject, $method)) {
throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s"', $method, get_class($loaderObject), $resource));
throw new \BadMethodCallException(sprintf('Method "%s" not found on "%s" when importing routing resource "%s"', $method, \get_class($loaderObject), $resource));
}
$routeCollection = call_user_func(array($loaderObject, $method), $this);
$routeCollection = \call_user_func(array($loaderObject, $method), $this);
if (!$routeCollection instanceof RouteCollection) {
$type = is_object($routeCollection) ? get_class($routeCollection) : gettype($routeCollection);
$type = \is_object($routeCollection) ? \get_class($routeCollection) : \gettype($routeCollection);
throw new \LogicException(sprintf('The %s::%s method must return a RouteCollection: %s returned', get_class($loaderObject), $method, $type));
throw new \LogicException(sprintf('The %s::%s method must return a RouteCollection: %s returned', \get_class($loaderObject), $method, $type));
}
// make the service file tracked so that if it changes, the cache rebuilds

View File

@@ -13,6 +13,7 @@ namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollection;
/**
@@ -35,9 +36,23 @@ class PhpFileLoader extends FileLoader
public function load($file, $type = null)
{
$path = $this->locator->locate($file);
$this->setCurrentDir(dirname($path));
$this->setCurrentDir(\dirname($path));
// the closure forbids access to the private scope in the included file
$loader = $this;
$load = \Closure::bind(function ($file) use ($loader) {
return include $file;
}, null, ProtectedPhpFileLoader::class);
$result = $load($path);
if ($result instanceof \Closure) {
$collection = new RouteCollection();
$result(new RoutingConfigurator($collection, $this, $path, $file), $this);
} else {
$collection = $result;
}
$collection = self::includeFile($path, $this);
$collection->addResource(new FileResource($path));
return $collection;
@@ -48,19 +63,13 @@ class PhpFileLoader extends FileLoader
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
}
/**
* Safe include. Used for scope isolation.
*
* @param string $file File to include
* @param PhpFileLoader $loader the loader variable is exposed to the included file below
*
* @return RouteCollection
*/
private static function includeFile($file, PhpFileLoader $loader)
{
return include $file;
return \is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'php' === $type);
}
}
/**
* @internal
*/
final class ProtectedPhpFileLoader extends PhpFileLoader
{
}

View File

@@ -11,11 +11,11 @@
namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\Util\XmlUtils;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* XmlFileLoader loads XML routing files.
@@ -36,8 +36,8 @@ class XmlFileLoader extends FileLoader
*
* @return RouteCollection A RouteCollection instance
*
* @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be
* parsed because it does not validate against the scheme.
* @throws \InvalidArgumentException when the file cannot be loaded or when the XML cannot be
* parsed because it does not validate against the scheme
*/
public function load($file, $type = null)
{
@@ -93,7 +93,7 @@ class XmlFileLoader extends FileLoader
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
return \is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
}
/**
@@ -144,28 +144,35 @@ class XmlFileLoader extends FileLoader
list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
$this->setCurrentDir(dirname($path));
$this->setCurrentDir(\dirname($path));
$subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);
$imported = $this->import($resource, ('' !== $type ? $type : null), false, $file);
$collection->addCollection($subCollection);
if (!\is_array($imported)) {
$imported = array($imported);
}
foreach ($imported as $subCollection) {
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);
$collection->addCollection($subCollection);
}
}
/**
@@ -202,12 +209,16 @@ class XmlFileLoader extends FileLoader
$condition = null;
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
if ($node !== $n->parentNode) {
continue;
}
switch ($n->localName) {
case 'default':
if ($this->isElementValueNull($n)) {
$defaults[$n->getAttribute('key')] = null;
} else {
$defaults[$n->getAttribute('key')] = trim($n->textContent);
$defaults[$n->getAttribute('key')] = $this->parseDefaultsConfig($n, $path);
}
break;
@@ -221,13 +232,120 @@ class XmlFileLoader extends FileLoader
$condition = trim($n->textContent);
break;
default:
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path));
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement", "option" or "condition".', $n->localName, $path));
}
}
if ($controller = $node->getAttribute('controller')) {
if (isset($defaults['_controller'])) {
$name = $node->hasAttribute('id') ? sprintf('"%s"', $node->getAttribute('id')) : sprintf('the "%s" tag', $node->tagName);
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" attribute and the defaults key "_controller" for %s.', $path, $name));
}
$defaults['_controller'] = $controller;
}
return array($defaults, $requirements, $options, $condition);
}
/**
* Parses the "default" elements.
*
* @param \DOMElement $element The "default" element to parse
* @param string $path Full path of the XML file being processed
*
* @return array|bool|float|int|string|null The parsed value of the "default" element
*/
private function parseDefaultsConfig(\DOMElement $element, $path)
{
if ($this->isElementValueNull($element)) {
return;
}
// Check for existing element nodes in the default element. There can
// only be a single element inside a default element. So this element
// (if one was found) can safely be returned.
foreach ($element->childNodes as $child) {
if (!$child instanceof \DOMElement) {
continue;
}
if (self::NAMESPACE_URI !== $child->namespaceURI) {
continue;
}
return $this->parseDefaultNode($child, $path);
}
// If the default element doesn't contain a nested "bool", "int", "float",
// "string", "list", or "map" element, the element contents will be treated
// as the string value of the associated default option.
return trim($element->textContent);
}
/**
* Recursively parses the value of a "default" element.
*
* @param \DOMElement $node The node value
* @param string $path Full path of the XML file being processed
*
* @return array|bool|float|int|string The parsed value
*
* @throws \InvalidArgumentException when the XML is invalid
*/
private function parseDefaultNode(\DOMElement $node, $path)
{
if ($this->isElementValueNull($node)) {
return;
}
switch ($node->localName) {
case 'bool':
return 'true' === trim($node->nodeValue) || '1' === trim($node->nodeValue);
case 'int':
return (int) trim($node->nodeValue);
case 'float':
return (float) trim($node->nodeValue);
case 'string':
return trim($node->nodeValue);
case 'list':
$list = array();
foreach ($node->childNodes as $element) {
if (!$element instanceof \DOMElement) {
continue;
}
if (self::NAMESPACE_URI !== $element->namespaceURI) {
continue;
}
$list[] = $this->parseDefaultNode($element, $path);
}
return $list;
case 'map':
$map = array();
foreach ($node->childNodes as $element) {
if (!$element instanceof \DOMElement) {
continue;
}
if (self::NAMESPACE_URI !== $element->namespaceURI) {
continue;
}
$map[$element->getAttribute('key')] = $this->parseDefaultNode($element, $path);
}
return $map;
default:
throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "bool", "int", "float", "string", "list", or "map".', $node->localName, $path));
}
}
private function isElementValueNull(\DOMElement $element)
{
$namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';

View File

@@ -11,12 +11,12 @@
namespace Symfony\Component\Routing\Loader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser as YamlParser;
use Symfony\Component\Config\Loader\FileLoader;
/**
* YamlFileLoader loads Yaml routing files.
@@ -27,7 +27,7 @@ use Symfony\Component\Config\Loader\FileLoader;
class YamlFileLoader extends FileLoader
{
private static $availableKeys = array(
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
);
private $yamlParser;
@@ -57,10 +57,18 @@ class YamlFileLoader extends FileLoader
$this->yamlParser = new YamlParser();
}
$prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) {
$message = E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message;
return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
});
try {
$parsedConfig = $this->yamlParser->parse(file_get_contents($path));
$parsedConfig = $this->yamlParser->parseFile($path);
} catch (ParseException $e) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
} finally {
restore_error_handler();
}
$collection = new RouteCollection();
@@ -72,7 +80,7 @@ class YamlFileLoader extends FileLoader
}
// not an array
if (!is_array($parsedConfig)) {
if (!\is_array($parsedConfig)) {
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
}
@@ -94,7 +102,7 @@ class YamlFileLoader extends FileLoader
*/
public function supports($resource, $type = null)
{
return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
return \is_string($resource) && \in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
}
/**
@@ -115,6 +123,10 @@ class YamlFileLoader extends FileLoader
$methods = isset($config['methods']) ? $config['methods'] : array();
$condition = isset($config['condition']) ? $config['condition'] : null;
if (isset($config['controller'])) {
$defaults['_controller'] = $config['controller'];
}
$route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
$collection->add($name, $route);
@@ -140,28 +152,39 @@ class YamlFileLoader extends FileLoader
$schemes = isset($config['schemes']) ? $config['schemes'] : null;
$methods = isset($config['methods']) ? $config['methods'] : null;
$this->setCurrentDir(dirname($path));
if (isset($config['controller'])) {
$defaults['_controller'] = $config['controller'];
}
$subCollection = $this->import($config['resource'], $type, false, $file);
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);
$this->setCurrentDir(\dirname($path));
$collection->addCollection($subCollection);
$imported = $this->import($config['resource'], $type, false, $file);
if (!\is_array($imported)) {
$imported = array($imported);
}
foreach ($imported as $subCollection) {
/* @var $subCollection RouteCollection */
$subCollection->addPrefix($prefix);
if (null !== $host) {
$subCollection->setHost($host);
}
if (null !== $condition) {
$subCollection->setCondition($condition);
}
if (null !== $schemes) {
$subCollection->setSchemes($schemes);
}
if (null !== $methods) {
$subCollection->setMethods($methods);
}
$subCollection->addDefaults($defaults);
$subCollection->addRequirements($requirements);
$subCollection->addOptions($options);
$collection->addCollection($subCollection);
}
}
/**
@@ -176,7 +199,7 @@ class YamlFileLoader extends FileLoader
*/
protected function validate($config, $name, $path)
{
if (!is_array($config)) {
if (!\is_array($config)) {
throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
}
if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
@@ -203,5 +226,8 @@ class YamlFileLoader extends FileLoader
$name, $path
));
}
if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
}
}
}

View File

@@ -26,7 +26,7 @@
<xsd:group name="configs">
<xsd:choice>
<xsd:element name="default" nillable="true" type="element" />
<xsd:element name="default" nillable="true" type="default" />
<xsd:element name="requirement" type="element" />
<xsd:element name="option" type="element" />
<xsd:element name="condition" type="xsd:string" />
@@ -41,6 +41,7 @@
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
<xsd:attribute name="controller" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="import">
@@ -52,6 +53,19 @@
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="schemes" type="xsd:string" />
<xsd:attribute name="methods" type="xsd:string" />
<xsd:attribute name="controller" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="default" mixed="true">
<xsd:choice minOccurs="0" maxOccurs="1">
<xsd:element name="bool" type="xsd:boolean" />
<xsd:element name="int" type="xsd:integer" />
<xsd:element name="float" type="xsd:float" />
<xsd:element name="string" type="xsd:string" />
<xsd:element name="list" type="list" />
<xsd:element name="map" type="map" />
</xsd:choice>
<xsd:attribute name="key" type="xsd:string" use="required" />
</xsd:complexType>
<xsd:complexType name="element">
@@ -61,4 +75,74 @@
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="list">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="bool" nillable="true" type="xsd:boolean" />
<xsd:element name="int" nillable="true" type="xsd:integer" />
<xsd:element name="float" nillable="true" type="xsd:float" />
<xsd:element name="string" nillable="true" type="xsd:string" />
<xsd:element name="list" nillable="true" type="list" />
<xsd:element name="map" nillable="true" type="map" />
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="map">
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="bool" nillable="true" type="map-bool-entry" />
<xsd:element name="int" nillable="true" type="map-int-entry" />
<xsd:element name="float" nillable="true" type="map-float-entry" />
<xsd:element name="string" nillable="true" type="map-string-entry" />
<xsd:element name="list" nillable="true" type="map-list-entry" />
<xsd:element name="map" nillable="true" type="map-map-entry" />
</xsd:choice>
</xsd:complexType>
<xsd:complexType name="map-bool-entry">
<xsd:simpleContent>
<xsd:extension base="xsd:boolean">
<xsd:attribute name="key" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="map-int-entry">
<xsd:simpleContent>
<xsd:extension base="xsd:integer">
<xsd:attribute name="key" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="map-float-entry">
<xsd:simpleContent>
<xsd:extension base="xsd:float">
<xsd:attribute name="key" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="map-string-entry">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="key" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="map-list-entry">
<xsd:complexContent>
<xsd:extension base="list">
<xsd:attribute name="key" type="xsd:string" use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="map-map-entry">
<xsd:complexContent>
<xsd:extension base="map">
<xsd:attribute name="key" type="xsd:string" use="required" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>