| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Json\FormatJson; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class AutoloadGenerator { |
| 37 |
private const FILETYPE_JSON = 'json'; |
| 38 |
private const FILETYPE_PHP = 'php'; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
protected $basepath; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected $collector; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected $classes = []; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
protected $variableName = 'wgAutoloadClasses'; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected $overrides = []; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
protected $excludePaths = []; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
protected $psr4Namespaces = []; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function __construct( $basepath, $flags = [] ) { |
| 87 |
if ( !is_array( $flags ) ) { |
| 88 |
$flags = [ $flags ]; |
| 89 |
} |
| 90 |
$this->basepath = self::normalizePathSeparator( realpath( $basepath ) ); |
| 91 |
$this->collector = new ClassCollector; |
| 92 |
if ( in_array( 'local', $flags ) ) { |
| 93 |
$this->variableName = 'wgAutoloadLocalClasses'; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function setExcludePaths( array $paths ) { |
| 104 |
foreach ( $paths as $path ) { |
| 105 |
$this->excludePaths[] = self::normalizePathSeparator( $path ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function setPsr4Namespaces( array $namespaces ) { |
| 118 |
foreach ( $namespaces as $ns => $path ) { |
| 119 |
$ns = rtrim( $ns, '\\' ) . '\\'; |
| 120 |
$this->psr4Namespaces[$ns] = rtrim( self::normalizePathSeparator( $path ), '/' ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private function shouldExclude( $path ) { |
| 131 |
foreach ( $this->excludePaths as $dir ) { |
| 132 |
if ( str_starts_with( $path, $dir ) ) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function forceClassPath( $fqcn, $inputPath ) { |
| 149 |
$path = self::normalizePathSeparator( realpath( $inputPath ) ); |
| 150 |
if ( !$path ) { |
| 151 |
throw new InvalidArgumentException( "Invalid path: $inputPath" ); |
| 152 |
} |
| 153 |
if ( !str_starts_with( $path, $this->basepath ) ) { |
| 154 |
throw new InvalidArgumentException( "Path is not within basepath: $inputPath" ); |
| 155 |
} |
| 156 |
$shortpath = substr( $path, strlen( $this->basepath ) ); |
| 157 |
$this->overrides[$fqcn] = $shortpath; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function readFile( $inputPath ) { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
$inputPath = self::normalizePathSeparator( $inputPath ); |
| 169 |
$len = strlen( $this->basepath ); |
| 170 |
if ( !str_starts_with( $inputPath, $this->basepath ) ) { |
| 171 |
throw new InvalidArgumentException( "Path is not within basepath: $inputPath" ); |
| 172 |
} |
| 173 |
if ( $this->shouldExclude( $inputPath ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
$fileContents = file_get_contents( $inputPath ); |
| 177 |
|
| 178 |
|
| 179 |
if ( preg_match( '!^// *NO_AUTOLOAD!m', $fileContents ) ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
if ( preg_match( |
| 185 |
'/(require|require_once)[ (].*(CommandLineInc.php|commandLine.inc)/', |
| 186 |
$fileContents ) |
| 187 |
) { |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
$result = $this->collector->getClasses( $fileContents ); |
| 192 |
|
| 193 |
if ( $result ) { |
| 194 |
$shortpath = substr( $inputPath, $len ); |
| 195 |
$this->classes[$shortpath] = $result; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public function readDir( $dir ) { |
| 203 |
$it = new RecursiveDirectoryIterator( |
| 204 |
self::normalizePathSeparator( realpath( $dir ) ) ); |
| 205 |
$it = new RecursiveIteratorIterator( $it ); |
| 206 |
|
| 207 |
foreach ( $it as $path => $file ) { |
| 208 |
if ( pathinfo( $path, PATHINFO_EXTENSION ) === 'php' ) { |
| 209 |
$this->readFile( $path ); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
protected function generateJsonAutoload( $filename ) { |
| 223 |
$key = 'AutoloadClasses'; |
| 224 |
$json = FormatJson::decode( file_get_contents( $filename ), true ); |
| 225 |
unset( $json[$key] ); |
| 226 |
|
| 227 |
|
| 228 |
foreach ( $this->classes as $path => $contained ) { |
| 229 |
foreach ( $contained as $fqcn ) { |
| 230 |
|
| 231 |
$json[$key][$fqcn] = substr( $path, 1 ); |
| 232 |
} |
| 233 |
} |
| 234 |
foreach ( $this->overrides as $path => $fqcn ) { |
| 235 |
|
| 236 |
$json[$key][$fqcn] = substr( $path, 1 ); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
ksort( $json[$key] ); |
| 241 |
|
| 242 |
|
| 243 |
return FormatJson::encode( $json, "\t", FormatJson::ALL_OK ) . "\n"; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
protected function generatePHPAutoload( $commandName, $filename ) { |
| 254 |
|
| 255 |
$content = []; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
$format = "%s => __DIR__ . %s,"; |
| 260 |
foreach ( $this->classes as $path => $contained ) { |
| 261 |
$exportedPath = var_export( $path, true ); |
| 262 |
foreach ( $contained as $fqcn ) { |
| 263 |
$content[$fqcn] = sprintf( |
| 264 |
$format, |
| 265 |
var_export( $fqcn, true ), |
| 266 |
$exportedPath |
| 267 |
); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
foreach ( $this->overrides as $fqcn => $path ) { |
| 272 |
$content[$fqcn] = sprintf( |
| 273 |
$format, |
| 274 |
var_export( $fqcn, true ), |
| 275 |
var_export( $path, true ) |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
ksort( $content ); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
if ( $this->variableName === 'wgAutoloadClasses' ) { |
| 285 |
$op = '+='; |
| 286 |
} else { |
| 287 |
$op = '='; |
| 288 |
} |
| 289 |
|
| 290 |
$output = implode( "\n\t", $content ); |
| 291 |
return <<<EOD |
| 292 |
<?php |
| 293 |
// This file is generated by $commandName, do not adjust manually |
| 294 |
// phpcs:disable Generic.Files.LineLength |
| 295 |
global \${$this->variableName}; |
| 296 |
|
| 297 |
\${$this->variableName} {$op} [ |
| 298 |
{$output} |
| 299 |
]; |
| 300 |
|
| 301 |
EOD; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public function getAutoload( $commandName = 'AutoloadGenerator' ) { |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
$fileinfo = $this->getTargetFileinfo(); |
| 317 |
|
| 318 |
if ( $fileinfo['type'] === self::FILETYPE_JSON ) { |
| 319 |
return $this->generateJsonAutoload( $fileinfo['filename'] ); |
| 320 |
} |
| 321 |
|
| 322 |
return $this->generatePHPAutoload( $commandName, $fileinfo['filename'] ); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function getTargetFileinfo() { |
| 334 |
if ( file_exists( $this->basepath . '/extension.json' ) ) { |
| 335 |
return [ |
| 336 |
'filename' => $this->basepath . '/extension.json', |
| 337 |
'type' => self::FILETYPE_JSON |
| 338 |
]; |
| 339 |
} |
| 340 |
if ( file_exists( $this->basepath . '/skin.json' ) ) { |
| 341 |
return [ |
| 342 |
'filename' => $this->basepath . '/skin.json', |
| 343 |
'type' => self::FILETYPE_JSON |
| 344 |
]; |
| 345 |
} |
| 346 |
|
| 347 |
return [ |
| 348 |
'filename' => $this->basepath . '/autoload.php', |
| 349 |
'type' => self::FILETYPE_PHP |
| 350 |
]; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
protected static function normalizePathSeparator( $path ) { |
| 360 |
return str_replace( '\\', '/', $path ); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
public function initMediaWikiDefault() { |
| 373 |
foreach ( [ 'includes', 'languages', 'maintenance', 'mw-config' ] as $dir ) { |
| 374 |
$this->readDir( $this->basepath . '/' . $dir ); |
| 375 |
} |
| 376 |
foreach ( glob( $this->basepath . '/*.php' ) as $file ) { |
| 377 |
$this->readFile( $file ); |
| 378 |
} |
| 379 |
} |
| 380 |
} |