85 if ( !is_array( $flags ) ) {
90 if ( in_array(
'local', $flags ) ) {
91 $this->variableName =
'wgAutoloadLocalClasses';
102 foreach ( $paths as
$path ) {
115 foreach ( $namespaces as $ns =>
$path ) {
116 $ns = rtrim( $ns,
'\\' ) .
'\\';
117 $this->psr4Namespaces[$ns] = rtrim( self::normalizePathSeparator(
$path ),
'/' );
128 foreach ( $this->excludePaths as $dir ) {
129 if ( strpos(
$path, $dir ) === 0 ) {
148 throw new \Exception(
"Invalid path: $inputPath" );
150 if ( !str_starts_with(
$path, $this->basepath ) ) {
151 throw new \Exception(
"Path is not within basepath: $inputPath" );
153 $shortpath = substr(
$path, strlen( $this->basepath ) );
154 $this->overrides[$fqcn] = $shortpath;
166 $len = strlen( $this->basepath );
167 if ( !str_starts_with( $inputPath, $this->basepath ) ) {
168 throw new \Exception(
"Path is not within basepath: $inputPath" );
173 $fileContents = file_get_contents( $inputPath );
176 if ( preg_match(
'!^// *NO_AUTOLOAD!m', $fileContents ) ) {
182 '/(require|require_once)[ (].*(CommandLineInc.php|commandLine.inc)/',
188 $result = $this->collector->getClasses( $fileContents );
191 $result = array_filter( $result,
function ( $class ) use ( $inputPath ) {
192 $parts = explode(
'\\', $class );
193 for ( $i = count( $parts ) - 1; $i > 0; $i-- ) {
194 $ns = implode(
'\\', array_slice( $parts, 0, $i ) ) .
'\\';
195 if ( isset( $this->psr4Namespaces[$ns] ) ) {
196 $expectedPath = $this->psr4Namespaces[$ns] .
'/'
197 . implode(
'/', array_slice( $parts, $i ) )
199 if ( $inputPath === $expectedPath ) {
209 $shortpath = substr( $inputPath, $len );
210 $this->classes[$shortpath] = $result;
218 $it =
new RecursiveDirectoryIterator(
219 self::normalizePathSeparator( realpath( $dir ) ) );
220 $it =
new RecursiveIteratorIterator( $it );
223 if ( pathinfo(
$path, PATHINFO_EXTENSION ) ===
'php' ) {
238 $key =
'AutoloadClasses';
240 unset( $json[$key] );
243 foreach ( $this->classes as
$path => $contained ) {
244 foreach ( $contained as $fqcn ) {
246 $json[$key][$fqcn] = substr(
$path, 1 );
249 foreach ( $this->overrides as
$path => $fqcn ) {
251 $json[$key][$fqcn] = substr(
$path, 1 );
255 ksort( $json[$key] );
274 $format =
"%s => __DIR__ . %s,";
275 foreach ( $this->classes as
$path => $contained ) {
276 $exportedPath = var_export(
$path,
true );
277 foreach ( $contained as $fqcn ) {
280 var_export( $fqcn,
true ),
286 foreach ( $this->overrides as $fqcn =>
$path ) {
289 var_export( $fqcn,
true ),
290 var_export(
$path,
true )
299 if ( $this->variableName ===
'wgAutoloadClasses' ) {
305 $output = implode(
"\n\t",
$content );
310 global \${$this->variableName};
312 \${$this->variableName} {$op} [
327 public function getAutoload( $commandName =
'AutoloadGenerator' ) {
333 if (
$fileinfo[
'type'] === self::FILETYPE_JSON ) {
349 if ( file_exists( $this->basepath .
'/extension.json' ) ) {
351 'filename' => $this->basepath .
'/extension.json',
355 if ( file_exists( $this->basepath .
'/skin.json' ) ) {
357 'filename' => $this->basepath .
'/skin.json',
363 'filename' => $this->basepath .
'/autoload.php',
375 return str_replace(
'\\',
'/',
$path );
388 foreach ( [
'includes',
'languages',
'maintenance',
'mw-config' ] as $dir ) {
389 $this->
readDir( $this->basepath .
'/' . $dir );
391 foreach ( glob( $this->basepath .
'/*.php' ) as
$file ) {
Accepts a list of files and directories to search for php files and generates $wgAutoloadLocalClasses...
shouldExclude( $path)
Whether the file should be excluded.
string $basepath
Root path of the project being scanned for classes.
setExcludePaths(array $paths)
Directories that should be excluded.
getTargetFileinfo()
Returns the filename of the extension.json of skin.json, if there's any, or otherwise the path to the...
generateJsonAutoload( $filename)
Updates the AutoloadClasses field at the given filename.
string[] $psr4Namespaces
Configured PSR4 namespaces.
__construct( $basepath, $flags=[])
getAutoload( $commandName='AutoloadGenerator')
Returns all known classes as a string, which can be used to put into a target file (e....
forceClassPath( $fqcn, $inputPath)
Force a class to be autoloaded from a specific path, regardless of where or if it was detected.
array $classes
Map of file shortpath to list of FQCN detected within file.
generatePHPAutoload( $commandName, $filename)
Generates a PHP file setting up autoload information.
string $variableName
The global variable to write output to.
initMediaWikiDefault()
Initialize the source files and directories which are used for the MediaWiki default autoloader in {m...
setPsr4Namespaces(array $namespaces)
Unlike self::setExcludePaths(), this will only skip outputting the autoloader entry when the namespac...
ClassCollector $collector
Helper class extracts class names from php files.
string[] $excludePaths
Directories that should be excluded.
static normalizePathSeparator( $path)
Ensure that Unix-style path separators ("/") are used in the path.
array $overrides
Map of FQCN to relative path(from self::$basepath)
Reads PHP code and returns the FQCN of every class defined within it.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.