*/
protected $excludePaths = [];
+ /**
+ * Configured PSR4 namespaces
+ *
+ * @var string[] namespace => path
+ */
+ protected $psr4Namespaces = [];
+
/**
* @param string $basepath Root path of the project being scanned for classes
* @param array|string $flags
}
}
+ /**
+ * Set PSR4 namespaces
+ *
+ * Unlike self::setExcludePaths(), this will only skip outputting the
+ * autoloader entry when the namespace matches the path.
+ *
+ * @since 1.32
+ * @param string[] $namespaces Associative array mapping namespace to path
+ */
+ public function setPsr4Namespaces( array $namespaces ) {
+ foreach ( $namespaces as $ns => $path ) {
+ $ns = rtrim( $ns, '\\' ) . '\\';
+ $this->psr4Namespaces[$ns] = rtrim( self::normalizePathSeparator( $path ), '/' );
+ }
+ }
+
/**
* Whether the file should be excluded
*
$result = $this->collector->getClasses(
file_get_contents( $inputPath )
);
+
+ // Filter out classes that will be found by PSR4
+ $result = array_filter( $result, function ( $class ) use ( $inputPath ) {
+ $parts = explode( '\\', $class );
+ for ( $i = count( $parts ) - 1; $i > 0; $i-- ) {
+ $ns = implode( '\\', array_slice( $parts, 0, $i ) ) . '\\';
+ if ( isset( $this->psr4Namespaces[$ns] ) ) {
+ $expectedPath = $this->psr4Namespaces[$ns] . '/'
+ . implode( '/', array_slice( $parts, $i ) )
+ . '.php';
+ if ( $inputPath === $expectedPath ) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+ } );
+
if ( $result ) {
$shortpath = substr( $inputPath, $len );
$this->classes[$shortpath] = $result;
$base = dirname( __DIR__ );
$generator = new AutoloadGenerator( $base, 'local' );
-$generator->setExcludePaths( array_values( AutoLoader::getAutoloadNamespaces() ) );
+$generator->setPsr4Namespaces( AutoLoader::getAutoloadNamespaces() );
$generator->initMediaWikiDefault();
// Write out the autoload
$expected = $wgAutoloadLocalClasses + $wgAutoloadClasses;
$actual = [];
+ $psr4Namespaces = [];
+ foreach ( AutoLoader::getAutoloadNamespaces() as $ns => $path ) {
+ $psr4Namespaces[rtrim( $ns, '\\' ) . '\\'] = rtrim( $path, '/' );
+ }
+
$files = array_unique( $expected );
foreach ( $files as $class => $file ) {
list( $classesInFile, $aliasesInFile ) = self::parseFile( $contents );
foreach ( $classesInFile as $className => $ignore ) {
+ // Skip if it's a PSR4 class
+ $parts = explode( '\\', $className );
+ for ( $i = count( $parts ) - 1; $i > 0; $i-- ) {
+ $ns = implode( '\\', array_slice( $parts, 0, $i ) ) . '\\';
+ if ( isset( $psr4Namespaces[$ns] ) ) {
+ $expectedPath = $psr4Namespaces[$ns] . '/'
+ . implode( '/', array_slice( $parts, $i ) )
+ . '.php';
+ if ( $filePath === $expectedPath ) {
+ continue 2;
+ }
+ }
+ }
+
+ // Nope, add it.
$actual[$className] = $file;
}
$path = realpath( __DIR__ . '/../../..' );
$oldAutoload = file_get_contents( $path . '/autoload.php' );
$generator = new AutoloadGenerator( $path, 'local' );
- $generator->setExcludePaths( array_values( AutoLoader::getAutoloadNamespaces() ) );
+ $generator->setPsr4Namespaces( AutoLoader::getAutoloadNamespaces() );
$generator->initMediaWikiDefault();
$newAutoload = $generator->getAutoload( 'maintenance/generateLocalAutoload.php' );