MediaWiki master
AutoLoader Class Reference

This initializes autoloading for MediaWiki core, extensions, and vendored libs. More...

Static Public Member Functions

static autoload ( $className)
 autoload - take a class name and attempt to load it
 
static find ( $className)
 Find the file containing the given class.
 
static getClassFiles ()
 Returns a map of class names to file paths for testing.
 
static getNamespaceDirectories ()
 Returns a map of namespace names to directories, per PSR4.
 
static getState ()
 Returns an array representing the internal state of Autoloader, so it can be remembered and later restored during testing.
 
static loadFile (string $file)
 Load a file that declares classes, functions, or constants.
 
static loadFiles (array $files)
 Batch version of loadFile()
 
static registerClasses (array $files)
 Register a file to load the given class from.
 
static registerNamespaces (array $dirs)
 Register a directory to load the classes of a given namespace from, per PSR4.
 
static restoreState ( $state)
 Returns an array representing the internal state of Autoloader, so it can be remembered and later restored during testing.
 

Public Attributes

const CORE_NAMESPACES
 A mapping of namespace => file path for MediaWiki core.
 

Detailed Description

This initializes autoloading for MediaWiki core, extensions, and vendored libs.

NOTE: This file sets up the PHP autoloader and so its stable contract is not this class, but the act of initializing spl_autoload_register and vendor. This file is widely referenced (akin to includes/Defines.php) and is therefore not renamed or moved to /includes/autoload.

Since
1.7

Definition at line 30 of file AutoLoader.php.

Member Function Documentation

◆ autoload()

static AutoLoader::autoload ( $className)
static

autoload - take a class name and attempt to load it

Parameters
class-string$classNameName of class we're looking for.

Definition at line 179 of file AutoLoader.php.

◆ find()

static AutoLoader::find ( $className)
static

Find the file containing the given class.

Parameters
class-string$classNameName of class we're looking for.
Returns
string|null The path containing the class, not null if not found

Definition at line 125 of file AutoLoader.php.

◆ getClassFiles()

static AutoLoader::getClassFiles ( )
static

Returns a map of class names to file paths for testing.

Note
Will throw if called outside of phpunit tests!
Returns
string[]

Definition at line 199 of file AutoLoader.php.

◆ getNamespaceDirectories()

static AutoLoader::getNamespaceDirectories ( )
static

Returns a map of namespace names to directories, per PSR4.

Note
Will throw if called outside of phpunit tests!
Returns
string[]

Definition at line 217 of file AutoLoader.php.

◆ getState()

static AutoLoader::getState ( )
static

Returns an array representing the internal state of Autoloader, so it can be remembered and later restored during testing.

Access: internal
Note
Will throw if called outside of phpunit tests!
Returns
array

Definition at line 229 of file AutoLoader.php.

◆ loadFile()

static AutoLoader::loadFile ( string $file)
static

Load a file that declares classes, functions, or constants.

The file will be loaded immediately using require_once in function scope.

Note
The file to be loaded MUST NOT set global variables or otherwise affect the global state. It MAY however use conditionals to determine what to declare and how, e.g. to provide polyfills.
The file to be loaded MUST NOT assume that MediaWiki has been initialized. In particular, it MUST NOT access configuration variables or MediaWikiServices.
Since
1.39
Parameters
string$filethe path of the file to load.

Definition at line 100 of file AutoLoader.php.

◆ loadFiles()

static AutoLoader::loadFiles ( array $files)
static

Batch version of loadFile()

See also
loadFile()
Since
1.39
Parameters
string[]$filesthe paths of the files to load.

Definition at line 113 of file AutoLoader.php.

◆ registerClasses()

static AutoLoader::registerClasses ( array $files)
static

Register a file to load the given class from.

Since
1.39
Parameters
string[]$filesa map of qualified class names to file names

Definition at line 80 of file AutoLoader.php.

◆ registerNamespaces()

static AutoLoader::registerNamespaces ( array $dirs)
static

Register a directory to load the classes of a given namespace from, per PSR4.

See also
https://www.php-fig.org/psr/psr-4/
Since
1.39
Parameters
string[]$dirsa map of namespace (ends with \) to path (ends with /)

Definition at line 70 of file AutoLoader.php.

◆ restoreState()

static AutoLoader::restoreState ( $state)
static

Returns an array representing the internal state of Autoloader, so it can be remembered and later restored during testing.

Access: internal
Note
Will throw if called outside of phpunit tests!
Parameters
array$stateA state array returned by getState().

Definition at line 245 of file AutoLoader.php.

Member Data Documentation

◆ CORE_NAMESPACES

const AutoLoader::CORE_NAMESPACES
Initial value:
= [
'MediaWiki\\' => __DIR__ . '/',
'MediaWiki\\Maintenance\\' => __DIR__ . '/../maintenance/includes/',
'Wikimedia\\' => __DIR__ . '/libs/',
]

A mapping of namespace => file path for MediaWiki core.

The namespaces must follow the PSR-4 standard for autoloading.

MediaWiki core does not use PSR-4 autoloading due to performance issues, but enforce the mapping to be maintained for future use. Instead using PSR-0, class map stored in autoload.php generated via script: php maintenance/run.php generateLocalAutoload

See also
https://www.php-fig.org/psr/psr-4/
https://techblog.wikimedia.org/2024/01/16/web-perf-hero-mate-szabo/
Access: internal
Only public for usage in AutoloadGenerator/AutoLoaderTest

Definition at line 46 of file AutoLoader.php.


The documentation for this class was generated from the following file: