35 private const FILETYPE_JSON =
'json';
36 private const FILETYPE_PHP =
'php';
85 if ( !is_array( $flags ) ) {
90 if ( in_array(
'local', $flags ) ) {
91 $this->variableName =
'wgAutoloadLocalClasses';
102 foreach ( $paths as
$path ) {
116 foreach ( $namespaces as $ns =>
$path ) {
117 $ns = rtrim( $ns,
'\\' ) .
'\\';
118 $this->psr4Namespaces[$ns] = rtrim( self::normalizePathSeparator(
$path ),
'/' );
128 private function shouldExclude(
$path ) {
129 foreach ( $this->excludePaths as $dir ) {
130 if ( str_starts_with(
$path, $dir ) ) {
149 throw new \Exception(
"Invalid path: $inputPath" );
151 if ( !str_starts_with(
$path, $this->basepath ) ) {
152 throw new \Exception(
"Path is not within basepath: $inputPath" );
154 $shortpath = substr(
$path, strlen( $this->basepath ) );
155 $this->overrides[$fqcn] = $shortpath;
167 $len = strlen( $this->basepath );
168 if ( !str_starts_with( $inputPath, $this->basepath ) ) {
169 throw new \Exception(
"Path is not within basepath: $inputPath" );
171 if ( $this->shouldExclude( $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 $shortpath = substr( $inputPath, $len );
193 $this->classes[$shortpath] = $result;
201 $it =
new RecursiveDirectoryIterator(
202 self::normalizePathSeparator( realpath( $dir ) ) );
203 $it =
new RecursiveIteratorIterator( $it );
206 if ( pathinfo(
$path, PATHINFO_EXTENSION ) ===
'php' ) {
221 $key =
'AutoloadClasses';
222 $json = FormatJson::decode( file_get_contents( $filename ),
true );
223 unset( $json[$key] );
226 foreach ( $this->classes as
$path => $contained ) {
227 foreach ( $contained as $fqcn ) {
229 $json[$key][$fqcn] = substr(
$path, 1 );
232 foreach ( $this->overrides as
$path => $fqcn ) {
234 $json[$key][$fqcn] = substr(
$path, 1 );
238 ksort( $json[$key] );
241 return FormatJson::encode( $json,
"\t", FormatJson::ALL_OK ) .
"\n";
257 $format =
"%s => __DIR__ . %s,";
258 foreach ( $this->classes as
$path => $contained ) {
259 $exportedPath = var_export(
$path,
true );
260 foreach ( $contained as $fqcn ) {
263 var_export( $fqcn,
true ),
269 foreach ( $this->overrides as $fqcn =>
$path ) {
272 var_export( $fqcn,
true ),
273 var_export(
$path,
true )
282 if ( $this->variableName ===
'wgAutoloadClasses' ) {
288 $output = implode(
"\n\t",
$content );
293global \${$this->variableName};
295\${$this->variableName} {$op} [
310 public function getAutoload( $commandName =
'AutoloadGenerator' ) {
316 if (
$fileinfo[
'type'] === self::FILETYPE_JSON ) {
332 if ( file_exists( $this->basepath .
'/extension.json' ) ) {
334 'filename' => $this->basepath .
'/extension.json',
335 'type' => self::FILETYPE_JSON
338 if ( file_exists( $this->basepath .
'/skin.json' ) ) {
340 'filename' => $this->basepath .
'/skin.json',
341 'type' => self::FILETYPE_JSON
346 'filename' => $this->basepath .
'/autoload.php',
347 'type' => self::FILETYPE_PHP
358 return str_replace(
'\\',
'/',
$path );
371 foreach ( [
'includes',
'languages',
'maintenance',
'mw-config' ] as $dir ) {
372 $this->
readDir( $this->basepath .
'/' . $dir );
374 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.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.