From d5a6b276fcab7c38f1657ad304780a2207258387 Mon Sep 17 00:00:00 2001 From: Iman Ghafoori Date: Thu, 27 May 2021 17:03:58 +0430 Subject: [PATCH] bug fix --- src/Analyzers/ComposerJson.php | 6 +++--- src/Analyzers/NamespaceCorrector.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Analyzers/ComposerJson.php b/src/Analyzers/ComposerJson.php index 5ad11809..e48799f6 100644 --- a/src/Analyzers/ComposerJson.php +++ b/src/Analyzers/ComposerJson.php @@ -10,13 +10,13 @@ class ComposerJson public static function readKey($key, $composerPath = null) { - $path = $composerPath ?: app()->basePath(); + $path = $composerPath ?: ''; if (isset(self::$result[$path][$key])) { return self::$result[$path][$key]; } - $composer = \json_decode(\file_get_contents($path.DIRECTORY_SEPARATOR.'composer.json'), true); + $composer = \json_decode(\file_get_contents(app()->basePath($path.'composer.json')), true); $value = (array) data_get($composer, $key, []); @@ -45,7 +45,7 @@ public static function readAutoload() $res = []; foreach ($composers as $path) { // We avoid autoload-dev for repositories. - $res = $res + self::readKey('autoload.psr-4', app()->basePath($path)); + $res = $res + self::readKey('autoload.psr-4', $path); } // add the root composer.json diff --git a/src/Analyzers/NamespaceCorrector.php b/src/Analyzers/NamespaceCorrector.php index 16e3fd2e..cf75ad1c 100644 --- a/src/Analyzers/NamespaceCorrector.php +++ b/src/Analyzers/NamespaceCorrector.php @@ -66,4 +66,22 @@ public static function getRelativePathFromNamespace($namespace) return \str_replace(['\\', '/'], DIRECTORY_SEPARATOR, \str_replace($namespaces, $paths, $namespace)); } + + public static function getNamespaceFromRelativePath($relPath) + { + // Remove .php from class path + $relPath = str_replace([base_path(), '.php'], '', $relPath); + + $autoload = ComposerJson::readAutoload(); + uksort($autoload, function ($a, $b) { + return strlen($b) <=> strlen($a); + }); + + $namespaces = array_keys($autoload); + $paths = array_values($autoload); + + $relPath = \str_replace('\\', '/', $relPath); + + return trim(\str_replace('/', '\\', \str_replace($paths, $namespaces, $relPath)), '\\'); + } }