Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
49.09% |
27 / 55 |
|
27.27% |
3 / 11 |
CRAP | |
0.00% |
0 / 1 |
AutoLoader | |
55.10% |
27 / 49 |
|
27.27% |
3 / 11 |
65.81 | |
0.00% |
0 / 1 |
registerNamespaces | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
registerClasses | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
loadFile | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
loadFiles | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
find | |
95.65% |
22 / 23 |
|
0.00% |
0 / 1 |
9 | |||
autoload | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
assertTesting | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getClassFiles | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getNamespaceDirectories | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getState | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
restoreState | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This defines autoloading handler for whole MediaWiki framework |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | */ |
22 | |
23 | // NO_AUTOLOAD -- file scope code, can't load self |
24 | |
25 | /** |
26 | * Locations of core classes |
27 | * Extension classes are specified with $wgAutoloadClasses |
28 | */ |
29 | require_once __DIR__ . '/../autoload.php'; |
30 | |
31 | class AutoLoader { |
32 | |
33 | /** |
34 | * A mapping of namespace => file path for MediaWiki core. |
35 | * The namespaces should follow the PSR-4 standard for autoloading |
36 | * |
37 | * @see <https://www.php-fig.org/psr/psr-4/> |
38 | * @internal Only public for usage in AutoloadGenerator |
39 | * @phpcs-require-sorted-array |
40 | */ |
41 | public const CORE_NAMESPACES = [ |
42 | 'MediaWiki\\' => __DIR__ . '/', |
43 | 'MediaWiki\\Actions\\' => __DIR__ . '/actions/', |
44 | 'MediaWiki\\Api\\' => __DIR__ . '/api/', |
45 | 'MediaWiki\\Auth\\' => __DIR__ . '/auth/', |
46 | 'MediaWiki\\Block\\' => __DIR__ . '/block/', |
47 | 'MediaWiki\\Cache\\' => __DIR__ . '/cache/', |
48 | 'MediaWiki\\ChangeTags\\' => __DIR__ . '/changetags/', |
49 | 'MediaWiki\\Composer\\' => __DIR__ . '/composer/', |
50 | 'MediaWiki\\Config\\' => __DIR__ . '/config/', |
51 | 'MediaWiki\\Content\\' => __DIR__ . '/content/', |
52 | 'MediaWiki\\DB\\' => __DIR__ . '/db/', |
53 | 'MediaWiki\\Deferred\\' => __DIR__ . '/deferred/', |
54 | 'MediaWiki\\Deferred\\LinksUpdate\\' => __DIR__ . '/deferred/LinksUpdate/', |
55 | 'MediaWiki\\Diff\\' => __DIR__ . '/diff/', |
56 | 'MediaWiki\\EditPage\\' => __DIR__ . '/editpage/', |
57 | 'MediaWiki\\Edit\\' => __DIR__ . '/edit/', |
58 | 'MediaWiki\\FileBackend\\LockManager\\' => __DIR__ . '/filebackend/lockmanager/', |
59 | 'MediaWiki\\Http\\' => __DIR__ . '/http/', |
60 | 'MediaWiki\\Installer\\' => __DIR__ . '/installer/', |
61 | 'MediaWiki\\Interwiki\\' => __DIR__ . '/interwiki/', |
62 | 'MediaWiki\\JobQueue\\' => __DIR__ . '/jobqueue/', |
63 | 'MediaWiki\\Json\\' => __DIR__ . '/json/', |
64 | 'MediaWiki\\Languages\\Data\\' => __DIR__ . '/languages/data/', |
65 | 'MediaWiki\\Linker\\' => __DIR__ . '/linker/', |
66 | 'MediaWiki\\Logger\\' => __DIR__ . '/debug/logger/', |
67 | 'MediaWiki\\Logger\\Monolog\\' => __DIR__ . '/debug/logger/monolog/', |
68 | 'MediaWiki\\Mail\\' => __DIR__ . '/mail/', |
69 | 'MediaWiki\\Page\\' => __DIR__ . '/page/', |
70 | 'MediaWiki\\Parser\\' => __DIR__ . '/parser/', |
71 | 'MediaWiki\\PoolCounter\\' => __DIR__ . '/poolcounter/', |
72 | 'MediaWiki\\Preferences\\' => __DIR__ . '/preferences/', |
73 | 'MediaWiki\\Search\\' => __DIR__ . '/search/', |
74 | 'MediaWiki\\Search\\SearchWidgets\\' => __DIR__ . '/search/searchwidgets/', |
75 | 'MediaWiki\\Session\\' => __DIR__ . '/session/', |
76 | 'MediaWiki\\Shell\\' => __DIR__ . '/shell/', |
77 | 'MediaWiki\\Site\\' => __DIR__ . '/site/', |
78 | 'MediaWiki\\Sparql\\' => __DIR__ . '/sparql/', |
79 | 'MediaWiki\\SpecialPage\\' => __DIR__ . '/specialpage/', |
80 | 'MediaWiki\\Specials\\Contribute\\' => __DIR__ . '/specials/Contribute', |
81 | 'MediaWiki\\Tidy\\' => __DIR__ . '/tidy/', |
82 | 'MediaWiki\\User\\' => __DIR__ . '/user/', |
83 | 'MediaWiki\\Utils\\' => __DIR__ . '/utils/', |
84 | 'MediaWiki\\Widget\\' => __DIR__ . '/widget/', |
85 | 'Wikimedia\\' => __DIR__ . '/libs/', |
86 | 'Wikimedia\\Composer\\' => __DIR__ . '/libs/composer/', |
87 | 'Wikimedia\\Http\\' => __DIR__ . '/libs/http/', |
88 | 'Wikimedia\\Rdbms\\Platform\\' => __DIR__ . '/libs/rdbms/platform/', |
89 | 'Wikimedia\\UUID\\' => __DIR__ . '/libs/uuid/', |
90 | ]; |
91 | |
92 | /** |
93 | * @var string[] Namespace (ends with \) => Path (ends with /) |
94 | */ |
95 | private static $psr4Namespaces = self::CORE_NAMESPACES; |
96 | |
97 | /** |
98 | * @var string[] Class => File |
99 | */ |
100 | private static $classFiles = []; |
101 | |
102 | /** |
103 | * Register a directory to load the classes of a given namespace from, |
104 | * per PSR4. |
105 | * |
106 | * @see <https://www.php-fig.org/psr/psr-4/> |
107 | * @since 1.39 |
108 | * @param string[] $dirs a map of namespace (ends with \) to path (ends with /) |
109 | */ |
110 | public static function registerNamespaces( array $dirs ): void { |
111 | self::$psr4Namespaces += $dirs; |
112 | } |
113 | |
114 | /** |
115 | * Register a file to load the given class from. |
116 | * @since 1.39 |
117 | * |
118 | * @param string[] $files a map of qualified class names to file names |
119 | */ |
120 | public static function registerClasses( array $files ): void { |
121 | self::$classFiles += $files; |
122 | } |
123 | |
124 | /** |
125 | * Load a file that declares classes, functions, or constants. |
126 | * The file will be loaded immediately using require_once in function scope. |
127 | * |
128 | * @note The file to be loaded MUST NOT set global variables or otherwise |
129 | * affect the global state. It MAY however use conditionals to determine |
130 | * what to declare and how, e.g. to provide polyfills. |
131 | * |
132 | * @note The file to be loaded MUST NOT assume that MediaWiki has been |
133 | * initialized. In particular, it MUST NOT access configuration variables |
134 | * or MediaWikiServices. |
135 | * |
136 | * @since 1.39 |
137 | * |
138 | * @param string $file the path of the file to load. |
139 | */ |
140 | public static function loadFile( string $file ): void { |
141 | require_once $file; |
142 | } |
143 | |
144 | /** |
145 | * Batch version of loadFile() |
146 | * |
147 | * @see loadFile() |
148 | * |
149 | * @since 1.39 |
150 | * |
151 | * @param string[] $files the paths of the files to load. |
152 | */ |
153 | public static function loadFiles( array $files ): void { |
154 | foreach ( $files as $f ) { |
155 | self::loadFile( $f ); |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * Find the file containing the given class. |
161 | * |
162 | * @param string $className Name of class we're looking for. |
163 | * @return string|null The path containing the class, not null if not found |
164 | */ |
165 | public static function find( $className ): ?string { |
166 | global $wgAutoloadLocalClasses, $wgAutoloadClasses; |
167 | |
168 | // NOTE: $wgAutoloadClasses is supported for compatibility with old-style extension |
169 | // registration files. |
170 | |
171 | $filename = $wgAutoloadLocalClasses[$className] ?? |
172 | self::$classFiles[$className] ?? |
173 | $wgAutoloadClasses[$className] ?? |
174 | false; |
175 | |
176 | if ( !$filename && strpos( $className, '\\' ) !== false ) { |
177 | // This class is namespaced, so look in the namespace map |
178 | $prefix = $className; |
179 | while ( ( $pos = strrpos( $prefix, '\\' ) ) !== false ) { |
180 | // Check to see if this namespace prefix is in the map |
181 | $prefix = substr( $className, 0, $pos + 1 ); |
182 | if ( isset( self::$psr4Namespaces[$prefix] ) ) { |
183 | $relativeClass = substr( $className, $pos + 1 ); |
184 | // Build the expected filename, and see if it exists |
185 | $file = self::$psr4Namespaces[$prefix] . |
186 | '/' . |
187 | strtr( $relativeClass, '\\', '/' ) . |
188 | '.php'; |
189 | if ( is_file( $file ) ) { |
190 | $filename = $file; |
191 | break; |
192 | } |
193 | } |
194 | |
195 | // Remove trailing separator for next iteration |
196 | $prefix = rtrim( $prefix, '\\' ); |
197 | } |
198 | } |
199 | |
200 | if ( !$filename ) { |
201 | // Class not found; let the next autoloader try to find it |
202 | return null; |
203 | } |
204 | |
205 | // Make an absolute path, this improves performance by avoiding some stat calls |
206 | // Optimisation: use string offset access instead of substr |
207 | if ( $filename[0] !== '/' && $filename[1] !== ':' ) { |
208 | $filename = __DIR__ . '/../' . $filename; |
209 | } |
210 | |
211 | return $filename; |
212 | } |
213 | |
214 | /** |
215 | * autoload - take a class name and attempt to load it |
216 | * |
217 | * @param string $className Name of class we're looking for. |
218 | */ |
219 | public static function autoload( $className ) { |
220 | $filename = self::find( $className ); |
221 | |
222 | if ( $filename !== null ) { |
223 | require $filename; |
224 | } |
225 | } |
226 | |
227 | ///// Methods used during testing ////////////////////////////////////////////// |
228 | private static function assertTesting( $method ) { |
229 | if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
230 | throw new LogicException( "$method is not supported outside phpunit tests!" ); |
231 | } |
232 | } |
233 | |
234 | /** |
235 | * Returns a map of class names to file paths for testing. |
236 | * @note Will throw if called outside of phpunit tests! |
237 | * @return string[] |
238 | */ |
239 | public static function getClassFiles(): array { |
240 | global $wgAutoloadLocalClasses, $wgAutoloadClasses; |
241 | |
242 | self::assertTesting( __METHOD__ ); |
243 | |
244 | // NOTE: ensure the order of preference is the same as used by find(). |
245 | return array_merge( |
246 | $wgAutoloadClasses, |
247 | self::$classFiles, |
248 | $wgAutoloadLocalClasses |
249 | ); |
250 | } |
251 | |
252 | /** |
253 | * Returns a map of namespace names to directories, per PSR4. |
254 | * @note Will throw if called outside of phpunit tests! |
255 | * @return string[] |
256 | */ |
257 | public static function getNamespaceDirectories(): array { |
258 | self::assertTesting( __METHOD__ ); |
259 | return self::$psr4Namespaces; |
260 | } |
261 | |
262 | /** |
263 | * Returns an array representing the internal state of Autoloader, |
264 | * so it can be remembered and later restored during testing. |
265 | * @internal |
266 | * @note Will throw if called outside of phpunit tests! |
267 | * @return array |
268 | */ |
269 | public static function getState(): array { |
270 | self::assertTesting( __METHOD__ ); |
271 | return [ |
272 | 'classFiles' => self::$classFiles, |
273 | 'psr4Namespaces' => self::$psr4Namespaces, |
274 | ]; |
275 | } |
276 | |
277 | /** |
278 | * Returns an array representing the internal state of Autoloader, |
279 | * so it can be remembered and later restored during testing. |
280 | * @internal |
281 | * @note Will throw if called outside of phpunit tests! |
282 | * |
283 | * @param array $state A state array returned by getState(). |
284 | */ |
285 | public static function restoreState( $state ): void { |
286 | self::assertTesting( __METHOD__ ); |
287 | |
288 | self::$classFiles = $state['classFiles']; |
289 | self::$psr4Namespaces = $state['psr4Namespaces']; |
290 | } |
291 | |
292 | } |
293 | |
294 | spl_autoload_register( [ 'AutoLoader', 'autoload' ] ); |
295 | |
296 | // Load composer's autoloader if present |
297 | if ( is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
298 | require_once __DIR__ . '/../vendor/autoload.php'; |
299 | } elseif ( file_exists( __DIR__ . '/../vendor/autoload.php' ) ) { |
300 | die( __DIR__ . '/../vendor/autoload.php exists but is not readable' ); |
301 | } |