vendor/symfony/routing/Loader/YamlFileLoader.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Routing\Loader;
  11. use Symfony\Component\Config\Loader\FileLoader;
  12. use Symfony\Component\Config\Resource\FileResource;
  13. use Symfony\Component\Routing\Loader\Configurator\Traits\HostTrait;
  14. use Symfony\Component\Routing\Loader\Configurator\Traits\LocalizedRouteTrait;
  15. use Symfony\Component\Routing\Loader\Configurator\Traits\PrefixTrait;
  16. use Symfony\Component\Routing\RouteCollection;
  17. use Symfony\Component\Yaml\Exception\ParseException;
  18. use Symfony\Component\Yaml\Parser as YamlParser;
  19. use Symfony\Component\Yaml\Yaml;
  20. /**
  21.  * YamlFileLoader loads Yaml routing files.
  22.  *
  23.  * @author Fabien Potencier <fabien@symfony.com>
  24.  * @author Tobias Schultze <http://tobion.de>
  25.  */
  26. class YamlFileLoader extends FileLoader
  27. {
  28.     use HostTrait;
  29.     use LocalizedRouteTrait;
  30.     use PrefixTrait;
  31.     private const AVAILABLE_KEYS = [
  32.         'resource''type''prefix''path''host''schemes''methods''defaults''requirements''options''condition''controller''name_prefix''trailing_slash_on_root''locale''format''utf8''exclude''stateless',
  33.     ];
  34.     private $yamlParser;
  35.     /**
  36.      * Loads a Yaml file.
  37.      *
  38.      * @param string      $file A Yaml file path
  39.      * @param string|null $type The resource type
  40.      *
  41.      * @return RouteCollection A RouteCollection instance
  42.      *
  43.      * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
  44.      */
  45.     public function load($filestring $type null)
  46.     {
  47.         $path $this->locator->locate($file);
  48.         if (!stream_is_local($path)) {
  49.             throw new \InvalidArgumentException(sprintf('This is not a local file "%s".'$path));
  50.         }
  51.         if (!file_exists($path)) {
  52.             throw new \InvalidArgumentException(sprintf('File "%s" not found.'$path));
  53.         }
  54.         if (null === $this->yamlParser) {
  55.             $this->yamlParser = new YamlParser();
  56.         }
  57.         try {
  58.             $parsedConfig $this->yamlParser->parseFile($pathYaml::PARSE_CONSTANT);
  59.         } catch (ParseException $e) {
  60.             throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: '$path).$e->getMessage(), 0$e);
  61.         }
  62.         $collection = new RouteCollection();
  63.         $collection->addResource(new FileResource($path));
  64.         // empty file
  65.         if (null === $parsedConfig) {
  66.             return $collection;
  67.         }
  68.         // not an array
  69.         if (!\is_array($parsedConfig)) {
  70.             throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.'$path));
  71.         }
  72.         foreach ($parsedConfig as $name => $config) {
  73.             $this->validate($config$name$path);
  74.             if (isset($config['resource'])) {
  75.                 $this->parseImport($collection$config$path$file);
  76.             } else {
  77.                 $this->parseRoute($collection$name$config$path);
  78.             }
  79.         }
  80.         return $collection;
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function supports($resourcestring $type null)
  86.     {
  87.         return \is_string($resource) && \in_array(pathinfo($resource\PATHINFO_EXTENSION), ['yml''yaml'], true) && (!$type || 'yaml' === $type);
  88.     }
  89.     /**
  90.      * Parses a route and adds it to the RouteCollection.
  91.      */
  92.     protected function parseRoute(RouteCollection $collectionstring $name, array $configstring $path)
  93.     {
  94.         $defaults $config['defaults'] ?? [];
  95.         $requirements $config['requirements'] ?? [];
  96.         $options $config['options'] ?? [];
  97.         foreach ($requirements as $placeholder => $requirement) {
  98.             if (\is_int($placeholder)) {
  99.                 throw new \InvalidArgumentException(sprintf('A placeholder name must be a string (%d given). Did you forget to specify the placeholder key for the requirement "%s" of route "%s" in "%s"?'$placeholder$requirement$name$path));
  100.             }
  101.         }
  102.         if (isset($config['controller'])) {
  103.             $defaults['_controller'] = $config['controller'];
  104.         }
  105.         if (isset($config['locale'])) {
  106.             $defaults['_locale'] = $config['locale'];
  107.         }
  108.         if (isset($config['format'])) {
  109.             $defaults['_format'] = $config['format'];
  110.         }
  111.         if (isset($config['utf8'])) {
  112.             $options['utf8'] = $config['utf8'];
  113.         }
  114.         if (isset($config['stateless'])) {
  115.             $defaults['_stateless'] = $config['stateless'];
  116.         }
  117.         $routes $this->createLocalizedRoute($collection$name$config['path']);
  118.         $routes->addDefaults($defaults);
  119.         $routes->addRequirements($requirements);
  120.         $routes->addOptions($options);
  121.         $routes->setSchemes($config['schemes'] ?? []);
  122.         $routes->setMethods($config['methods'] ?? []);
  123.         $routes->setCondition($config['condition'] ?? null);
  124.         if (isset($config['host'])) {
  125.             $this->addHost($routes$config['host']);
  126.         }
  127.     }
  128.     /**
  129.      * Parses an import and adds the routes in the resource to the RouteCollection.
  130.      */
  131.     protected function parseImport(RouteCollection $collection, array $configstring $pathstring $file)
  132.     {
  133.         $type $config['type'] ?? null;
  134.         $prefix $config['prefix'] ?? '';
  135.         $defaults $config['defaults'] ?? [];
  136.         $requirements $config['requirements'] ?? [];
  137.         $options $config['options'] ?? [];
  138.         $host $config['host'] ?? null;
  139.         $condition $config['condition'] ?? null;
  140.         $schemes $config['schemes'] ?? null;
  141.         $methods $config['methods'] ?? null;
  142.         $trailingSlashOnRoot $config['trailing_slash_on_root'] ?? true;
  143.         $namePrefix $config['name_prefix'] ?? null;
  144.         $exclude $config['exclude'] ?? null;
  145.         if (isset($config['controller'])) {
  146.             $defaults['_controller'] = $config['controller'];
  147.         }
  148.         if (isset($config['locale'])) {
  149.             $defaults['_locale'] = $config['locale'];
  150.         }
  151.         if (isset($config['format'])) {
  152.             $defaults['_format'] = $config['format'];
  153.         }
  154.         if (isset($config['utf8'])) {
  155.             $options['utf8'] = $config['utf8'];
  156.         }
  157.         if (isset($config['stateless'])) {
  158.             $defaults['_stateless'] = $config['stateless'];
  159.         }
  160.         $this->setCurrentDir(\dirname($path));
  161.         /** @var RouteCollection[] $imported */
  162.         $imported $this->import($config['resource'], $typefalse$file$exclude) ?: [];
  163.         if (!\is_array($imported)) {
  164.             $imported = [$imported];
  165.         }
  166.         foreach ($imported as $subCollection) {
  167.             $this->addPrefix($subCollection$prefix$trailingSlashOnRoot);
  168.             if (null !== $host) {
  169.                 $this->addHost($subCollection$host);
  170.             }
  171.             if (null !== $condition) {
  172.                 $subCollection->setCondition($condition);
  173.             }
  174.             if (null !== $schemes) {
  175.                 $subCollection->setSchemes($schemes);
  176.             }
  177.             if (null !== $methods) {
  178.                 $subCollection->setMethods($methods);
  179.             }
  180.             if (null !== $namePrefix) {
  181.                 $subCollection->addNamePrefix($namePrefix);
  182.             }
  183.             $subCollection->addDefaults($defaults);
  184.             $subCollection->addRequirements($requirements);
  185.             $subCollection->addOptions($options);
  186.             $collection->addCollection($subCollection);
  187.         }
  188.     }
  189.     /**
  190.      * Validates the route configuration.
  191.      *
  192.      * @param array  $config A resource config
  193.      * @param string $name   The config key
  194.      * @param string $path   The loaded file path
  195.      *
  196.      * @throws \InvalidArgumentException If one of the provided config keys is not supported,
  197.      *                                   something is missing or the combination is nonsense
  198.      */
  199.     protected function validate($configstring $namestring $path)
  200.     {
  201.         if (!\is_array($config)) {
  202.             throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.'$name$path));
  203.         }
  204.         if ($extraKeys array_diff(array_keys($config), self::AVAILABLE_KEYS)) {
  205.             throw new \InvalidArgumentException(sprintf('The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".'$path$nameimplode('", "'$extraKeys), implode('", "'self::AVAILABLE_KEYS)));
  206.         }
  207.         if (isset($config['resource']) && isset($config['path'])) {
  208.             throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "resource" key and the "path" key for "%s". Choose between an import and a route definition.'$path$name));
  209.         }
  210.         if (!isset($config['resource']) && isset($config['type'])) {
  211.             throw new \InvalidArgumentException(sprintf('The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.'$name$path));
  212.         }
  213.         if (!isset($config['resource']) && !isset($config['path'])) {
  214.             throw new \InvalidArgumentException(sprintf('You must define a "path" for the route "%s" in file "%s".'$name$path));
  215.         }
  216.         if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
  217.             throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".'$path$name));
  218.         }
  219.         if (isset($config['stateless']) && isset($config['defaults']['_stateless'])) {
  220.             throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "stateless" key and the defaults key "_stateless" for "%s".'$path$name));
  221.         }
  222.     }
  223. }