29 private const FILETYPE_JSON =
'json';
30 private const FILETYPE_PHP =
'php';
79 if ( !is_array( $flags ) ) {
84 if ( in_array(
'local', $flags ) ) {
85 $this->variableName =
'wgAutoloadLocalClasses';
96 foreach ( $paths as
$path ) {
111 foreach ( $namespaces as $ns =>
$path ) {
112 $ns = rtrim( $ns,
'\\' ) .
'\\';
113 $this->psr4Namespaces[$ns] = rtrim( self::normalizePathSeparator(
$path ),
'/' );
123 private function shouldExclude(
$path ) {
124 foreach ( $this->excludePaths as $dir ) {
125 if ( str_starts_with(
$path, $dir ) ) {
144 throw new InvalidArgumentException(
"Invalid path: $inputPath" );
146 if ( !str_starts_with(
$path, $this->basepath ) ) {
147 throw new InvalidArgumentException(
"Path is not within basepath: $inputPath" );
149 $shortpath = substr(
$path, strlen( $this->basepath ) );
150 $this->overrides[$fqcn] = $shortpath;
162 $len = strlen( $this->basepath );
163 if ( !str_starts_with( $inputPath, $this->basepath ) ) {
164 throw new InvalidArgumentException(
"Path is not within basepath: $inputPath" );
166 if ( $this->shouldExclude( $inputPath ) ) {
169 $fileContents = file_get_contents( $inputPath );
172 if ( preg_match(
'!^// *NO_AUTOLOAD!m', $fileContents ) ) {
178 '/(require|require_once)[ (].*(CommandLineInc.php|commandLine.inc)/',
184 $result = $this->collector->getClasses( $fileContents );
187 $shortpath = substr( $inputPath, $len );
188 $this->classes[$shortpath] = $result;
196 $it =
new RecursiveDirectoryIterator(
197 self::normalizePathSeparator( realpath( $dir ) ) );
198 $it =
new RecursiveIteratorIterator( $it );
200 foreach ( $it as
$path => $file ) {
201 if ( pathinfo(
$path, PATHINFO_EXTENSION ) ===
'php' ) {
216 $key =
'AutoloadClasses';
217 $json = FormatJson::decode( file_get_contents( $filename ),
true );
218 unset( $json[$key] );
221 foreach ( $this->classes as
$path => $contained ) {
222 foreach ( $contained as $fqcn ) {
224 $json[$key][$fqcn] = substr(
$path, 1 );
227 foreach ( $this->overrides as
$path => $fqcn ) {
229 $json[$key][$fqcn] = substr(
$path, 1 );
233 ksort( $json[$key] );
236 return FormatJson::encode( $json,
"\t", FormatJson::ALL_OK ) .
"\n";
252 $format =
"%s => __DIR__ . %s,";
253 foreach ( $this->classes as
$path => $contained ) {
254 $exportedPath = var_export(
$path,
true );
255 foreach ( $contained as $fqcn ) {
256 $content[$fqcn] = sprintf(
258 var_export( $fqcn,
true ),
264 foreach ( $this->overrides as $fqcn =>
$path ) {
265 $content[$fqcn] = sprintf(
267 var_export( $fqcn,
true ),
268 var_export(
$path,
true )
277 if ( $this->variableName ===
'wgAutoloadClasses' ) {
283 $output = implode(
"\n\t", $content );
288global \${$this->variableName};
290\${$this->variableName} {$op} [
305 public function getAutoload( $commandName =
'AutoloadGenerator' ) {
311 if ( $fileinfo[
'type'] === self::FILETYPE_JSON ) {
327 if ( file_exists( $this->basepath .
'/extension.json' ) ) {
329 'filename' => $this->basepath .
'/extension.json',
330 'type' => self::FILETYPE_JSON
333 if ( file_exists( $this->basepath .
'/skin.json' ) ) {
335 'filename' => $this->basepath .
'/skin.json',
336 'type' => self::FILETYPE_JSON
341 'filename' => $this->basepath .
'/autoload.php',
342 'type' => self::FILETYPE_PHP
353 return str_replace(
'\\',
'/',
$path );
366 foreach ( [
'includes',
'languages',
'maintenance',
'mw-config' ] as $dir ) {
367 $this->
readDir( $this->basepath .
'/' . $dir );
369 foreach ( glob( $this->basepath .
'/*.php' ) as $file ) {
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Scan given directories and files and create an autoload class map.
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)
Read a PHP file and return the FQCN of every class defined within it.