Merge "Upgrade wikimedia/remex-html to 2.0.1" into REL1_31
[lhc/web/wiklou.git] / includes / AutoLoader.php
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 /**
24 * Locations of core classes
25 * Extension classes are specified with $wgAutoloadClasses
26 * This array is a global instead of a static member of AutoLoader to work around a bug in APC
27 */
28 require_once __DIR__ . '/../autoload.php';
29
30 class AutoLoader {
31 static protected $autoloadLocalClassesLower = null;
32
33 /**
34 * @private Only public for ExtensionRegistry
35 * @var string[] Namespace (ends with \) => Path (ends with /)
36 */
37 static public $psr4Namespaces = [];
38
39 /**
40 * autoload - take a class name and attempt to load it
41 *
42 * @param string $className Name of class we're looking for.
43 */
44 static function autoload( $className ) {
45 global $wgAutoloadClasses, $wgAutoloadLocalClasses,
46 $wgAutoloadAttemptLowercase;
47
48 $filename = false;
49
50 if ( isset( $wgAutoloadLocalClasses[$className] ) ) {
51 $filename = $wgAutoloadLocalClasses[$className];
52 } elseif ( isset( $wgAutoloadClasses[$className] ) ) {
53 $filename = $wgAutoloadClasses[$className];
54 } elseif ( $wgAutoloadAttemptLowercase ) {
55 /*
56 * Try a different capitalisation.
57 *
58 * PHP 4 objects are always serialized with the classname coerced to lowercase,
59 * and we are plagued with several legacy uses created by MediaWiki < 1.5, see
60 * https://wikitech.wikimedia.org/wiki/Text_storage_data
61 */
62 $lowerClass = strtolower( $className );
63
64 if ( self::$autoloadLocalClassesLower === null ) {
65 self::$autoloadLocalClassesLower = array_change_key_case( $wgAutoloadLocalClasses, CASE_LOWER );
66 }
67
68 if ( isset( self::$autoloadLocalClassesLower[$lowerClass] ) ) {
69 if ( function_exists( 'wfDebugLog' ) ) {
70 wfDebugLog( 'autoloader', "Class {$className} was loaded using incorrect case" );
71 }
72 $filename = self::$autoloadLocalClassesLower[$lowerClass];
73 }
74 }
75
76 if ( !$filename && strpos( $className, '\\' ) !== false ) {
77 // This class is namespaced, so try looking at the namespace map
78 $prefix = $className;
79 while ( false !== $pos = strrpos( $prefix, '\\' ) ) {
80 // Check to see if this namespace prefix is in the map
81 $prefix = substr( $className, 0, $pos + 1 );
82 if ( isset( self::$psr4Namespaces[$prefix] ) ) {
83 $relativeClass = substr( $className, $pos + 1 );
84 // Build the expected filename, and see if it exists
85 $file = self::$psr4Namespaces[$prefix] . '/' .
86 str_replace( '\\', '/', $relativeClass ) . '.php';
87 if ( file_exists( $file ) ) {
88 $filename = $file;
89 break;
90 }
91 }
92
93 // Remove trailing separator for next iteration
94 $prefix = rtrim( $prefix, '\\' );
95 }
96 }
97
98 if ( !$filename ) {
99 // Class not found; let the next autoloader try to find it
100 return;
101 }
102
103 // Make an absolute path, this improves performance by avoiding some stat calls
104 if ( substr( $filename, 0, 1 ) != '/' && substr( $filename, 1, 1 ) != ':' ) {
105 global $IP;
106 $filename = "$IP/$filename";
107 }
108
109 require $filename;
110 }
111
112 /**
113 * Method to clear the protected class property $autoloadLocalClassesLower.
114 * Used in tests.
115 */
116 static function resetAutoloadLocalClassesLower() {
117 self::$autoloadLocalClassesLower = null;
118 }
119
120 /**
121 * Get a mapping of namespace => file path
122 * The namespaces should follow the PSR-4 standard for autoloading
123 *
124 * @see <http://www.php-fig.org/psr/psr-4/>
125 * @private Only public for usage in AutoloadGenerator
126 * @since 1.31
127 * @return string[]
128 */
129 public static function getAutoloadNamespaces() {
130 return [
131 'MediaWiki\\Linker\\' => __DIR__ .'/linker/'
132 ];
133 }
134 }
135
136 AutoLoader::$psr4Namespaces = AutoLoader::getAutoloadNamespaces();
137 spl_autoload_register( [ 'AutoLoader', 'autoload' ] );