37 private const FILETYPE_JSON =
'json';
38 private const FILETYPE_PHP =
'php';
87 if ( !is_array( $flags ) ) {
92 if ( in_array(
'local', $flags ) ) {
93 $this->variableName =
'wgAutoloadLocalClasses';
104 foreach ( $paths as
$path ) {
118 foreach ( $namespaces as $ns =>
$path ) {
119 $ns = rtrim( $ns,
'\\' ) .
'\\';
120 $this->psr4Namespaces[$ns] = rtrim( self::normalizePathSeparator(
$path ),
'/' );
130 private function shouldExclude(
$path ) {
131 foreach ( $this->excludePaths as $dir ) {
132 if ( str_starts_with(
$path, $dir ) ) {
151 throw new InvalidArgumentException(
"Invalid path: $inputPath" );
153 if ( !str_starts_with(
$path, $this->basepath ) ) {
154 throw new InvalidArgumentException(
"Path is not within basepath: $inputPath" );
156 $shortpath = substr(
$path, strlen( $this->basepath ) );
157 $this->overrides[$fqcn] = $shortpath;
169 $len = strlen( $this->basepath );
170 if ( !str_starts_with( $inputPath, $this->basepath ) ) {
171 throw new InvalidArgumentException(
"Path is not within basepath: $inputPath" );
173 if ( $this->shouldExclude( $inputPath ) ) {
176 $fileContents = file_get_contents( $inputPath );
179 if ( preg_match(
'!^// *NO_AUTOLOAD!m', $fileContents ) ) {
185 '/(require|require_once)[ (].*(CommandLineInc.php|commandLine.inc)/',
191 $result = $this->collector->getClasses( $fileContents );
194 $shortpath = substr( $inputPath, $len );
195 $this->classes[$shortpath] = $result;
203 $it =
new RecursiveDirectoryIterator(
204 self::normalizePathSeparator( realpath( $dir ) ) );
205 $it =
new RecursiveIteratorIterator( $it );
207 foreach ( $it as
$path => $file ) {
208 if ( pathinfo(
$path, PATHINFO_EXTENSION ) ===
'php' ) {
223 $key =
'AutoloadClasses';
224 $json = FormatJson::decode( file_get_contents( $filename ),
true );
225 unset( $json[$key] );
228 foreach ( $this->classes as
$path => $contained ) {
229 foreach ( $contained as $fqcn ) {
231 $json[$key][$fqcn] = substr(
$path, 1 );
234 foreach ( $this->overrides as
$path => $fqcn ) {
236 $json[$key][$fqcn] = substr(
$path, 1 );
240 ksort( $json[$key] );
243 return FormatJson::encode( $json,
"\t", FormatJson::ALL_OK ) .
"\n";
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(
265 var_export( $fqcn,
true ),
271 foreach ( $this->overrides as $fqcn =>
$path ) {
272 $content[$fqcn] = sprintf(
274 var_export( $fqcn,
true ),
275 var_export(
$path,
true )
284 if ( $this->variableName ===
'wgAutoloadClasses' ) {
290 $output = implode(
"\n\t", $content );
295global \${$this->variableName};
297\${$this->variableName} {$op} [
312 public function getAutoload( $commandName =
'AutoloadGenerator' ) {
318 if ( $fileinfo[
'type'] === self::FILETYPE_JSON ) {
334 if ( file_exists( $this->basepath .
'/extension.json' ) ) {
336 'filename' => $this->basepath .
'/extension.json',
337 'type' => self::FILETYPE_JSON
340 if ( file_exists( $this->basepath .
'/skin.json' ) ) {
342 'filename' => $this->basepath .
'/skin.json',
343 'type' => self::FILETYPE_JSON
348 'filename' => $this->basepath .
'/autoload.php',
349 'type' => self::FILETYPE_PHP
360 return str_replace(
'\\',
'/',
$path );
373 foreach ( [
'includes',
'languages',
'maintenance',
'mw-config' ] as $dir ) {
374 $this->
readDir( $this->basepath .
'/' . $dir );
376 foreach ( glob( $this->basepath .
'/*.php' ) as $file ) {
Accepts a list of files and directories to search for php files and generates $wgAutoloadLocalClasses...
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.