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

Filesystem.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 5.77 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of Twig.
 005   *
 006   * (c) 2009 Fabien Potencier
 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  /**
 013   * Loads template from the filesystem.
 014   *
 015   * @author Fabien Potencier <fabien@symfony.com>
 016   */
 017  class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
 018  {
 019      /** Identifier of the main namespace. */
 020      const MAIN_NAMESPACE = '__main__';
 021   
 022      protected $paths;
 023      protected $cache;
 024   
 025      /**
 026       * Constructor.
 027       *
 028       * @param string|array $paths A path or an array of paths where to look for templates
 029       */
 030      public function __construct($paths = array())
 031      {
 032          if ($paths) {
 033              $this->setPaths($paths);
 034          }
 035      }
 036   
 037      /**
 038       * Returns the paths to the templates.
 039       *
 040       * @param string $namespace A path namespace
 041       *
 042       * @return array The array of paths where to look for templates
 043       */
 044      public function getPaths($namespace = self::MAIN_NAMESPACE)
 045      {
 046          return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
 047      }
 048   
 049      /**
 050       * Returns the path namespaces.
 051       *
 052       * The main namespace is always defined.
 053       *
 054       * @return array The array of defined namespaces
 055       */
 056      public function getNamespaces()
 057      {
 058          return array_keys($this->paths);
 059      }
 060   
 061      /**
 062       * Sets the paths where templates are stored.
 063       *
 064       * @param string|array $paths     A path or an array of paths where to look for templates
 065       * @param string       $namespace A path namespace
 066       */
 067      public function setPaths($paths, $namespace = self::MAIN_NAMESPACE)
 068      {
 069          if (!is_array($paths)) {
 070              $paths = array($paths);
 071          }
 072   
 073          $this->paths[$namespace] = array();
 074          foreach ($paths as $path) {
 075              $this->addPath($path, $namespace);
 076          }
 077      }
 078   
 079      /**
 080       * Adds a path where templates are stored.
 081       *
 082       * @param string $path      A path where to look for templates
 083       * @param string $namespace A path name
 084       *
 085       * @throws Twig_Error_Loader
 086       */
 087      public function addPath($path, $namespace = self::MAIN_NAMESPACE)
 088      {
 089          // invalidate the cache
 090          $this->cache = array();
 091   
 092          if (!is_dir($path)) {
 093              throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
 094          }
 095   
 096          $this->paths[$namespace][] = rtrim($path, '/\\');
 097      }
 098   
 099      /**
 100       * Prepends a path where templates are stored.
 101       *
 102       * @param string $path      A path where to look for templates
 103       * @param string $namespace A path name
 104       *
 105       * @throws Twig_Error_Loader
 106       */
 107      public function prependPath($path, $namespace = self::MAIN_NAMESPACE)
 108      {
 109          // invalidate the cache
 110          $this->cache = array();
 111   
 112          if (!is_dir($path)) {
 113              throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
 114          }
 115   
 116          $path = rtrim($path, '/\\');
 117   
 118          if (!isset($this->paths[$namespace])) {
 119              $this->paths[$namespace][] = $path;
 120          } else {
 121              array_unshift($this->paths[$namespace], $path);
 122          }
 123      }
 124   
 125      /**
 126       * {@inheritdoc}
 127       */
 128      public function getSource($name)
 129      {
 130          return file_get_contents($this->findTemplate($name));
 131      }
 132   
 133      /**
 134       * {@inheritdoc}
 135       */
 136      public function getCacheKey($name)
 137      {
 138          return $this->findTemplate($name);
 139      }
 140   
 141      /**
 142       * {@inheritdoc}
 143       */
 144      public function exists($name)
 145      {
 146          $name = (string) $name;
 147          if (isset($this->cache[$name])) {
 148              return true;
 149          }
 150   
 151          try {
 152              $this->findTemplate($name);
 153   
 154              return true;
 155          } catch (Twig_Error_Loader $exception) {
 156              return false;
 157          }
 158      }
 159   
 160      /**
 161       * {@inheritdoc}
 162       */
 163      public function isFresh($name, $time)
 164      {
 165          return filemtime($this->findTemplate($name)) <= $time;
 166      }
 167   
 168      protected function findTemplate($name)
 169      {
 170          $name = (string) $name;
 171   
 172          // normalize name
 173          $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
 174   
 175          if (isset($this->cache[$name])) {
 176              return $this->cache[$name];
 177          }
 178   
 179          $this->validateName($name);
 180   
 181          $namespace = self::MAIN_NAMESPACE;
 182          if (isset($name[0]) && '@' == $name[0]) {
 183              if (false === $pos = strpos($name, '/')) {
 184                  throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
 185              }
 186   
 187              $namespace = substr($name, 1, $pos - 1);
 188   
 189              $name = substr($name, $pos + 1);
 190          }
 191   
 192          if (!isset($this->paths[$namespace])) {
 193              throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
 194          }
 195   
 196          foreach ($this->paths[$namespace] as $path) {
 197              if (is_file($path.'/'.$name)) {
 198                  return $this->cache[$name] = $path.'/'.$name;
 199              }
 200          }
 201   
 202          throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
 203      }
 204   
 205      protected function validateName($name)
 206      {
 207          if (false !== strpos($name, "\0")) {
 208              throw new Twig_Error_Loader('A template name cannot contain NUL bytes.');
 209          }
 210   
 211          $name = ltrim($name, '/');
 212          $parts = explode('/', $name);
 213          $level = 0;
 214          foreach ($parts as $part) {
 215              if ('..' === $part) {
 216                  --$level;
 217              } elseif ('.' !== $part) {
 218                  ++$level;
 219              }
 220   
 221              if ($level < 0) {
 222                  throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name));
 223              }
 224          }
 225      }
 226  }
 227