MediaWiki  1.34.0
AutoloadGenerator Class Reference

Accepts a list of files and directories to search for php files and generates $wgAutoloadLocalClasses or $wgAutoloadClasses lines for all detected classes. More...

Collaboration diagram for AutoloadGenerator:

Public Member Functions

 __construct ( $basepath, $flags=[])
 
 forceClassPath ( $fqcn, $inputPath)
 Force a class to be autoloaded from a specific path, regardless of where or if it was detected. More...
 
 getAutoload ( $commandName='AutoloadGenerator')
 Returns all known classes as a string, which can be used to put into a target file (e.g. More...
 
 getTargetFileinfo ()
 Returns the filename of the extension.json of skin.json, if there's any, or otherwise the path to the autoload.php file in an array as the "filename" key and with the type (AutoloadGenerator::FILETYPE_JSON or AutoloadGenerator::FILETYPE_PHP) of the file as the "type" key. More...
 
 initMediaWikiDefault ()
 Initialize the source files and directories which are used for the MediaWiki default autoloader in {mw-base-dir}/autoload.php including: More...
 
 readDir ( $dir)
 
 readFile ( $inputPath)
 
 setExcludePaths (array $paths)
 Directories that should be excluded. More...
 
 setPsr4Namespaces (array $namespaces)
 Set PSR4 namespaces. More...
 

Public Attributes

const FILETYPE_JSON = 'json'
 
const FILETYPE_PHP = 'php'
 

Protected Member Functions

 generateJsonAutoload ( $filename)
 Updates the AutoloadClasses field at the given filename. More...
 
 generatePHPAutoload ( $commandName, $filename)
 Generates a PHP file setting up autoload information. More...
 

Static Protected Member Functions

static normalizePathSeparator ( $path)
 Ensure that Unix-style path separators ("/") are used in the path. More...
 

Protected Attributes

string $basepath
 Root path of the project being scanned for classes. More...
 
array $classes = []
 Map of file shortpath to list of FQCN detected within file. More...
 
ClassCollector $collector
 Helper class extracts class names from php files. More...
 
string[] $excludePaths = []
 Directories that should be excluded. More...
 
array $overrides = []
 Map of FQCN to relative path(from self::$basepath) More...
 
string[] $psr4Namespaces = []
 Configured PSR4 namespaces. More...
 
string $variableName = 'wgAutoloadClasses'
 The global variable to write output to. More...
 

Private Member Functions

 shouldExclude ( $path)
 Whether the file should be excluded. More...
 

Detailed Description

Accepts a list of files and directories to search for php files and generates $wgAutoloadLocalClasses or $wgAutoloadClasses lines for all detected classes.

These lines are written out to an autoload.php file in the projects provided basedir.

Usage:

$gen = new AutoloadGenerator( __DIR__ );
$gen->readDir( __DIR__ . '/includes' );
$gen->readFile( __DIR__ . '/foo.php' )
$gen->getAutoload();

Definition at line 34 of file AutoloadGenerator.php.

Constructor & Destructor Documentation

◆ __construct()

AutoloadGenerator::__construct (   $basepath,
  $flags = [] 
)
Parameters
string$basepathRoot path of the project being scanned for classes
array | string$flagslocal - If this flag is set $wgAutoloadLocalClasses will be build instead of $wgAutoloadClasses

Definition at line 84 of file AutoloadGenerator.php.

References $basepath, and normalizePathSeparator().

Member Function Documentation

◆ forceClassPath()

AutoloadGenerator::forceClassPath (   $fqcn,
  $inputPath 
)

Force a class to be autoloaded from a specific path, regardless of where or if it was detected.

Parameters
string$fqcnFQCN to force the location of
string$inputPathFull path to the file containing the class
Exceptions
Exception

Definition at line 147 of file AutoloadGenerator.php.

References $path, and normalizePathSeparator().

◆ generateJsonAutoload()

AutoloadGenerator::generateJsonAutoload (   $filename)
protected

Updates the AutoloadClasses field at the given filename.

Parameters
string$filenameFilename of JSON extension/skin registration file
Returns
string Updated Json of the file given as the $filename parameter

Definition at line 230 of file AutoloadGenerator.php.

References $path, FormatJson\ALL_OK, FormatJson\decode(), and FormatJson\encode().

Referenced by getAutoload().

◆ generatePHPAutoload()

AutoloadGenerator::generatePHPAutoload (   $commandName,
  $filename 
)
protected

Generates a PHP file setting up autoload information.

Parameters
string$commandNameCommand name to include in comment
string$filenameof PHP file to put autoload information in.
Returns
string

Definition at line 261 of file AutoloadGenerator.php.

References $content, $output, and $path.

Referenced by getAutoload().

◆ getAutoload()

AutoloadGenerator::getAutoload (   $commandName = 'AutoloadGenerator')

Returns all known classes as a string, which can be used to put into a target file (e.g.

extension.json, skin.json or autoload.php)

Parameters
string$commandNameValue used in file comment to direct developers towards the appropriate way to update the autoload.
Returns
string

Definition at line 320 of file AutoloadGenerator.php.

References $fileinfo, generateJsonAutoload(), generatePHPAutoload(), and getTargetFileinfo().

◆ getTargetFileinfo()

AutoloadGenerator::getTargetFileinfo ( )

Returns the filename of the extension.json of skin.json, if there's any, or otherwise the path to the autoload.php file in an array as the "filename" key and with the type (AutoloadGenerator::FILETYPE_JSON or AutoloadGenerator::FILETYPE_PHP) of the file as the "type" key.

Returns
array

Definition at line 341 of file AutoloadGenerator.php.

References $fileinfo, FILETYPE_JSON, and FILETYPE_PHP.

Referenced by getAutoload().

◆ initMediaWikiDefault()

AutoloadGenerator::initMediaWikiDefault ( )

Initialize the source files and directories which are used for the MediaWiki default autoloader in {mw-base-dir}/autoload.php including:

  • includes/
  • languages/
  • maintenance/
  • mw-config/
  • /*.php

Definition at line 380 of file AutoloadGenerator.php.

References $file, readDir(), and readFile().

◆ normalizePathSeparator()

static AutoloadGenerator::normalizePathSeparator (   $path)
staticprotected

Ensure that Unix-style path separators ("/") are used in the path.

Parameters
string$path
Returns
string

Definition at line 367 of file AutoloadGenerator.php.

References $path.

Referenced by __construct(), forceClassPath(), readFile(), and setExcludePaths().

◆ readDir()

AutoloadGenerator::readDir (   $dir)
Parameters
string$dirPath to a directory to recursively search for php files with either .php or .inc extensions

Definition at line 208 of file AutoloadGenerator.php.

References $ext, $file, $path, and readFile().

Referenced by initMediaWikiDefault().

◆ readFile()

AutoloadGenerator::readFile (   $inputPath)
Parameters
string$inputPathPath to a php file to find classes within
Exceptions
Exception

Definition at line 164 of file AutoloadGenerator.php.

References normalizePathSeparator(), and shouldExclude().

Referenced by initMediaWikiDefault(), and readDir().

◆ setExcludePaths()

AutoloadGenerator::setExcludePaths ( array  $paths)

Directories that should be excluded.

Since
1.31
Parameters
string[]$paths

Definition at line 101 of file AutoloadGenerator.php.

References $path, and normalizePathSeparator().

◆ setPsr4Namespaces()

AutoloadGenerator::setPsr4Namespaces ( array  $namespaces)

Set PSR4 namespaces.

Unlike self::setExcludePaths(), this will only skip outputting the autoloader entry when the namespace matches the path.

Since
1.32
Parameters
string[]$namespacesAssociative array mapping namespace to path

Definition at line 116 of file AutoloadGenerator.php.

References $path.

◆ shouldExclude()

AutoloadGenerator::shouldExclude (   $path)
private

Whether the file should be excluded.

Parameters
string$pathFile path
Returns
bool

Definition at line 129 of file AutoloadGenerator.php.

References $path.

Referenced by readFile().

Member Data Documentation

◆ $basepath

string AutoloadGenerator::$basepath
protected

Root path of the project being scanned for classes.

Definition at line 41 of file AutoloadGenerator.php.

Referenced by __construct().

◆ $classes

array AutoloadGenerator::$classes = []
protected

Map of file shortpath to list of FQCN detected within file.

Definition at line 51 of file AutoloadGenerator.php.

◆ $collector

ClassCollector AutoloadGenerator::$collector
protected

Helper class extracts class names from php files.

Definition at line 46 of file AutoloadGenerator.php.

◆ $excludePaths

string [] AutoloadGenerator::$excludePaths = []
protected

Directories that should be excluded.

Definition at line 68 of file AutoloadGenerator.php.

◆ $overrides

array AutoloadGenerator::$overrides = []
protected

Map of FQCN to relative path(from self::$basepath)

Definition at line 61 of file AutoloadGenerator.php.

◆ $psr4Namespaces

string [] AutoloadGenerator::$psr4Namespaces = []
protected

Configured PSR4 namespaces.

namespace => path

Definition at line 75 of file AutoloadGenerator.php.

◆ $variableName

string AutoloadGenerator::$variableName = 'wgAutoloadClasses'
protected

The global variable to write output to.

Definition at line 56 of file AutoloadGenerator.php.

◆ FILETYPE_JSON

const AutoloadGenerator::FILETYPE_JSON = 'json'

Definition at line 35 of file AutoloadGenerator.php.

Referenced by getTargetFileinfo().

◆ FILETYPE_PHP

const AutoloadGenerator::FILETYPE_PHP = 'php'

Definition at line 36 of file AutoloadGenerator.php.

Referenced by getTargetFileinfo().


The documentation for this class was generated from the following file: