123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?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\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 <fabien.potencier@symfony-project.com>
- */
- 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->locator->locate($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);
- $file = $this->locator->locate($config['resource'], $this->currentDir);
- $collection->addCollection($this->import($file, $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);
- }
- }
|