From 856839336ba13a357eb91b2a187e1c145529e23c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Wed, 27 Jun 2018 17:45:13 +0800 Subject: [PATCH] Parse namespace to dir path according to composer settings (https://github.com/swoft-cloud/swoft-component/pull/103) * Parse namespace to dir path according to composer settings * Update ComposerHelper.php --- .../src/Bean/Resource/AnnotationResource.php | 15 ++-- src/framework/src/Helper/ComposerHelper.php | 68 +++++++++++++++++++ 2 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 src/framework/src/Helper/ComposerHelper.php diff --git a/src/framework/src/Bean/Resource/AnnotationResource.php b/src/framework/src/Bean/Resource/AnnotationResource.php index bab52f5ba..2d6d72d66 100644 --- a/src/framework/src/Bean/Resource/AnnotationResource.php +++ b/src/framework/src/Bean/Resource/AnnotationResource.php @@ -5,8 +5,7 @@ use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\AnnotationRegistry; use Swoft\Bean\Wrapper\WrapperInterface; -use Swoft\Helper\ComponentHelper; -use Swoft\Helper\JsonHelper; +use Swoft\Helper\ComposerHelper; /** * Annotation resource @@ -189,8 +188,6 @@ public function parseAnnotationsData() } /** - * 添加扫描namespace - * * @param array $namespaces */ public function addScanNamespace(array $namespaces) @@ -200,10 +197,12 @@ public function addScanNamespace(array $namespaces) $this->scanNamespaces[$key] = $namespace; continue; } - - $nsPath = str_replace("\\", "/", $namespace); - $nsPath = str_replace('App/', 'app/', $nsPath); - $this->scanNamespaces[$namespace] = BASE_PATH . "/" . $nsPath; + $nsPath = ComposerHelper::getDirByNamespace($namespace); + if (!$nsPath) { + $nsPath = str_replace("\\", "/", $namespace); + $nsPath = BASE_PATH . "/" . $nsPath; + } + $this->scanNamespaces[$namespace] = $nsPath; } $this->registerNamespace(); diff --git a/src/framework/src/Helper/ComposerHelper.php b/src/framework/src/Helper/ComposerHelper.php new file mode 100644 index 000000000..9ccde34b7 --- /dev/null +++ b/src/framework/src/Helper/ComposerHelper.php @@ -0,0 +1,68 @@ +getPrefixesPsr4(); + $maxLength = 0; + foreach ($prefixesPsr4 as $prefix => $path) { + if (StringHelper::startsWith($namespace, $prefix)) { + $strLen = strlen($prefix); + if ($strLen > $maxLength) { + $dir = current($path) . DIRECTORY_SEPARATOR . substr($namespace, $strLen); + } + } + } + return $dir; + } + +}