Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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: 09.10.2024, 12:56 - Dateigröße: 9.40 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\Routing\RouteCollection;
015  use Symfony\Component\Routing\Route;
016  use Symfony\Component\Config\Resource\FileResource;
017  use Symfony\Component\Yaml\Exception\ParseException;
018  use Symfony\Component\Yaml\Parser as YamlParser;
019  use Symfony\Component\Config\Loader\FileLoader;
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 = array(
030          'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition',
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          try {
061              $parsedConfig = $this->yamlParser->parse(file_get_contents($path));
062          } catch (ParseException $e) {
063              throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e);
064          }
065   
066          $collection = new RouteCollection();
067          $collection->addResource(new FileResource($path));
068   
069          // empty file
070          if (null === $parsedConfig) {
071              return $collection;
072          }
073   
074          // not an array
075          if (!is_array($parsedConfig)) {
076              throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
077          }
078   
079          foreach ($parsedConfig as $name => $config) {
080              if (isset($config['pattern'])) {
081                  if (isset($config['path'])) {
082                      throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
083                  }
084   
085                  @trigger_error(sprintf('The "pattern" option in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', $path), E_USER_DEPRECATED);
086   
087                  $config['path'] = $config['pattern'];
088                  unset($config['pattern']);
089              }
090   
091              $this->validate($config, $name, $path);
092   
093              if (isset($config['resource'])) {
094                  $this->parseImport($collection, $config, $path, $file);
095              } else {
096                  $this->parseRoute($collection, $name, $config, $path);
097              }
098          }
099   
100          return $collection;
101      }
102   
103      /**
104       * {@inheritdoc}
105       */
106      public function supports($resource, $type = null)
107      {
108          return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type);
109      }
110   
111      /**
112       * Parses a route and adds it to the RouteCollection.
113       *
114       * @param RouteCollection $collection A RouteCollection instance
115       * @param string          $name       Route name
116       * @param array           $config     Route definition
117       * @param string          $path       Full path of the YAML file being processed
118       */
119      protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
120      {
121          $defaults = isset($config['defaults']) ? $config['defaults'] : array();
122          $requirements = isset($config['requirements']) ? $config['requirements'] : array();
123          $options = isset($config['options']) ? $config['options'] : array();
124          $host = isset($config['host']) ? $config['host'] : '';
125          $schemes = isset($config['schemes']) ? $config['schemes'] : array();
126          $methods = isset($config['methods']) ? $config['methods'] : array();
127          $condition = isset($config['condition']) ? $config['condition'] : null;
128   
129          if (isset($requirements['_method'])) {
130              if (0 === count($methods)) {
131                  $methods = explode('|', $requirements['_method']);
132              }
133   
134              unset($requirements['_method']);
135              @trigger_error(sprintf('The "_method" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "methods" option instead.', $name, $path), E_USER_DEPRECATED);
136          }
137   
138          if (isset($requirements['_scheme'])) {
139              if (0 === count($schemes)) {
140                  $schemes = explode('|', $requirements['_scheme']);
141              }
142   
143              unset($requirements['_scheme']);
144              @trigger_error(sprintf('The "_scheme" requirement of route "%s" in file "%s" is deprecated since version 2.2 and will be removed in 3.0. Use the "schemes" option instead.', $name, $path), E_USER_DEPRECATED);
145          }
146   
147          $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods, $condition);
148   
149          $collection->add($name, $route);
150      }
151   
152      /**
153       * Parses an import and adds the routes in the resource to the RouteCollection.
154       *
155       * @param RouteCollection $collection A RouteCollection instance
156       * @param array           $config     Route definition
157       * @param string          $path       Full path of the YAML file being processed
158       * @param string          $file       Loaded file name
159       */
160      protected function parseImport(RouteCollection $collection, array $config, $path, $file)
161      {
162          $type = isset($config['type']) ? $config['type'] : null;
163          $prefix = isset($config['prefix']) ? $config['prefix'] : '';
164          $defaults = isset($config['defaults']) ? $config['defaults'] : array();
165          $requirements = isset($config['requirements']) ? $config['requirements'] : array();
166          $options = isset($config['options']) ? $config['options'] : array();
167          $host = isset($config['host']) ? $config['host'] : null;
168          $condition = isset($config['condition']) ? $config['condition'] : null;
169          $schemes = isset($config['schemes']) ? $config['schemes'] : null;
170          $methods = isset($config['methods']) ? $config['methods'] : null;
171   
172          $this->setCurrentDir(dirname($path));
173   
174          $subCollection = $this->import($config['resource'], $type, false, $file);
175          /* @var $subCollection RouteCollection */
176          $subCollection->addPrefix($prefix);
177          if (null !== $host) {
178              $subCollection->setHost($host);
179          }
180          if (null !== $condition) {
181              $subCollection->setCondition($condition);
182          }
183          if (null !== $schemes) {
184              $subCollection->setSchemes($schemes);
185          }
186          if (null !== $methods) {
187              $subCollection->setMethods($methods);
188          }
189          $subCollection->addDefaults($defaults);
190          $subCollection->addRequirements($requirements);
191          $subCollection->addOptions($options);
192   
193          $collection->addCollection($subCollection);
194      }
195   
196      /**
197       * Validates the route configuration.
198       *
199       * @param array  $config A resource config
200       * @param string $name   The config key
201       * @param string $path   The loaded file path
202       *
203       * @throws \InvalidArgumentException If one of the provided config keys is not supported,
204       *                                   something is missing or the combination is nonsense
205       */
206      protected function validate($config, $name, $path)
207      {
208          if (!is_array($config)) {
209              throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
210          }
211          if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
212              throw new \InvalidArgumentException(sprintf(
213                  'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
214                  $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
215              ));
216          }
217          if (isset($config['resource']) && isset($config['path'])) {
218              throw new \InvalidArgumentException(sprintf(
219                  '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.',
220                  $path, $name
221              ));
222          }
223          if (!isset($config['resource']) && isset($config['type'])) {
224              throw new \InvalidArgumentException(sprintf(
225                  'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
226                  $name, $path
227              ));
228          }
229          if (!isset($config['resource']) && !isset($config['path'])) {
230              throw new \InvalidArgumentException(sprintf(
231                  'You must define a "path" for the route "%s" in file "%s".',
232                  $name, $path
233              ));
234          }
235      }
236  }
237