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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
class_loader.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb;
015
016 /**
017 * The class loader resolves class names to file system paths and loads them if
018 * necessary.
019 *
020 * Classes have to be of the form phpbb_(dir_)*(classpart_)*, so directory names
021 * must never contain underscores. Example: phpbb_dir_subdir_class_name is a
022 * valid class name, while phpbb_dir_sub_dir_class_name is not.
023 *
024 * If every part of the class name is a directory, the last directory name is
025 * also used as the filename, e.g. phpbb_dir would resolve to dir/dir.php.
026 */
027 class class_loader
028 {
029 private $namespace;
030 private $path;
031 private $php_ext;
032 private $cache;
033
034 /**
035 * A map of looked up class names to paths relative to $this->path.
036 * This map is stored in cache and looked up if the cache is available.
037 *
038 * @var array
039 */
040 private $cached_paths = array();
041
042 /**
043 * Creates a new \phpbb\class_loader, which loads files with the given
044 * file extension from the given path.
045 *
046 * @param string $namespace Required namespace for files to be loaded
047 * @param string $path Directory to load files from
048 * @param string $php_ext The file extension for PHP files
049 * @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface.
050 */
051 public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface $cache = null)
052 {
053 if ($namespace[0] !== '\\')
054 {
055 $namespace = '\\' . $namespace;
056 }
057
058 $this->namespace = $namespace;
059 $this->path = $path;
060 $this->php_ext = $php_ext;
061
062 $this->set_cache($cache);
063 }
064
065 /**
066 * Provide the class loader with a cache to store paths. If set to null, the
067 * the class loader will resolve paths by checking for the existance of every
068 * directory in the class name every time.
069 *
070 * @param \phpbb\cache\driver\driver_interface $cache An implementation of the phpBB cache interface.
071 */
072 public function set_cache(\phpbb\cache\driver\driver_interface $cache = null)
073 {
074 if ($cache)
075 {
076 $this->cached_paths = $cache->get('class_loader_' . str_replace('\\', '__', $this->namespace));
077
078 if ($this->cached_paths === false)
079 {
080 $this->cached_paths = array();
081 }
082 }
083
084 $this->cache = $cache;
085 }
086
087 /**
088 * Registers the class loader as an autoloader using SPL.
089 */
090 public function register()
091 {
092 spl_autoload_register(array($this, 'load_class'));
093 }
094
095 /**
096 * Removes the class loader from the SPL autoloader stack.
097 */
098 public function unregister()
099 {
100 spl_autoload_unregister(array($this, 'load_class'));
101 }
102
103 /**
104 * Resolves a phpBB class name to a relative path which can be included.
105 *
106 * @param string $class The class name to resolve, must be in the
107 * namespace the loader was constructed with.
108 * Has to begin with \
109 * @return string|bool A relative path to the file containing the
110 * class or false if looking it up failed.
111 */
112 public function resolve_path($class)
113 {
114 if (isset($this->cached_paths[$class]))
115 {
116 return $this->path . $this->cached_paths[$class] . '.' . $this->php_ext;
117 }
118
119 if (!preg_match('/^' . preg_quote($this->namespace, '/') . '[a-zA-Z0-9_\\\\]+$/', $class))
120 {
121 return false;
122 }
123
124 $relative_path = str_replace('\\', '/', substr($class, strlen($this->namespace)));
125
126 if (!file_exists($this->path . $relative_path . '.' . $this->php_ext))
127 {
128 return false;
129 }
130
131 if ($this->cache)
132 {
133 $this->cached_paths[$class] = $relative_path;
134 $this->cache->put('class_loader_' . str_replace('\\', '__', $this->namespace), $this->cached_paths);
135 }
136
137 return $this->path . $relative_path . '.' . $this->php_ext;
138 }
139
140 /**
141 * Resolves a class name to a path and then includes it.
142 *
143 * @param string $class The class name which is being loaded.
144 */
145 public function load_class($class)
146 {
147 // In general $class is not supposed to contain a leading backslash,
148 // but sometimes it does. See tickets PHP-50731 and HHVM-1840.
149 if ($class[0] !== '\\')
150 {
151 $class = '\\' . $class;
152 }
153
154 if (substr($class, 0, strlen($this->namespace)) === $this->namespace)
155 {
156 $path = $this->resolve_path($class);
157
158 if ($path)
159 {
160 require $path;
161 }
162 }
163 }
164 }
165