class YamlFileLoader

YamlFileLoader loads Yaml routing files.

@author Fabien Potencier <fabien@symfony.com>

@api

Hierarchy

  • class \Symfony\Component\Routing\Loader\YamlFileLoader extends \Symfony\Component\Config\Loader\FileLoader

Expanded class hierarchy of YamlFileLoader

1 file declares its use of YamlFileLoader
YamlFileLoaderTest.php in drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php

File

drupal/core/vendor/symfony/routing/Symfony/Component/Routing/Loader/YamlFileLoader.php, line 27

Namespace

Symfony\Component\Routing\Loader
View source
class YamlFileLoader extends FileLoader {
  private static $availableKeys = array(
    'type',
    'resource',
    'prefix',
    'pattern',
    'options',
    'defaults',
    'requirements',
  );

  /**
   * 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
   *
   * @api
   */
  public function load($file, $type = null) {
    $path = $this->locator
      ->locate($file);
    $config = Yaml::parse($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) {
      $config = $this
        ->normalizeRouteConfig($config);
      if (isset($config['resource'])) {
        $type = isset($config['type']) ? $config['type'] : null;
        $prefix = isset($config['prefix']) ? $config['prefix'] : null;
        $defaults = isset($config['defaults']) ? $config['defaults'] : array();
        $requirements = isset($config['requirements']) ? $config['requirements'] : array();
        $options = isset($config['options']) ? $config['options'] : array();
        $this
          ->setCurrentDir(dirname($path));
        $collection
          ->addCollection($this
          ->import($config['resource'], $type, false, $file), $prefix, $defaults, $requirements, $options);
      }
      else {
        $this
          ->parseRoute($collection, $name, $config, $path);
      }
    }
    return $collection;
  }

  /**
   * {@inheritdoc}
   *
   * @api
   */
  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);
  }

  /**
   * Normalize route configuration.
   *
   * @param array $config A resource config
   *
   * @return array
   *
   * @throws InvalidArgumentException if one of the provided config keys is not supported
   */
  private function normalizeRouteConfig(array $config) {
    foreach ($config as $key => $value) {
      if (!in_array($key, self::$availableKeys)) {
        throw new \InvalidArgumentException(sprintf('Yaml routing loader does not support given key: "%s". Expected one of the (%s).', $key, implode(', ', self::$availableKeys)));
      }
    }
    return $config;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
YamlFileLoader::$availableKeys private static property
YamlFileLoader::load public function Loads a Yaml file.
YamlFileLoader::normalizeRouteConfig private function Normalize route configuration.
YamlFileLoader::parseRoute protected function Parses a route and adds it to the RouteCollection.
YamlFileLoader::supports public function @api