Doc: Mention the php command in autoload.php to regenerate it
[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 * This array is now generated by maintenance/generateLocalAutoload.php
28 */
29 require_once __DIR__ . '/../autoload.php';
30
31 class AutoLoader {
32 static protected $autoloadLocalClassesLower = null;
33
34 /**
35 * autoload - take a class name and attempt to load it
36 *
37 * @param string $className Name of class we're looking for.
38 */
39 static function autoload( $className ) {
40 global $wgAutoloadClasses, $wgAutoloadLocalClasses,
41 $wgAutoloadAttemptLowercase;
42
43 // Workaround for PHP bug <https://bugs.php.net/bug.php?id=49143> (5.3.2. is broken, it's
44 // fixed in 5.3.6). Strip leading backslashes from class names. When namespaces are used,
45 // leading backslashes are used to indicate the top-level namespace, e.g. \foo\Bar. When
46 // used like this in the code, the leading backslash isn't passed to the auto-loader
47 // ($className would be 'foo\Bar'). However, if a class is accessed using a string instead
48 // of a class literal (e.g. $class = '\foo\Bar'; new $class()), then some versions of PHP
49 // do not strip the leading backlash in this case, causing autoloading to fail.
50 $className = ltrim( $className, '\\' );
51
52 $filename = false;
53
54 if ( isset( $wgAutoloadLocalClasses[$className] ) ) {
55 $filename = $wgAutoloadLocalClasses[$className];
56 } elseif ( isset( $wgAutoloadClasses[$className] ) ) {
57 $filename = $wgAutoloadClasses[$className];
58 } elseif ( $wgAutoloadAttemptLowercase ) {
59 /*
60 * Try a different capitalisation.
61 *
62 * PHP 4 objects are always serialized with the classname coerced to lowercase,
63 * and we are plagued with several legacy uses created by MediaWiki < 1.5, see
64 * https://wikitech.wikimedia.org/wiki/Text_storage_data
65 */
66 $lowerClass = strtolower( $className );
67
68 if ( self::$autoloadLocalClassesLower === null ) {
69 self::$autoloadLocalClassesLower = array_change_key_case( $wgAutoloadLocalClasses, CASE_LOWER );
70 }
71
72 if ( isset( self::$autoloadLocalClassesLower[$lowerClass] ) ) {
73 if ( function_exists( 'wfDebugLog' ) ) {
74 wfDebugLog( 'autoloader', "Class {$className} was loaded using incorrect case" );
75 }
76 $filename = self::$autoloadLocalClassesLower[$lowerClass];
77 }
78 }
79
80 if ( !$filename ) {
81 // Class not found; let the next autoloader try to find it
82 return;
83 }
84
85 // Make an absolute path, this improves performance by avoiding some stat calls
86 if ( substr( $filename, 0, 1 ) != '/' && substr( $filename, 1, 1 ) != ':' ) {
87 global $IP;
88 $filename = "$IP/$filename";
89 }
90
91 require $filename;
92 }
93
94 /**
95 * Force a class to be run through the autoloader, helpful for things like
96 * Sanitizer that have define()s outside of their class definition. Of course
97 * this wouldn't be necessary if everything in MediaWiki was class-based. Sigh.
98 *
99 * @param string $class
100 * @return bool Return the results of class_exists() so we know if we were successful
101 */
102 static function loadClass( $class ) {
103 return class_exists( $class );
104 }
105
106 /**
107 * Method to clear the protected class property $autoloadLocalClassesLower.
108 * Used in tests.
109 */
110 static function resetAutoloadLocalClassesLower() {
111 self::$autoloadLocalClassesLower = null;
112 }
113 }
114
115 spl_autoload_register( array( 'AutoLoader', 'autoload' ) );