MediaWiki REL1_40
AutoLoader.php
Go to the documentation of this file.
1<?php
23// NO_AUTOLOAD -- file scope code, can't load self
24
29require_once __DIR__ . '/../autoload.php';
30
32
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\\Config\\' => __DIR__ . '/config/',
50 'MediaWiki\\Content\\' => __DIR__ . '/content/',
51 'MediaWiki\\DB\\' => __DIR__ . '/db/',
52 'MediaWiki\\Deferred\\LinksUpdate\\' => __DIR__ . '/deferred/LinksUpdate/',
53 'MediaWiki\\Diff\\' => __DIR__ . '/diff/',
54 'MediaWiki\\EditPage\\' => __DIR__ . '/editpage/',
55 'MediaWiki\\Edit\\' => __DIR__ . '/edit/',
56 'MediaWiki\\FileBackend\\LockManager\\' => __DIR__ . '/filebackend/lockmanager/',
57 'MediaWiki\\Http\\' => __DIR__ . '/http/',
58 'MediaWiki\\Installer\\' => __DIR__ . '/installer/',
59 'MediaWiki\\Interwiki\\' => __DIR__ . '/interwiki/',
60 'MediaWiki\\JobQueue\\' => __DIR__ . '/jobqueue/',
61 'MediaWiki\\Json\\' => __DIR__ . '/json/',
62 'MediaWiki\\Languages\\Data\\' => __DIR__ . '/languages/data/',
63 'MediaWiki\\Linker\\' => __DIR__ . '/linker/',
64 'MediaWiki\\Logger\\' => __DIR__ . '/debug/logger/',
65 'MediaWiki\\Logger\\Monolog\\' => __DIR__ . '/debug/logger/monolog/',
66 'MediaWiki\\Mail\\' => __DIR__ . '/mail/',
67 'MediaWiki\\Page\\' => __DIR__ . '/page/',
68 'MediaWiki\\Parser\\' => __DIR__ . '/parser/',
69 'MediaWiki\\PoolCounter\\' => __DIR__ . '/poolcounter/',
70 'MediaWiki\\Preferences\\' => __DIR__ . '/preferences/',
71 'MediaWiki\\Search\\' => __DIR__ . '/search/',
72 'MediaWiki\\Search\\SearchWidgets\\' => __DIR__ . '/search/searchwidgets/',
73 'MediaWiki\\Session\\' => __DIR__ . '/session/',
74 'MediaWiki\\Shell\\' => __DIR__ . '/shell/',
75 'MediaWiki\\Site\\' => __DIR__ . '/site/',
76 'MediaWiki\\Sparql\\' => __DIR__ . '/sparql/',
77 'MediaWiki\\SpecialPage\\' => __DIR__ . '/specialpage/',
78 'MediaWiki\\Specials\\Contribute\\' => __DIR__ . '/specials/Contribute',
79 'MediaWiki\\Tidy\\' => __DIR__ . '/tidy/',
80 'MediaWiki\\User\\' => __DIR__ . '/user/',
81 'MediaWiki\\Utils\\' => __DIR__ . '/utils/',
82 'MediaWiki\\Widget\\' => __DIR__ . '/widget/',
83 'Wikimedia\\' => __DIR__ . '/libs/',
84 'Wikimedia\\Http\\' => __DIR__ . '/libs/http/',
85 'Wikimedia\\Rdbms\\Platform\\' => __DIR__ . '/libs/rdbms/platform/',
86 'Wikimedia\\UUID\\' => __DIR__ . '/libs/uuid/',
87 ];
88
92 private static $psr4Namespaces = self::CORE_NAMESPACES;
93
97 private static $classFiles = [];
98
107 public static function registerNamespaces( array $dirs ): void {
108 self::$psr4Namespaces += $dirs;
109 }
110
117 public static function registerClasses( array $files ): void {
118 self::$classFiles += $files;
119 }
120
137 public static function loadFile( string $file ): void {
138 require_once $file;
139 }
140
150 public static function loadFiles( array $files ): void {
151 foreach ( $files as $f ) {
152 self::loadFile( $f );
153 }
154 }
155
162 public static function find( $className ): ?string {
164
165 // NOTE: $wgAutoloadClasses is supported for compatibility with old-style extension
166 // registration files.
167
168 $filename = $wgAutoloadLocalClasses[$className] ??
169 self::$classFiles[$className] ??
170 $wgAutoloadClasses[$className] ??
171 false;
172
173 if ( !$filename && strpos( $className, '\\' ) !== false ) {
174 // This class is namespaced, so look in the namespace map
175 $prefix = $className;
176 while ( ( $pos = strrpos( $prefix, '\\' ) ) !== false ) {
177 // Check to see if this namespace prefix is in the map
178 $prefix = substr( $className, 0, $pos + 1 );
179 if ( isset( self::$psr4Namespaces[$prefix] ) ) {
180 $relativeClass = substr( $className, $pos + 1 );
181 // Build the expected filename, and see if it exists
182 $file = self::$psr4Namespaces[$prefix] .
183 '/' .
184 strtr( $relativeClass, '\\', '/' ) .
185 '.php';
186 if ( is_file( $file ) ) {
187 $filename = $file;
188 break;
189 }
190 }
191
192 // Remove trailing separator for next iteration
193 $prefix = rtrim( $prefix, '\\' );
194 }
195 }
196
197 if ( !$filename ) {
198 // Class not found; let the next autoloader try to find it
199 return null;
200 }
201
202 // Make an absolute path, this improves performance by avoiding some stat calls
203 // Optimisation: use string offset access instead of substr
204 if ( $filename[0] !== '/' && $filename[1] !== ':' ) {
205 $filename = __DIR__ . '/../' . $filename;
206 }
207
208 return $filename;
209 }
210
216 public static function autoload( $className ) {
217 $filename = self::find( $className );
218
219 if ( $filename !== null ) {
220 require $filename;
221 }
222 }
223
225 private static function assertTesting( $method ) {
226 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
227 throw new LogicException( "$method is not supported outside phpunit tests!" );
228 }
229 }
230
236 public static function getClassFiles(): array {
238
239 self::assertTesting( __METHOD__ );
240
241 // NOTE: ensure the order of preference is the same as used by find().
242 return array_merge(
244 self::$classFiles,
246 );
247 }
248
254 public static function getNamespaceDirectories(): array {
255 self::assertTesting( __METHOD__ );
256 return self::$psr4Namespaces;
257 }
258
266 public static function getState(): array {
267 self::assertTesting( __METHOD__ );
268 return [
269 'classFiles' => self::$classFiles,
270 'psr4Namespaces' => self::$psr4Namespaces,
271 ];
272 }
273
282 public static function restoreState( $state ): void {
283 self::assertTesting( __METHOD__ );
284
285 self::$classFiles = $state['classFiles'];
286 self::$psr4Namespaces = $state['psr4Namespaces'];
287 }
288
289}
290
291spl_autoload_register( [ 'AutoLoader', 'autoload' ] );
292
293// Load composer's autoloader if present
294if ( is_readable( __DIR__ . '/../vendor/autoload.php' ) ) {
295 require_once __DIR__ . '/../vendor/autoload.php';
296} elseif ( file_exists( __DIR__ . '/../vendor/autoload.php' ) ) {
297 die( __DIR__ . '/../vendor/autoload.php exists but is not readable' );
298}
$wgAutoloadClasses
Definition Setup.php:143
global $wgAutoloadLocalClasses
Definition autoload.php:4
Locations of core classes Extension classes are specified with $wgAutoloadClasses.
static registerClasses(array $files)
Register a file to load the given class from.
static loadFiles(array $files)
Batch version of loadFile()
static restoreState( $state)
Returns an array representing the internal state of Autoloader, so it can be remembered and later res...
static getNamespaceDirectories()
Returns a map of namespace names to directories, per PSR4.
static registerNamespaces(array $dirs)
Register a directory to load the classes of a given namespace from, per PSR4.
static find( $className)
Find the file containing the given class.
static getState()
Returns an array representing the internal state of Autoloader, so it can be remembered and later res...
static getClassFiles()
Returns a map of class names to file paths for testing.
const CORE_NAMESPACES
A mapping of namespace => file path for MediaWiki core.
static loadFile(string $file)
Load a file that declares classes, functions, or constants.
static autoload( $className)
autoload - take a class name and attempt to load it
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42