X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Futils%2FAutoloadGenerator.php;h=2fc7bc0645d2238333a23754ef81e367e987db01;hb=ac0ab2c03a12756b0965f250c174e228348ec459;hp=511b67346b93f48499b3a1d574ab1249eb7fd898;hpb=e548e0f35c4b47a0de4903e8a3758c2b2d9e2712;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/utils/AutoloadGenerator.php b/includes/utils/AutoloadGenerator.php index 511b67346b..2fc7bc0645 100644 --- a/includes/utils/AutoloadGenerator.php +++ b/includes/utils/AutoloadGenerator.php @@ -49,6 +49,13 @@ class AutoloadGenerator { */ 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 @@ -79,6 +86,22 @@ class AutoloadGenerator { } } + /** + * 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 * @@ -135,6 +158,25 @@ class AutoloadGenerator { $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;