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 $len = strlen( $this->basepath );
151 if ( substr(
$path, 0, $len ) !== $this->basepath ) {
152 throw new \Exception(
"Path is not within basepath: $inputPath" );
154 $shortpath = substr(
$path, $len );
155 $this->overrides[$fqcn] = $shortpath;
167 $len = strlen( $this->basepath );
168 if ( substr( $inputPath, 0, $len ) !== $this->basepath ) {
169 throw new \Exception(
"Path is not within basepath: $inputPath" );
174 $fileContents = file_get_contents( $inputPath );
177 if ( preg_match(
'!^// *NO_AUTOLOAD!m', $fileContents ) ) {
183 '/(require|require_once)[ (].*(CommandLineInc.php|commandLine.inc)/',
189 $result = $this->collector->getClasses( $fileContents );
192 $result = array_filter( $result,
function ( $class ) use ( $inputPath ) {
193 $parts = explode(
'\\', $class );
194 for ( $i = count( $parts ) - 1; $i > 0; $i-- ) {
195 $ns = implode(
'\\', array_slice( $parts, 0, $i ) ) .
'\\';
196 if ( isset( $this->psr4Namespaces[$ns] ) ) {
197 $expectedPath = $this->psr4Namespaces[$ns] .
'/'
198 . implode(
'/', array_slice( $parts, $i ) )
200 if ( $inputPath === $expectedPath ) {
210 $shortpath = substr( $inputPath, $len );
211 $this->classes[$shortpath] = $result;
219 $it =
new RecursiveDirectoryIterator(
220 self::normalizePathSeparator( realpath( $dir ) ) );
221 $it =
new RecursiveIteratorIterator( $it );
224 if ( pathinfo(
$path, PATHINFO_EXTENSION ) ===
'php' ) {
239 $key =
'AutoloadClasses';
240 $json = FormatJson::decode( file_get_contents( $filename ),
true );
241 unset( $json[$key] );
244 foreach ( $this->classes as
$path => $contained ) {
245 foreach ( $contained as $fqcn ) {
247 $json[$key][$fqcn] = substr(
$path, 1 );
250 foreach ( $this->overrides as
$path => $fqcn ) {
252 $json[$key][$fqcn] = substr(
$path, 1 );
256 ksort( $json[$key] );
259 return FormatJson::encode( $json,
"\t", FormatJson::ALL_OK ) .
"\n";
275 $format =
"%s => __DIR__ . %s,";
276 foreach ( $this->classes as
$path => $contained ) {
277 $exportedPath = var_export(
$path,
true );
278 foreach ( $contained as $fqcn ) {
281 var_export( $fqcn,
true ),
287 foreach ( $this->overrides as $fqcn =>
$path ) {
290 var_export( $fqcn,
true ),
291 var_export(
$path,
true )
300 if ( $this->variableName ===
'wgAutoloadClasses' ) {
306 $output = implode(
"\n\t",
$content );
311global \${$this->variableName};
313\${$this->variableName} {$op} [
328 public function getAutoload( $commandName =
'AutoloadGenerator' ) {
334 if (
$fileinfo[
'type'] === self::FILETYPE_JSON ) {
350 if ( file_exists( $this->basepath .
'/extension.json' ) ) {
352 'filename' => $this->basepath .
'/extension.json',
356 if ( file_exists( $this->basepath .
'/skin.json' ) ) {
358 'filename' => $this->basepath .
'/skin.json',
364 'filename' => $this->basepath .
'/autoload.php',
376 return str_replace(
'\\',
'/',
$path );
389 foreach ( [
'includes',
'languages',
'maintenance',
'mw-config' ] as $dir ) {
390 $this->
readDir( $this->basepath .
'/' . $dir );
392 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.