Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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:58 - Dateigröße: 7.80 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\Parser as YamlParser;
018  use Symfony\Component\Config\Loader\FileLoader;
019   
020  /**
021   * YamlFileLoader loads Yaml routing files.
022   *
023   * @author Fabien Potencier <fabien@symfony.com>
024   * @author Tobias Schultze <http://tobion.de>
025   *
026   * @api
027   */
028  class YamlFileLoader extends FileLoader
029  {
030      private static $availableKeys = array(
031          'resource', 'type', 'prefix', 'pattern', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options',
032      );
033      private $yamlParser;
034   
035      /**
036       * Loads a Yaml file.
037       *
038       * @param string      $file A Yaml file path
039       * @param string|null $type The resource type
040       *
041       * @return RouteCollection A RouteCollection instance
042       *
043       * @throws \InvalidArgumentException When a route can't be parsed because YAML is invalid
044       *
045       * @api
046       */
047      public function load($file, $type = null)
048      {
049          $path = $this->locator->locate($file);
050   
051          if (!stream_is_local($path)) {
052              throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path));
053          }
054   
055          if (!file_exists($path)) {
056              throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path));
057          }
058   
059          if (null === $this->yamlParser) {
060              $this->yamlParser = new YamlParser();
061          }
062   
063          $config = $this->yamlParser->parse(file_get_contents($path));
064   
065          $collection = new RouteCollection();
066          $collection->addResource(new FileResource($path));
067   
068          // empty file
069          if (null === $config) {
070              return $collection;
071          }
072   
073          // not an array
074          if (!is_array($config)) {
075              throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path));
076          }
077   
078          foreach ($config as $name => $config) {
079              if (isset($config['pattern'])) {
080                  if (isset($config['path'])) {
081                      throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
082                  }
083   
084                  $config['path'] = $config['pattern'];
085                  unset($config['pattern']);
086              }
087   
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       * @api
104       */
105      public function supports($resource, $type = null)
106      {
107          return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type);
108      }
109   
110      /**
111       * Parses a route and adds it to the RouteCollection.
112       *
113       * @param RouteCollection $collection A RouteCollection instance
114       * @param string          $name       Route name
115       * @param array           $config     Route definition
116       * @param string          $path       Full path of the YAML file being processed
117       */
118      protected function parseRoute(RouteCollection $collection, $name, array $config, $path)
119      {
120          $defaults = isset($config['defaults']) ? $config['defaults'] : array();
121          $requirements = isset($config['requirements']) ? $config['requirements'] : array();
122          $options = isset($config['options']) ? $config['options'] : array();
123          $host = isset($config['host']) ? $config['host'] : '';
124          $schemes = isset($config['schemes']) ? $config['schemes'] : array();
125          $methods = isset($config['methods']) ? $config['methods'] : array();
126   
127          $route = new Route($config['path'], $defaults, $requirements, $options, $host, $schemes, $methods);
128   
129          $collection->add($name, $route);
130      }
131   
132      /**
133       * Parses an import and adds the routes in the resource to the RouteCollection.
134       *
135       * @param RouteCollection $collection A RouteCollection instance
136       * @param array           $config     Route definition
137       * @param string          $path       Full path of the YAML file being processed
138       * @param string          $file       Loaded file name
139       */
140      protected function parseImport(RouteCollection $collection, array $config, $path, $file)
141      {
142          $type = isset($config['type']) ? $config['type'] : null;
143          $prefix = isset($config['prefix']) ? $config['prefix'] : '';
144          $defaults = isset($config['defaults']) ? $config['defaults'] : array();
145          $requirements = isset($config['requirements']) ? $config['requirements'] : array();
146          $options = isset($config['options']) ? $config['options'] : array();
147          $host = isset($config['host']) ? $config['host'] : null;
148          $schemes = isset($config['schemes']) ? $config['schemes'] : null;
149          $methods = isset($config['methods']) ? $config['methods'] : null;
150   
151          $this->setCurrentDir(dirname($path));
152   
153          $subCollection = $this->import($config['resource'], $type, false, $file);
154          /* @var $subCollection RouteCollection */
155          $subCollection->addPrefix($prefix);
156          if (null !== $host) {
157              $subCollection->setHost($host);
158          }
159          if (null !== $schemes) {
160              $subCollection->setSchemes($schemes);
161          }
162          if (null !== $methods) {
163              $subCollection->setMethods($methods);
164          }
165          $subCollection->addDefaults($defaults);
166          $subCollection->addRequirements($requirements);
167          $subCollection->addOptions($options);
168   
169          $collection->addCollection($subCollection);
170      }
171   
172      /**
173       * Validates the route configuration.
174       *
175       * @param array  $config A resource config
176       * @param string $name   The config key
177       * @param string $path   The loaded file path
178       *
179       * @throws \InvalidArgumentException If one of the provided config keys is not supported,
180       *                                   something is missing or the combination is nonsense
181       */
182      protected function validate($config, $name, $path)
183      {
184          if (!is_array($config)) {
185              throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path));
186          }
187          if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) {
188              throw new \InvalidArgumentException(sprintf(
189                  'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".',
190                  $path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys)
191              ));
192          }
193          if (isset($config['resource']) && isset($config['path'])) {
194              throw new \InvalidArgumentException(sprintf(
195                  '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.',
196                  $path, $name
197              ));
198          }
199          if (!isset($config['resource']) && isset($config['type'])) {
200              throw new \InvalidArgumentException(sprintf(
201                  'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.',
202                  $name, $path
203              ));
204          }
205          if (!isset($config['resource']) && !isset($config['path'])) {
206              throw new \InvalidArgumentException(sprintf(
207                  'You must define a "path" for the route "%s" in file "%s".',
208                  $name, $path
209              ));
210          }
211      }
212  }
213