0 follower

Final Class Yiisoft\Yii\Gii\Helper

InheritanceYiisoft\Yii\Gii\Helper

Public Methods

Hide inherited methods

Method Description Defined By
getNamespacePath() Returns the file path matching the given namespace. Yiisoft\Yii\Gii\Helper

Method Details

Hide inherited methods

getNamespacePath() public static method

Returns the file path matching the given namespace.

public static string getNamespacePath ( string $namespace )
$namespace string

Namespace.

return string

File path.

                public static function getNamespacePath(string $namespace): string
{
    $classLoaderReflection = new ReflectionClass(ClassLoader::class);
    $vendorDir = dirname($classLoaderReflection->getFileName(), 2);
    $namespaceWithBackSlash = "$namespace\\";
    /**
     * @psalm-suppress UnresolvableInclude
     * @psalm-var array<string, list<string>> $map
     */
    $map = require "$vendorDir/composer/autoload_psr4.php";
    foreach ($map as $mapNamespace => $mapDirectories) {
        if (str_starts_with($namespaceWithBackSlash, trim($mapNamespace, '\\') . '\\')) {
            /** @var string $mapDirectory */
            $mapDirectory = reset($mapDirectories);
            return $mapDirectory . '/' . str_replace('\\', '/', substr($namespace, strlen($mapNamespace)));
        }
    }
    throw new LogicException("Invalid namespace: \"$namespace\".");
}