* * 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\Routing\RouteCollection; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Resource\FileResource; use Symfony\Component\Yaml\Yaml; /** * YamlFileLoader loads Yaml routing files. * * @author Fabien Potencier */ class YamlFileLoader extends FileLoader { /** * Loads a Yaml file. * * @param string $file A Yaml file path * @param string $type The resource type * * @return RouteCollection A RouteCollection instance * * @throws \InvalidArgumentException When route can't be parsed */ public function load($file, $type = null) { $path = $this->findFile($file); $config = $this->loadFile($path); $collection = new RouteCollection(); $collection->addResource(new FileResource($path)); // 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)); } foreach ($config as $name => $config) { if (isset($config['resource'])) { $type = isset($config['type']) ? $config['type'] : null; $prefix = isset($config['prefix']) ? $config['prefix'] : null; $this->currentDir = dirname($path); $collection->addCollection($this->import($config['resource'], $type), $prefix); } elseif (isset($config['pattern'])) { $this->parseRoute($collection, $name, $config, $path); } else { throw new \InvalidArgumentException(sprintf('Unable to parse the "%s" route.', $name)); } } return $collection; } /** * Returns true if this class supports the given resource. * * @param mixed $resource A resource * @param string $type The resource type * * @return boolean True if this class supports the given resource, false otherwise */ public function supports($resource, $type = null) { return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type); } /** * 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 * * @throws \InvalidArgumentException When config pattern is not defined for the given route */ protected function parseRoute(RouteCollection $collection, $name, $config, $file) { $defaults = isset($config['defaults']) ? $config['defaults'] : array(); $requirements = isset($config['requirements']) ? $config['requirements'] : array(); $options = isset($config['options']) ? $config['options'] : array(); if (!isset($config['pattern'])) { throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name)); } $route = new Route($config['pattern'], $defaults, $requirements, $options); $collection->add($name, $route); } /** * Loads a Yaml file. * * @param string $file A Yaml file path * * @return array */ protected function loadFile($file) { return Yaml::load($file); } }