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

Router.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 8.54 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;
013   
014  use Symfony\Component\Config\Loader\LoaderInterface;
015  use Symfony\Component\Config\ConfigCache;
016  use Psr\Log\LoggerInterface;
017  use Symfony\Component\Routing\Generator\ConfigurableRequirementsInterface;
018  use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
019  use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
020   
021  /**
022   * The Router class is an example of the integration of all pieces of the
023   * routing system for easier use.
024   *
025   * @author Fabien Potencier <fabien@symfony.com>
026   */
027  class Router implements RouterInterface
028  {
029      /**
030       * @var UrlMatcherInterface|null
031       */
032      protected $matcher;
033   
034      /**
035       * @var UrlGeneratorInterface|null
036       */
037      protected $generator;
038   
039      /**
040       * @var RequestContext
041       */
042      protected $context;
043   
044      /**
045       * @var LoaderInterface
046       */
047      protected $loader;
048   
049      /**
050       * @var RouteCollection|null
051       */
052      protected $collection;
053   
054      /**
055       * @var mixed
056       */
057      protected $resource;
058   
059      /**
060       * @var array
061       */
062      protected $options = array();
063   
064      /**
065       * @var LoggerInterface|null
066       */
067      protected $logger;
068   
069      /**
070       * Constructor.
071       *
072       * @param LoaderInterface $loader   A LoaderInterface instance
073       * @param mixed           $resource The main resource to load
074       * @param array           $options  An array of options
075       * @param RequestContext  $context  The context
076       * @param LoggerInterface $logger   A logger instance
077       */
078      public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)
079      {
080          $this->loader = $loader;
081          $this->resource = $resource;
082          $this->logger = $logger;
083          $this->context = null === $context ? new RequestContext() : $context;
084          $this->setOptions($options);
085      }
086   
087      /**
088       * Sets options.
089       *
090       * Available options:
091       *
092       *   * cache_dir:     The cache directory (or null to disable caching)
093       *   * debug:         Whether to enable debugging or not (false by default)
094       *   * resource_type: Type hint for the main resource (optional)
095       *
096       * @param array $options An array of options
097       *
098       * @throws \InvalidArgumentException When unsupported option is provided
099       */
100      public function setOptions(array $options)
101      {
102          $this->options = array(
103              'cache_dir'              => null,
104              'debug'                  => false,
105              'generator_class'        => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
106              'generator_base_class'   => 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
107              'generator_dumper_class' => 'Symfony\\Component\\Routing\\Generator\\Dumper\\PhpGeneratorDumper',
108              'generator_cache_class'  => 'ProjectUrlGenerator',
109              'matcher_class'          => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
110              'matcher_base_class'     => 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
111              'matcher_dumper_class'   => 'Symfony\\Component\\Routing\\Matcher\\Dumper\\PhpMatcherDumper',
112              'matcher_cache_class'    => 'ProjectUrlMatcher',
113              'resource_type'          => null,
114              'strict_requirements'    => true,
115          );
116   
117          // check option names and live merge, if errors are encountered Exception will be thrown
118          $invalid = array();
119          foreach ($options as $key => $value) {
120              if (array_key_exists($key, $this->options)) {
121                  $this->options[$key] = $value;
122              } else {
123                  $invalid[] = $key;
124              }
125          }
126   
127          if ($invalid) {
128              throw new \InvalidArgumentException(sprintf('The Router does not support the following options: "%s".', implode('", "', $invalid)));
129          }
130      }
131   
132      /**
133       * Sets an option.
134       *
135       * @param string $key   The key
136       * @param mixed  $value The value
137       *
138       * @throws \InvalidArgumentException
139       */
140      public function setOption($key, $value)
141      {
142          if (!array_key_exists($key, $this->options)) {
143              throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
144          }
145   
146          $this->options[$key] = $value;
147      }
148   
149      /**
150       * Gets an option value.
151       *
152       * @param string $key The key
153       *
154       * @return mixed The value
155       *
156       * @throws \InvalidArgumentException
157       */
158      public function getOption($key)
159      {
160          if (!array_key_exists($key, $this->options)) {
161              throw new \InvalidArgumentException(sprintf('The Router does not support the "%s" option.', $key));
162          }
163   
164          return $this->options[$key];
165      }
166   
167      /**
168       * {@inheritdoc}
169       */
170      public function getRouteCollection()
171      {
172          if (null === $this->collection) {
173              $this->collection = $this->loader->load($this->resource, $this->options['resource_type']);
174          }
175   
176          return $this->collection;
177      }
178   
179      /**
180       * {@inheritdoc}
181       */
182      public function setContext(RequestContext $context)
183      {
184          $this->context = $context;
185   
186          if (null !== $this->matcher) {
187              $this->getMatcher()->setContext($context);
188          }
189          if (null !== $this->generator) {
190              $this->getGenerator()->setContext($context);
191          }
192      }
193   
194      /**
195       * {@inheritdoc}
196       */
197      public function getContext()
198      {
199          return $this->context;
200      }
201   
202      /**
203       * {@inheritdoc}
204       */
205      public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
206      {
207          return $this->getGenerator()->generate($name, $parameters, $referenceType);
208      }
209   
210      /**
211       * {@inheritdoc}
212       */
213      public function match($pathinfo)
214      {
215          return $this->getMatcher()->match($pathinfo);
216      }
217   
218      /**
219       * Gets the UrlMatcher instance associated with this Router.
220       *
221       * @return UrlMatcherInterface A UrlMatcherInterface instance
222       */
223      public function getMatcher()
224      {
225          if (null !== $this->matcher) {
226              return $this->matcher;
227          }
228   
229          if (null === $this->options['cache_dir'] || null === $this->options['matcher_cache_class']) {
230              return $this->matcher = new $this->options['matcher_class']($this->getRouteCollection(), $this->context);
231          }
232   
233          $class = $this->options['matcher_cache_class'];
234          $cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
235          if (!$cache->isFresh()) {
236              $dumper = new $this->options['matcher_dumper_class']($this->getRouteCollection());
237   
238              $options = array(
239                  'class'      => $class,
240                  'base_class' => $this->options['matcher_base_class'],
241              );
242   
243              $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
244          }
245   
246          require_once $cache;
247   
248          return $this->matcher = new $class($this->context);
249      }
250   
251      /**
252       * Gets the UrlGenerator instance associated with this Router.
253       *
254       * @return UrlGeneratorInterface A UrlGeneratorInterface instance
255       */
256      public function getGenerator()
257      {
258          if (null !== $this->generator) {
259              return $this->generator;
260          }
261   
262          if (null === $this->options['cache_dir'] || null === $this->options['generator_cache_class']) {
263              $this->generator = new $this->options['generator_class']($this->getRouteCollection(), $this->context, $this->logger);
264          } else {
265              $class = $this->options['generator_cache_class'];
266              $cache = new ConfigCache($this->options['cache_dir'].'/'.$class.'.php', $this->options['debug']);
267              if (!$cache->isFresh()) {
268                  $dumper = new $this->options['generator_dumper_class']($this->getRouteCollection());
269   
270                  $options = array(
271                      'class'      => $class,
272                      'base_class' => $this->options['generator_base_class'],
273                  );
274   
275                  $cache->write($dumper->dump($options), $this->getRouteCollection()->getResources());
276              }
277   
278              require_once $cache;
279   
280              $this->generator = new $class($this->context, $this->logger);
281          }
282   
283          if ($this->generator instanceof ConfigurableRequirementsInterface) {
284              $this->generator->setStrictRequirements($this->options['strict_requirements']);
285          }
286   
287          return $this->generator;
288      }
289  }
290