public function YamlFileLoader::load

Same name in this branch

Loads a Yaml file.

@api

Parameters

string $file A Yaml file path:

string $type The resource type:

Return value

RouteCollection A RouteCollection instance

Throws

\InvalidArgumentException When route can't be parsed

File

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

Class

YamlFileLoader
YamlFileLoader loads Yaml routing files.

Namespace

Symfony\Component\Routing\Loader

Code

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;
}