Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. YamlFileLoader.php

function YamlFileLoader::load

Same name in this branch
  1. 11.1.x vendor/symfony/dependency-injection/Loader/YamlFileLoader.php \Symfony\Component\DependencyInjection\Loader\YamlFileLoader::load()
  2. 11.1.x core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php \Drupal\Core\DependencyInjection\YamlFileLoader::load()

Throws

\InvalidArgumentException When a route can't be parsed because YAML is invalid

File

vendor/symfony/routing/Loader/YamlFileLoader.php, line 44

Class

YamlFileLoader
YamlFileLoader loads Yaml routing files.

Namespace

Symfony\Component\Routing\Loader

Code

public function load(mixed $file, ?string $type = null) : RouteCollection {
    $path = $this->locator
        ->locate($file);
    if (!stream_is_local($path)) {
        throw new \InvalidArgumentException(\sprintf('This is not a local file "%s".', $path));
    }
    if (!file_exists($path)) {
        throw new \InvalidArgumentException(\sprintf('File "%s" not found.', $path));
    }
    $this->yamlParser ??= new YamlParser();
    try {
        $parsedConfig = $this->yamlParser
            ->parseFile($path, Yaml::PARSE_CONSTANT);
    } catch (ParseException $e) {
        throw new \InvalidArgumentException(\sprintf('The file "%s" does not contain valid YAML: ', $path) . $e->getMessage(), 0, $e);
    }
    $collection = new RouteCollection();
    $collection->addResource(new FileResource($path));
    // empty file
    if (null === $parsedConfig) {
        return $collection;
    }
    // not an array
    if (!\is_array($parsedConfig)) {
        throw new \InvalidArgumentException(\sprintf('The file "%s" must contain a YAML array.', $path));
    }
    foreach ($parsedConfig as $name => $config) {
        if (str_starts_with($name, 'when@')) {
            if (!$this->env || 'when@' . $this->env !== $name) {
                continue;
            }
            foreach ($config as $name => $config) {
                $this->validate($config, $name . '" when "@' . $this->env, $path);
                if (isset($config['resource'])) {
                    $this->parseImport($collection, $config, $path, $file);
                }
                else {
                    $this->parseRoute($collection, $name, $config, $path);
                }
            }
            continue;
        }
        $this->validate($config, $name, $path);
        if (isset($config['resource'])) {
            $this->parseImport($collection, $config, $path, $file);
        }
        else {
            $this->parseRoute($collection, $name, $config, $path);
        }
    }
    return $collection;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal