2010-02-17 13:53:31 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Routing\Loader;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
use Symfony\Component\Routing\Resource\FileResource;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2010-02-17 13:53:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* YamlFileLoader loads Yaml routing files.
|
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:53:31 +00:00
|
|
|
*/
|
|
|
|
class YamlFileLoader extends FileLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Loads a Yaml file.
|
|
|
|
*
|
2010-12-06 06:48:44 +00:00
|
|
|
* @param string $file A Yaml file path
|
|
|
|
* @param string $type The resource type
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return RouteCollection A RouteCollection instance
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException When route can't be parsed
|
|
|
|
*/
|
2010-12-06 06:48:44 +00:00
|
|
|
public function load($file, $type = null)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$path = $this->findFile($file);
|
|
|
|
|
|
|
|
$config = $this->loadFile($path);
|
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->addResource(new FileResource($path));
|
|
|
|
|
2010-12-12 07:39:37 +00:00
|
|
|
// empty file
|
|
|
|
if (null === $config) {
|
|
|
|
$config = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// not an array
|
|
|
|
if (!is_array($config)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file));
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($config as $name => $config) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($config['resource'])) {
|
2010-12-06 06:48:44 +00:00
|
|
|
$type = isset($config['type']) ? $config['type'] : null;
|
2010-07-17 16:18:30 +01:00
|
|
|
$prefix = isset($config['prefix']) ? $config['prefix'] : null;
|
|
|
|
$this->currentDir = dirname($path);
|
2010-12-06 06:48:44 +00:00
|
|
|
$collection->addCollection($this->import($config['resource'], $type), $prefix);
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (isset($config['pattern'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->parseRoute($collection, $name, $config, $path);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('Unable to parse the "%s" route.', $name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $collection;
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-07-17 16:18:30 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this class supports the given resource.
|
|
|
|
*
|
2010-12-06 06:48:44 +00:00
|
|
|
* @param mixed $resource A resource
|
|
|
|
* @param string $type The resource type
|
2010-07-17 16:18:30 +01:00
|
|
|
*
|
2010-12-06 06:48:44 +00:00
|
|
|
* @return boolean True if this class supports the given resource, false otherwise
|
2010-07-17 16:18:30 +01:00
|
|
|
*/
|
2010-12-06 06:48:44 +00:00
|
|
|
public function supports($resource, $type = null)
|
2010-07-17 16:18:30 +01:00
|
|
|
{
|
2010-12-06 06:48:44 +00:00
|
|
|
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type);
|
2010-07-17 16:18:30 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-12-06 06:48:44 +00:00
|
|
|
* Parses a route and adds it to the RouteCollection.
|
|
|
|
*
|
|
|
|
* @param RouteCollection $collection A RouteCollection instance
|
|
|
|
* @param string $name Route name
|
|
|
|
* @param array $config Route definition
|
|
|
|
* @param string $file A Yaml file path
|
|
|
|
*
|
2010-05-06 12:25:53 +01:00
|
|
|
* @throws \InvalidArgumentException When config pattern is not defined for the given route
|
|
|
|
*/
|
|
|
|
protected function parseRoute(RouteCollection $collection, $name, $config, $file)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$defaults = isset($config['defaults']) ? $config['defaults'] : array();
|
|
|
|
$requirements = isset($config['requirements']) ? $config['requirements'] : array();
|
|
|
|
$options = isset($config['options']) ? $config['options'] : array();
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!isset($config['pattern'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name));
|
|
|
|
}
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$route = new Route($config['pattern'], $defaults, $requirements, $options);
|
2010-02-17 13:53:31 +00:00
|
|
|
|
2010-11-23 08:42:19 +00:00
|
|
|
$collection->add($name, $route);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 06:48:44 +00:00
|
|
|
/**
|
|
|
|
* Loads a Yaml file.
|
|
|
|
*
|
|
|
|
* @param string $file A Yaml file path
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function loadFile($file)
|
2010-02-17 13:53:31 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return Yaml::load($file);
|
2010-02-17 13:53:31 +00:00
|
|
|
}
|
|
|
|
}
|