Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

YamlFileLoader.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 8.85 KiB


001  <?php
002   
003  /*
004   * This file is part of the Symfony package.
005   *
006   * (c) Fabien Potencier <fabien@symfony.com>
007   *
008   * For the full copyright and license information, please view the LICENSE
009   * file that was distributed with this source code.
010   */
011   
012  namespace Symfony\Component\Routing\Loader;
013   
014  use Symfony\Component\Config\Loader\FileLoader;
015  use Symfony\Component\Config\Resource\FileResource;
016  use Symfony\Component\Routing\Route;
017  use Symfony\Component\Routing\RouteCollection;
018  use Symfony\Component\Yaml\Exception\ParseException;
019  use Symfony\Component\Yaml\Parser as YamlParser;
020   
021  /**
022   * YamlFileLoader loads Yaml routing files.
023   *
024   * @author Fabien Potencier <fabien@symfony.com>
025   * @author Tobias Schultze <http://tobion.de>
026   */
027  class YamlFileLoader extends FileLoader
028  {
029      private static $availableKeys = [
030          'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller',
031      ];
032      private $yamlParser;
033   
034      /**
035       * Loads a Yaml file.
036       *
037       * @param string      $file A Yaml file path
038       * @param string|null $type The resource type
039       *
040       * @return RouteCollection A RouteCollection instance
041       *
042       * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
043       */
044      public function load($file, $type = null)
045      {
046          $path = $this->locator->locate($file);
047   
048          if (!stream_is_local($path)) {
049              throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
050          }
051   
052          if (!file_exists($path)) {
053              throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
054          }
055   
056          if (null === $this->yamlParser) {
057              $this->yamlParser = new YamlParser();
058          }
059   
060          $prevErrorHandler = set_error_handler(function ($level, $message, $script, $line) use ($file, &$prevErrorHandler) {
061              $message = \E_USER_DEPRECATED === $level ? preg_replace('/ on line \d+/', ' in "'.$file.'"$0', $message) : $message;
062   
063              return $prevErrorHandler ? $prevErrorHandler($level, $message, $script, $line) : false;
064          });
065   
066          try {
067              $parsedConfig = $this->yamlParser->parseFile($path);
068          } catch (ParseException $e) {
069              throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: ', $path).$e->getMessage(), 0, $e);
070          } finally {
071              restore_error_handler();
072          }
073   
074          $collection = new RouteCollection();
075          $collection->addResource(new FileResource($path));
076   
077          // empty file
078          if (null === $parsedConfig) {
079              return $collection;
080          }
081   
082          // not an array
083          if (!\is_array($parsedConfig)) {
084              throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
085          }
086   
087          foreach ($parsedConfig as $name => $config) {
088              $this->validate($config, $name, $path);
089   
090              if (isset($config['resource'])) {
091                  $this->parseImport($collection, $config, $path, $file);
092              } else {
093                  $this->parseRoute($collection, $name, $config, $path);
094              }
095          }
096   
097          return $collection;
098      }
099   
100      /**
101       * {@inheritdoc}
102       */
103      public function supports($resource, $type = null)
104      {
105          return \is_string($resource) && \in_array(pathinfo($resource, \PATHINFO_EXTENSION), ['yml', 'yaml'], true) && (!$type || 'yaml' === $type);
106      }
107   
108      /**
109       * Parses a route and adds it to the RouteCollection.
110       *
111       * @param RouteCollection $collection A RouteCollection instance
112       * @param string          $name       Route name
113       * @param array           $config     Route definition
114       * @param string          $path       Full path of the YAML file being processed
115       */
116      protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
117      {
118          $defaults = isset($config['defaults']) ? $config['defaults'] : [];
119          $requirements = isset($config['requirements']) ? $config['requirements'] : [];
120          $options = isset($config['options']) ? $config['options'] : [];
121          $host = isset($config['host']) ? $config['host'] : '';
122          $schemes = isset($config['schemes']) ? $config['schemes'] : [];
123          $methods = isset($config['methods']) ? $config['methods'] : [];
124          $condition = isset($config['condition']) ? $config['condition'] : null;
125   
126          if (isset($config['controller'])) {
127              $defaults['_controller'] = $config['controller'];
128          }
129   
130          $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
131   
132          $collection->add($name, $route);
133      }
134   
135      /**
136       * Parses an import and adds the routes in the resource to the RouteCollection.
137       *
138       * @param RouteCollection $collection A RouteCollection instance
139       * @param array           $config     Route definition
140       * @param string          $path       Full path of the YAML file being processed
141       * @param string          $file       Loaded file name
142       */
143      protected function parseImport(RouteCollection $collection, array $config, $path, $file)
144      {
145          $type = isset($config['type']) ? $config['type'] : null;
146          $prefix = isset($config['prefix']) ? $config['prefix'] : '';
147          $defaults = isset($config['defaults']) ? $config['defaults'] : [];
148          $requirements = isset($config['requirements']) ? $config['requirements'] : [];
149          $options = isset($config['options']) ? $config['options'] : [];
150          $host = isset($config['host']) ? $config['host'] : null;
151          $condition = isset($config['condition']) ? $config['condition'] : null;
152          $schemes = isset($config['schemes']) ? $config['schemes'] : null;
153          $methods = isset($config['methods']) ? $config['methods'] : null;
154   
155          if (isset($config['controller'])) {
156              $defaults['_controller'] = $config['controller'];
157          }
158   
159          $this->setCurrentDir(\dirname($path));
160   
161          $imported = $this->import($config['resource'], $type, false, $file) ?: [];
162   
163          if (!\is_array($imported)) {
164              $imported = [$imported];
165          }
166   
167          foreach ($imported as $subCollection) {
168              /* @var $subCollection RouteCollection */
169              $subCollection->addPrefix($prefix);
170              if (null !== $host) {
171                  $subCollection->setHost($host);
172              }
173              if (null !== $condition) {
174                  $subCollection->setCondition($condition);
175              }
176              if (null !== $schemes) {
177                  $subCollection->setSchemes($schemes);
178              }
179              if (null !== $methods) {
180                  $subCollection->setMethods($methods);
181              }
182              $subCollection->addDefaults($defaults);
183              $subCollection->addRequirements($requirements);
184              $subCollection->addOptions($options);
185   
186              $collection->addCollection($subCollection);
187          }
188      }
189   
190      /**
191       * Validates the route configuration.
192       *
193       * @param array  $config A resource config
194       * @param string $name   The config key
195       * @param string $path   The loaded file path
196       *
197       * @throws \InvalidArgumentException If one of the provided config keys is not supported,
198       *                                   something is missing or the combination is nonsense
199       */
200      protected function validate($config, $name, $path)
201      {
202          if (!\is_array($config)) {
203              throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
204          }
205          if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
206              throw new \InvalidArgumentException(sprintf('The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".', $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)));
207          }
208          if (isset($config['resource']) && isset($config['path'])) {
209              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));
210          }
211          if (!isset($config['resource']) && isset($config['type'])) {
212              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));
213          }
214          if (!isset($config['resource']) && !isset($config['path'])) {
215              throw new \InvalidArgumentException(sprintf('You must define a "path" for the route "%s" in file "%s".', $name, $path));
216          }
217          if (isset($config['controller']) && isset($config['defaults']['_controller'])) {
218              throw new \InvalidArgumentException(sprintf('The routing file "%s" must not specify both the "controller" key and the defaults key "_controller" for "%s".', $path, $name));
219          }
220      }
221  }
222