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

ClassLoader.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 10.77 KiB


001  <?php
002   
003  /*
004   * This file is part of Composer.
005   *
006   * (c) Nils Adermann <naderman@naderman.de>
007   *     Jordi Boggiano <j.boggiano@seld.be>
008   *
009   * For the full copyright and license information, please view the LICENSE
010   * file that was distributed with this source code.
011   */
012   
013  namespace Composer\Autoload;
014   
015  /**
016   * ClassLoader implements a PSR-0 class loader
017   *
018   * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
019   *
020   *     $loader = new \Composer\Autoload\ClassLoader();
021   *
022   *     // register classes with namespaces
023   *     $loader->add('Symfony\Component', __DIR__.'/component');
024   *     $loader->add('Symfony',           __DIR__.'/framework');
025   *
026   *     // activate the autoloader
027   *     $loader->register();
028   *
029   *     // to enable searching the include path (eg. for PEAR packages)
030   *     $loader->setUseIncludePath(true);
031   *
032   * In this example, if you try to use a class in the Symfony\Component
033   * namespace or one of its children (Symfony\Component\Console for instance),
034   * the autoloader will first look for the class under the component/
035   * directory, and it will then fallback to the framework/ directory if not
036   * found before giving up.
037   *
038   * This class is loosely based on the Symfony UniversalClassLoader.
039   *
040   * @author Fabien Potencier <fabien@symfony.com>
041   * @author Jordi Boggiano <j.boggiano@seld.be>
042   */
043  class ClassLoader
044  {
045      // PSR-4
046      private $prefixLengthsPsr4 = array();
047      private $prefixDirsPsr4 = array();
048      private $fallbackDirsPsr4 = array();
049   
050      // PSR-0
051      private $prefixesPsr0 = array();
052      private $fallbackDirsPsr0 = array();
053   
054      private $useIncludePath = false;
055      private $classMap = array();
056   
057      public function getPrefixes()
058      {
059          return call_user_func_array('array_merge', $this->prefixesPsr0);
060      }
061   
062      public function getPrefixesPsr4()
063      {
064          return $this->prefixDirsPsr4;
065      }
066   
067      public function getFallbackDirs()
068      {
069          return $this->fallbackDirsPsr0;
070      }
071   
072      public function getFallbackDirsPsr4()
073      {
074          return $this->fallbackDirsPsr4;
075      }
076   
077      public function getClassMap()
078      {
079          return $this->classMap;
080      }
081   
082      /**
083       * @param array $classMap Class to filename map
084       */
085      public function addClassMap(array $classMap)
086      {
087          if ($this->classMap) {
088              $this->classMap = array_merge($this->classMap, $classMap);
089          } else {
090              $this->classMap = $classMap;
091          }
092      }
093   
094      /**
095       * Registers a set of PSR-0 directories for a given prefix, either
096       * appending or prepending to the ones previously set for this prefix.
097       *
098       * @param string       $prefix  The prefix
099       * @param array|string $paths   The PSR-0 root directories
100       * @param bool         $prepend Whether to prepend the directories
101       */
102      public function add($prefix, $paths, $prepend = false)
103      {
104          if (!$prefix) {
105              if ($prepend) {
106                  $this->fallbackDirsPsr0 = array_merge(
107                      (array) $paths,
108                      $this->fallbackDirsPsr0
109                  );
110              } else {
111                  $this->fallbackDirsPsr0 = array_merge(
112                      $this->fallbackDirsPsr0,
113                      (array) $paths
114                  );
115              }
116   
117              return;
118          }
119   
120          $first = $prefix[0];
121          if (!isset($this->prefixesPsr0[$first][$prefix])) {
122              $this->prefixesPsr0[$first][$prefix] = (array) $paths;
123   
124              return;
125          }
126          if ($prepend) {
127              $this->prefixesPsr0[$first][$prefix] = array_merge(
128                  (array) $paths,
129                  $this->prefixesPsr0[$first][$prefix]
130              );
131          } else {
132              $this->prefixesPsr0[$first][$prefix] = array_merge(
133                  $this->prefixesPsr0[$first][$prefix],
134                  (array) $paths
135              );
136          }
137      }
138   
139      /**
140       * Registers a set of PSR-4 directories for a given namespace, either
141       * appending or prepending to the ones previously set for this namespace.
142       *
143       * @param string       $prefix  The prefix/namespace, with trailing '\\'
144       * @param array|string $paths   The PSR-0 base directories
145       * @param bool         $prepend Whether to prepend the directories
146       */
147      public function addPsr4($prefix, $paths, $prepend = false)
148      {
149          if (!$prefix) {
150              // Register directories for the root namespace.
151              if ($prepend) {
152                  $this->fallbackDirsPsr4 = array_merge(
153                      (array) $paths,
154                      $this->fallbackDirsPsr4
155                  );
156              } else {
157                  $this->fallbackDirsPsr4 = array_merge(
158                      $this->fallbackDirsPsr4,
159                      (array) $paths
160                  );
161              }
162          } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
163              // Register directories for a new namespace.
164              $length = strlen($prefix);
165              if ('\\' !== $prefix[$length - 1]) {
166                  throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
167              }
168              $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
169              $this->prefixDirsPsr4[$prefix] = (array) $paths;
170          } elseif ($prepend) {
171              // Prepend directories for an already registered namespace.
172              $this->prefixDirsPsr4[$prefix] = array_merge(
173                  (array) $paths,
174                  $this->prefixDirsPsr4[$prefix]
175              );
176          } else {
177              // Append directories for an already registered namespace.
178              $this->prefixDirsPsr4[$prefix] = array_merge(
179                  $this->prefixDirsPsr4[$prefix],
180                  (array) $paths
181              );
182          }
183      }
184   
185      /**
186       * Registers a set of PSR-0 directories for a given prefix,
187       * replacing any others previously set for this prefix.
188       *
189       * @param string       $prefix The prefix
190       * @param array|string $paths  The PSR-0 base directories
191       */
192      public function set($prefix, $paths)
193      {
194          if (!$prefix) {
195              $this->fallbackDirsPsr0 = (array) $paths;
196          } else {
197              $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
198          }
199      }
200   
201      /**
202       * Registers a set of PSR-4 directories for a given namespace,
203       * replacing any others previously set for this namespace.
204       *
205       * @param string       $prefix  The prefix/namespace, with trailing '\\'
206       * @param array|string $paths   The PSR-4 base directories
207       */
208      public function setPsr4($prefix, $paths) {
209          if (!$prefix) {
210              $this->fallbackDirsPsr4 = (array) $paths;
211          } else {
212              $length = strlen($prefix);
213              if ('\\' !== $prefix[$length - 1]) {
214                  throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
215              }
216              $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
217              $this->prefixDirsPsr4[$prefix] = (array) $paths;
218          }
219      }
220   
221      /**
222       * Turns on searching the include path for class files.
223       *
224       * @param bool $useIncludePath
225       */
226      public function setUseIncludePath($useIncludePath)
227      {
228          $this->useIncludePath = $useIncludePath;
229      }
230   
231      /**
232       * Can be used to check if the autoloader uses the include path to check
233       * for classes.
234       *
235       * @return bool
236       */
237      public function getUseIncludePath()
238      {
239          return $this->useIncludePath;
240      }
241   
242      /**
243       * Registers this instance as an autoloader.
244       *
245       * @param bool $prepend Whether to prepend the autoloader or not
246       */
247      public function register($prepend = false)
248      {
249          spl_autoload_register(array($this, 'loadClass'), true, $prepend);
250      }
251   
252      /**
253       * Unregisters this instance as an autoloader.
254       */
255      public function unregister()
256      {
257          spl_autoload_unregister(array($this, 'loadClass'));
258      }
259   
260      /**
261       * Loads the given class or interface.
262       *
263       * @param  string    $class The name of the class
264       * @return bool|null True if loaded, null otherwise
265       */
266      public function loadClass($class)
267      {
268          if ($file = $this->findFile($class)) {
269              include $file;
270   
271              return true;
272          }
273      }
274   
275      /**
276       * Finds the path to the file where the class is defined.
277       *
278       * @param string $class The name of the class
279       *
280       * @return string|false The path if found, false otherwise
281       */
282      public function findFile($class)
283      {
284          // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
285          if ('\\' == $class[0]) {
286              $class = substr($class, 1);
287          }
288   
289          // class map lookup
290          if (isset($this->classMap[$class])) {
291              return $this->classMap[$class];
292          }
293   
294          // PSR-4 lookup
295          $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';
296   
297          $first = $class[0];
298          if (isset($this->prefixLengthsPsr4[$first])) {
299              foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
300                  if (0 === strpos($class, $prefix)) {
301                      foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
302                          if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
303                              return $file;
304                          }
305                      }
306                  }
307              }
308          }
309   
310          // PSR-4 fallback dirs
311          foreach ($this->fallbackDirsPsr4 as $dir) {
312              if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
313                  return $file;
314              }
315          }
316   
317          // PSR-0 lookup
318          if (false !== $pos = strrpos($class, '\\')) {
319              // namespaced class name
320              $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
321                  . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
322          } else {
323              // PEAR-like class name
324              $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . '.php';
325          }
326   
327          if (isset($this->prefixesPsr0[$first])) {
328              foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
329                  if (0 === strpos($class, $prefix)) {
330                      foreach ($dirs as $dir) {
331                          if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
332                              return $file;
333                          }
334                      }
335                  }
336              }
337          }
338   
339          // PSR-0 fallback dirs
340          foreach ($this->fallbackDirsPsr0 as $dir) {
341              if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
342                  return $file;
343              }
344          }
345   
346          // PSR-0 include paths.
347          if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
348              return $file;
349          }
350   
351          // Remember that this class does not exist.
352          return $this->classMap[$class] = false;
353      }
354  }
355