MediaWiki REL1_33
AutoLoaderStructureTest.php
Go to the documentation of this file.
1<?php
2
13 public function testAutoLoadConfig() {
14 $results = self::checkAutoLoadConf();
15
16 $this->assertEquals(
17 $results['expected'],
18 $results['actual']
19 );
20 }
21
22 public function providePSR4Completeness() {
23 foreach ( AutoLoader::$psr4Namespaces as $prefix => $dir ) {
24 foreach ( $this->recurseFiles( $dir ) as $file ) {
25 yield [ $prefix, $dir, $file ];
26 }
27 }
28 }
29
30 private function recurseFiles( $dir ) {
31 return ( new File_Iterator_Facade() )->getFilesAsArray( $dir, [ '.php' ] );
32 }
33
37 public function testPSR4Completeness( $prefix, $dir, $file ) {
39 $contents = file_get_contents( $file );
40 list( $classesInFile, $aliasesInFile ) = self::parseFile( $contents );
41 $classes = array_keys( $classesInFile );
42 if ( $classes ) {
43 $this->assertCount(
44 1,
45 $classes,
46 "Only one class per file in PSR-4 autoloaded classes ($file)"
47 );
48
49 // Check that the expected class name (based on the filename) is the
50 // same as the one we found.
51 // Strip directory prefix from front of filename, and .php extension
52 $abbrFileName = substr( substr( $file, strlen( $dir ) ), 0, -4 );
53 $expectedClassName = $prefix . str_replace( '/', '\\', $abbrFileName );
54
55 $this->assertSame(
56 $expectedClassName,
57 $classes[0],
58 "Class not autoloaded properly"
59 );
60
61 } else {
62 // Dummy assertion so this test isn't marked in risky
63 // if the file has no classes nor aliases in it
64 $this->assertCount( 0, $classes );
65 }
66
67 if ( $aliasesInFile ) {
69 foreach ( $aliasesInFile as $alias => $class ) {
70 $this->assertArrayHasKey( $alias, $otherClasses,
71 'Alias must be in the classmap autoloader'
72 );
73 }
74 }
75 }
76
77 private static function parseFile( $contents ) {
78 // We could use token_get_all() here, but this is faster
79 // Note: Keep in sync with ClassCollector
80 $matches = [];
81 preg_match_all( '/
82 ^ [\t ]* (?:
83 (?:final\s+)? (?:abstract\s+)? (?:class|interface|trait) \s+
84 (?P<class> \w+)
85 |
86 class_alias \s* \‍( \s*
87 ([\'"]) (?P<original> [^\'"]+) \g{-2} \s* , \s*
88 ([\'"]) (?P<alias> [^\'"]+ ) \g{-2} \s*
89 \‍) \s* ;
90 |
91 class_alias \s* \‍( \s*
92 (?P<originalStatic> [\w\\\\]+)::class \s* , \s*
93 ([\'"]) (?P<aliasString> [^\'"]+ ) \g{-2} \s*
94 \‍) \s* ;
95 )
96 /imx', $contents, $matches, PREG_SET_ORDER );
97
98 $namespaceMatch = [];
99 preg_match( '/
100 ^ [\t ]*
101 namespace \s+
102 (\w+(\\\\\w+)*)
103 \s* ;
104 /imx', $contents, $namespaceMatch );
105 $fileNamespace = $namespaceMatch ? $namespaceMatch[1] . '\\' : '';
106
107 $classesInFile = [];
108 $aliasesInFile = [];
109
110 foreach ( $matches as $match ) {
111 if ( !empty( $match['class'] ) ) {
112 // 'class Foo {}'
113 $class = $fileNamespace . $match['class'];
114 $classesInFile[$class] = true;
115 } elseif ( !empty( $match['original'] ) ) {
116 // 'class_alias( "Foo", "Bar" );'
117 $aliasesInFile[$match['alias']] = $match['original'];
118 } else {
119 // 'class_alias( Foo::class, "Bar" );'
120 $aliasesInFile[$match['aliasString']] = $fileNamespace . $match['originalStatic'];
121 }
122 }
123
124 return [ $classesInFile, $aliasesInFile ];
125 }
126
127 protected static function checkAutoLoadConf() {
129
130 // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
132 $actual = [];
133
134 $psr4Namespaces = [];
135 foreach ( AutoLoader::getAutoloadNamespaces() as $ns => $path ) {
136 $psr4Namespaces[rtrim( $ns, '\\' ) . '\\'] = rtrim( $path, '/' );
137 }
138
139 foreach ( $expected as $class => $file ) {
140 // Only prefix $IP if it doesn't have it already.
141 // Generally local classes don't have it, and those from extensions and test suites do.
142 if ( substr( $file, 0, 1 ) != '/' && substr( $file, 1, 1 ) != ':' ) {
143 $filePath = "$IP/$file";
144 } else {
145 $filePath = $file;
146 }
147
148 if ( !file_exists( $filePath ) ) {
149 $actual[$class] = "[file '$filePath' does not exist]";
150 continue;
151 }
152
153 Wikimedia\suppressWarnings();
154 $contents = file_get_contents( $filePath );
155 Wikimedia\restoreWarnings();
156
157 if ( $contents === false ) {
158 $actual[$class] = "[couldn't read file '$filePath']";
159 continue;
160 }
161
162 list( $classesInFile, $aliasesInFile ) = self::parseFile( $contents );
163
164 foreach ( $classesInFile as $className => $ignore ) {
165 // Skip if it's a PSR4 class
166 $parts = explode( '\\', $className );
167 for ( $i = count( $parts ) - 1; $i > 0; $i-- ) {
168 $ns = implode( '\\', array_slice( $parts, 0, $i ) ) . '\\';
169 if ( isset( $psr4Namespaces[$ns] ) ) {
170 $expectedPath = $psr4Namespaces[$ns] . '/'
171 . implode( '/', array_slice( $parts, $i ) )
172 . '.php';
173 if ( $filePath === $expectedPath ) {
174 continue 2;
175 }
176 }
177 }
178
179 // Nope, add it.
180 $actual[$className] = $file;
181 }
182
183 // Only accept aliases for classes in the same file, because for correct
184 // behavior, all aliases for a class must be set up when the class is loaded
185 // (see <https://bugs.php.net/bug.php?id=61422>).
186 foreach ( $aliasesInFile as $alias => $class ) {
187 if ( isset( $classesInFile[$class] ) ) {
188 $actual[$alias] = $file;
189 } else {
190 $actual[$alias] = "[original class not in $file]";
191 }
192 }
193 }
194
195 return [
196 'expected' => $expected,
197 'actual' => $actual,
198 ];
199 }
200
201 public function testAutoloadOrder() {
202 $path = realpath( __DIR__ . '/../../..' );
203 $oldAutoload = file_get_contents( $path . '/autoload.php' );
204 $generator = new AutoloadGenerator( $path, 'local' );
205 $generator->setPsr4Namespaces( AutoLoader::getAutoloadNamespaces() );
206 $generator->initMediaWikiDefault();
207 $newAutoload = $generator->getAutoload( 'maintenance/generateLocalAutoload.php' );
208
209 $this->assertEquals( $oldAutoload, $newAutoload, 'autoload.php does not match' .
210 ' output of generateLocalAutoload.php script.' );
211 }
212}
$wgAutoloadClasses
Array mapping class names to filenames, for autoloading.
$IP
Definition WebStart.php:41
global $wgAutoloadLocalClasses
Definition autoload.php:4
testAutoLoadConfig()
Assert that there were no classes loaded that are not registered with the AutoLoader.
testPSR4Completeness( $prefix, $dir, $file)
providePSR4Completeness
static getAutoloadNamespaces()
Get a mapping of namespace => file path The namespaces should follow the PSR-4 standard for autoloadi...
static string[] $psr4Namespaces
Accepts a list of files and directories to search for php files and generates $wgAutoloadLocalClasses...
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition router.php:42