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

XmlFileLoader.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 10.43 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\Config\Loader\FileLoader;
 018  use Symfony\Component\Config\Util\XmlUtils;
 019   
 020  /**
 021   * XmlFileLoader loads XML routing files.
 022   *
 023   * @author Fabien Potencier <fabien@symfony.com>
 024   * @author Tobias Schultze <http://tobion.de>
 025   */
 026  class XmlFileLoader extends FileLoader
 027  {
 028      const NAMESPACE_URI = 'http://symfony.com/schema/routing';
 029      const SCHEME_PATH = '/schema/routing/routing-1.0.xsd';
 030   
 031      /**
 032       * Loads an XML file.
 033       *
 034       * @param string      $file An XML file path
 035       * @param string|null $type The resource type
 036       *
 037       * @return RouteCollection A RouteCollection instance
 038       *
 039       * @throws \InvalidArgumentException When the file cannot be loaded or when the XML cannot be
 040       *                                   parsed because it does not validate against the scheme.
 041       */
 042      public function load($file, $type = null)
 043      {
 044          $path = $this->locator->locate($file);
 045   
 046          $xml = $this->loadFile($path);
 047   
 048          $collection = new RouteCollection();
 049          $collection->addResource(new FileResource($path));
 050   
 051          // process routes and imports
 052          foreach ($xml->documentElement->childNodes as $node) {
 053              if (!$node instanceof \DOMElement) {
 054                  continue;
 055              }
 056   
 057              $this->parseNode($collection, $node, $path, $file);
 058          }
 059   
 060          return $collection;
 061      }
 062   
 063      /**
 064       * Parses a node from a loaded XML file.
 065       *
 066       * @param RouteCollection $collection Collection to associate with the node
 067       * @param \DOMElement     $node       Element to parse
 068       * @param string          $path       Full path of the XML file being processed
 069       * @param string          $file       Loaded file name
 070       *
 071       * @throws \InvalidArgumentException When the XML is invalid
 072       */
 073      protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file)
 074      {
 075          if (self::NAMESPACE_URI !== $node->namespaceURI) {
 076              return;
 077          }
 078   
 079          switch ($node->localName) {
 080              case 'route':
 081                  $this->parseRoute($collection, $node, $path);
 082                  break;
 083              case 'import':
 084                  $this->parseImport($collection, $node, $path, $file);
 085                  break;
 086              default:
 087                  throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "route" or "import".', $node->localName, $path));
 088          }
 089      }
 090   
 091      /**
 092       * {@inheritdoc}
 093       */
 094      public function supports($resource, $type = null)
 095      {
 096          return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'xml' === $type);
 097      }
 098   
 099      /**
 100       * Parses a route and adds it to the RouteCollection.
 101       *
 102       * @param RouteCollection $collection RouteCollection instance
 103       * @param \DOMElement     $node       Element to parse that represents a Route
 104       * @param string          $path       Full path of the XML file being processed
 105       *
 106       * @throws \InvalidArgumentException When the XML is invalid
 107       */
 108      protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path)
 109      {
 110          if ('' === ($id = $node->getAttribute('id')) || (!$node->hasAttribute('pattern') && !$node->hasAttribute('path'))) {
 111              throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" must have an "id" and a "path" attribute.', $path));
 112          }
 113   
 114          if ($node->hasAttribute('pattern')) {
 115              if ($node->hasAttribute('path')) {
 116                  throw new \InvalidArgumentException(sprintf('The <route> element in file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path));
 117              }
 118   
 119              @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);
 120   
 121              $node->setAttribute('path', $node->getAttribute('pattern'));
 122              $node->removeAttribute('pattern');
 123          }
 124   
 125          $schemes = preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY);
 126          $methods = preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY);
 127   
 128          list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
 129   
 130          if (isset($requirements['_method'])) {
 131              if (0 === count($methods)) {
 132                  $methods = explode('|', $requirements['_method']);
 133              }
 134   
 135              unset($requirements['_method']);
 136              @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" attribute instead.', $id, $path), E_USER_DEPRECATED);
 137          }
 138   
 139          if (isset($requirements['_scheme'])) {
 140              if (0 === count($schemes)) {
 141                  $schemes = explode('|', $requirements['_scheme']);
 142              }
 143   
 144              unset($requirements['_scheme']);
 145              @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" attribute instead.', $id, $path), E_USER_DEPRECATED);
 146          }
 147   
 148          $route = new Route($node->getAttribute('path'), $defaults, $requirements, $options, $node->getAttribute('host'), $schemes, $methods, $condition);
 149          $collection->add($id, $route);
 150      }
 151   
 152      /**
 153       * Parses an import and adds the routes in the resource to the RouteCollection.
 154       *
 155       * @param RouteCollection $collection RouteCollection instance
 156       * @param \DOMElement     $node       Element to parse that represents a Route
 157       * @param string          $path       Full path of the XML file being processed
 158       * @param string          $file       Loaded file name
 159       *
 160       * @throws \InvalidArgumentException When the XML is invalid
 161       */
 162      protected function parseImport(RouteCollection $collection, \DOMElement $node, $path, $file)
 163      {
 164          if ('' === $resource = $node->getAttribute('resource')) {
 165              throw new \InvalidArgumentException(sprintf('The <import> element in file "%s" must have a "resource" attribute.', $path));
 166          }
 167   
 168          $type = $node->getAttribute('type');
 169          $prefix = $node->getAttribute('prefix');
 170          $host = $node->hasAttribute('host') ? $node->getAttribute('host') : null;
 171          $schemes = $node->hasAttribute('schemes') ? preg_split('/[\s,\|]++/', $node->getAttribute('schemes'), -1, PREG_SPLIT_NO_EMPTY) : null;
 172          $methods = $node->hasAttribute('methods') ? preg_split('/[\s,\|]++/', $node->getAttribute('methods'), -1, PREG_SPLIT_NO_EMPTY) : null;
 173   
 174          list($defaults, $requirements, $options, $condition) = $this->parseConfigs($node, $path);
 175   
 176          $this->setCurrentDir(dirname($path));
 177   
 178          $subCollection = $this->import($resource, ('' !== $type ? $type : null), false, $file);
 179          /* @var $subCollection RouteCollection */
 180          $subCollection->addPrefix($prefix);
 181          if (null !== $host) {
 182              $subCollection->setHost($host);
 183          }
 184          if (null !== $condition) {
 185              $subCollection->setCondition($condition);
 186          }
 187          if (null !== $schemes) {
 188              $subCollection->setSchemes($schemes);
 189          }
 190          if (null !== $methods) {
 191              $subCollection->setMethods($methods);
 192          }
 193          $subCollection->addDefaults($defaults);
 194          $subCollection->addRequirements($requirements);
 195          $subCollection->addOptions($options);
 196   
 197          $collection->addCollection($subCollection);
 198      }
 199   
 200      /**
 201       * Loads an XML file.
 202       *
 203       * @param string $file An XML file path
 204       *
 205       * @return \DOMDocument
 206       *
 207       * @throws \InvalidArgumentException When loading of XML file fails because of syntax errors
 208       *                                   or when the XML structure is not as expected by the scheme -
 209       *                                   see validate()
 210       */
 211      protected function loadFile($file)
 212      {
 213          return XmlUtils::loadFile($file, __DIR__.static::SCHEME_PATH);
 214      }
 215   
 216      /**
 217       * Parses the config elements (default, requirement, option).
 218       *
 219       * @param \DOMElement $node Element to parse that contains the configs
 220       * @param string      $path Full path of the XML file being processed
 221       *
 222       * @return array An array with the defaults as first item, requirements as second and options as third
 223       *
 224       * @throws \InvalidArgumentException When the XML is invalid
 225       */
 226      private function parseConfigs(\DOMElement $node, $path)
 227      {
 228          $defaults = array();
 229          $requirements = array();
 230          $options = array();
 231          $condition = null;
 232   
 233          foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
 234              switch ($n->localName) {
 235                  case 'default':
 236                      if ($this->isElementValueNull($n)) {
 237                          $defaults[$n->getAttribute('key')] = null;
 238                      } else {
 239                          $defaults[$n->getAttribute('key')] = trim($n->textContent);
 240                      }
 241   
 242                      break;
 243                  case 'requirement':
 244                      $requirements[$n->getAttribute('key')] = trim($n->textContent);
 245                      break;
 246                  case 'option':
 247                      $options[$n->getAttribute('key')] = trim($n->textContent);
 248                      break;
 249                  case 'condition':
 250                      $condition = trim($n->textContent);
 251                      break;
 252                  default:
 253                      throw new \InvalidArgumentException(sprintf('Unknown tag "%s" used in file "%s". Expected "default", "requirement" or "option".', $n->localName, $path));
 254              }
 255          }
 256   
 257          return array($defaults, $requirements, $options, $condition);
 258      }
 259   
 260      private function isElementValueNull(\DOMElement $element)
 261      {
 262          $namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';
 263   
 264          if (!$element->hasAttributeNS($namespaceUri, 'nil')) {
 265              return false;
 266          }
 267   
 268          return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil');
 269      }
 270  }
 271