Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
28 / 30 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| class_loader | |
93.33% |
28 / 30 |
|
66.67% |
4 / 6 |
16.08 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| set_cache | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| register | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unregister | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolve_path | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
5.02 | |||
| load_class | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb; |
| 15 | |
| 16 | /** |
| 17 | * The class loader resolves class names to file system paths and loads them if |
| 18 | * necessary. |
| 19 | * |
| 20 | * Classes have to be of the form phpbb_(dir_)*(classpart_)*, so directory names |
| 21 | * must never contain underscores. Example: phpbb_dir_subdir_class_name is a |
| 22 | * valid class name, while phpbb_dir_sub_dir_class_name is not. |
| 23 | * |
| 24 | * If every part of the class name is a directory, the last directory name is |
| 25 | * also used as the filename, e.g. phpbb_dir would resolve to dir/dir.php. |
| 26 | */ |
| 27 | class class_loader |
| 28 | { |
| 29 | private $namespace; |
| 30 | private $path; |
| 31 | private $php_ext; |
| 32 | private $cache; |
| 33 | |
| 34 | /** |
| 35 | * A map of looked up class names to paths relative to $this->path. |
| 36 | * This map is stored in cache and looked up if the cache is available. |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | private $cached_paths = array(); |
| 41 | |
| 42 | /** |
| 43 | * Creates a new \phpbb\class_loader, which loads files with the given |
| 44 | * file extension from the given path. |
| 45 | * |
| 46 | * @param string $namespace Required namespace for files to be loaded |
| 47 | * @param string $path Directory to load files from |
| 48 | * @param string $php_ext The file extension for PHP files |
| 49 | * @param \phpbb\cache\driver\driver_interface|null $cache An implementation of the phpBB cache interface. |
| 50 | */ |
| 51 | public function __construct($namespace, $path, $php_ext = 'php', \phpbb\cache\driver\driver_interface|null $cache = null) |
| 52 | { |
| 53 | if ($namespace[0] !== '\\') |
| 54 | { |
| 55 | $namespace = '\\' . $namespace; |
| 56 | } |
| 57 | |
| 58 | $this->namespace = $namespace; |
| 59 | $this->path = $path; |
| 60 | $this->php_ext = $php_ext; |
| 61 | |
| 62 | $this->set_cache($cache); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Provide the class loader with a cache to store paths. If set to null, the |
| 67 | * the class loader will resolve paths by checking for the existence of every |
| 68 | * directory in the class name every time. |
| 69 | * |
| 70 | * @param \phpbb\cache\driver\driver_interface|null $cache An implementation of the phpBB cache interface. |
| 71 | */ |
| 72 | public function set_cache(\phpbb\cache\driver\driver_interface|null $cache = null) |
| 73 | { |
| 74 | if ($cache) |
| 75 | { |
| 76 | $this->cached_paths = $cache->get('class_loader_' . str_replace('\\', '__', $this->namespace)); |
| 77 | |
| 78 | if ($this->cached_paths === false) |
| 79 | { |
| 80 | $this->cached_paths = array(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | $this->cache = $cache; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Registers the class loader as an autoloader using SPL. |
| 89 | */ |
| 90 | public function register() |
| 91 | { |
| 92 | spl_autoload_register(array($this, 'load_class')); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Removes the class loader from the SPL autoloader stack. |
| 97 | */ |
| 98 | public function unregister() |
| 99 | { |
| 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 | } |