85 if ( !is_array( $flags ) ) {
90 if ( in_array(
'local', $flags ) ) {
91 $this->variableName =
'wgAutoloadLocalClasses';
102 foreach ( $paths as
$path ) {
117 foreach ( $namespaces as $ns =>
$path ) {
118 $ns = rtrim( $ns,
'\\' ) .
'\\';
119 $this->psr4Namespaces[$ns] = rtrim( self::normalizePathSeparator(
$path ),
'/' );
130 foreach ( $this->excludePaths as $dir ) {
131 if ( strpos(
$path, $dir ) === 0 ) {
150 throw new \Exception(
"Invalid path: $inputPath" );
152 $len = strlen( $this->basepath );
153 if ( substr(
$path, 0, $len ) !== $this->basepath ) {
154 throw new \Exception(
"Path is not within basepath: $inputPath" );
156 $shortpath = substr(
$path, $len );
157 $this->overrides[$fqcn] = $shortpath;
169 $len = strlen( $this->basepath );
170 if ( substr( $inputPath, 0, $len ) !== $this->basepath ) {
171 throw new \Exception(
"Path is not within basepath: $inputPath" );
176 $result = $this->collector->getClasses(
177 file_get_contents( $inputPath )
181 $result = array_filter( $result,
function ( $class ) use ( $inputPath ) {
182 $parts = explode(
'\\', $class );
183 for ( $i = count( $parts ) - 1; $i > 0; $i-- ) {
184 $ns = implode(
'\\', array_slice( $parts, 0, $i ) ) .
'\\';
185 if ( isset( $this->psr4Namespaces[$ns] ) ) {
186 $expectedPath = $this->psr4Namespaces[$ns] .
'/'
187 . implode(
'/', array_slice( $parts, $i ) )
189 if ( $inputPath === $expectedPath ) {
199 $shortpath = substr( $inputPath, $len );
200 $this->classes[$shortpath] = $result;
209 $it =
new RecursiveDirectoryIterator(
210 self::normalizePathSeparator( realpath( $dir ) ) );
211 $it =
new RecursiveIteratorIterator( $it );
214 $ext = pathinfo(
$path, PATHINFO_EXTENSION );
216 if (
$ext ===
'php' ||
$ext ===
'inc' ) {
231 $key =
'AutoloadClasses';
232 $json = FormatJson::decode( file_get_contents( $filename ),
true );
233 unset( $json[$key] );
236 foreach ( $this->classes as
$path => $contained ) {
237 foreach ( $contained as $fqcn ) {
239 $json[$key][$fqcn] = substr(
$path, 1 );
242 foreach ( $this->overrides as
$path => $fqcn ) {
244 $json[$key][$fqcn] = substr(
$path, 1 );
248 ksort( $json[$key] );
251 return FormatJson::encode( $json,
"\t", FormatJson::ALL_OK ) .
"\n";
267 $format =
"%s => __DIR__ . %s,";
268 foreach ( $this->classes as
$path => $contained ) {
269 $exportedPath = var_export(
$path,
true );
270 foreach ( $contained as $fqcn ) {
273 var_export( $fqcn,
true ),
279 foreach ( $this->overrides as $fqcn =>
$path ) {
282 var_export( $fqcn,
true ),
283 var_export(
$path,
true )
292 if ( $this->variableName ===
'wgAutoloadClasses' ) {
298 $output = implode(
"\n\t",
$content );
303global \${$this->variableName};
305\${$this->variableName} {$op} [
320 public function getAutoload( $commandName =
'AutoloadGenerator' ) {
326 if (
$fileinfo[
'type'] === self::FILETYPE_JSON ) {
342 if ( file_exists( $this->basepath .
'/extension.json' ) ) {
344 'filename' => $this->basepath .
'/extension.json',
348 if ( file_exists( $this->basepath .
'/skin.json' ) ) {
350 'filename' => $this->basepath .
'/skin.json',
356 'filename' => $this->basepath .
'/autoload.php',
368 return str_replace(
'\\',
'/',
$path );
381 foreach ( [
'includes',
'languages',
'maintenance',
'mw-config' ] as $dir ) {
382 $this->
readDir( $this->basepath .
'/' . $dir );
384 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)
Set PSR4 namespaces.
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.
if(!is_readable( $file)) $ext