Skip to content

Commit

Permalink
Parse namespace to dir path according to composer settings (swoft-clo…
Browse files Browse the repository at this point in the history
…ud/swoft-component#103)

* Parse namespace to dir path according to composer settings
* Update ComposerHelper.php
  • Loading branch information
huangzhhui committed Jun 27, 2018
1 parent 290a2f4 commit e55fae5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Bean/Resource/AnnotationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -189,8 +188,6 @@ public function parseAnnotationsData()
}

/**
* 添加扫描namespace
*
* @param array $namespaces
*/
public function addScanNamespace(array $namespaces)
Expand All @@ -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();
Expand Down
68 changes: 68 additions & 0 deletions src/Helper/ComposerHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Swoft\Helper;


use Composer\Autoload\ClassLoader;

class ComposerHelper
{

/**
* @var ClassLoader|mixed
*/
static $loader;

/**
* @return ClassLoader
*/
public static function getLoader(): ClassLoader
{
if (! self::$loader) {
$loader = self::findLoader();
$loader instanceof ClassLoader && self::$loader = $loader;
}
return self::$loader;
}

/**
* @return ClassLoader
* @throws \RuntimeException When Composer loader not found
*/
public static function findLoader(): ClassLoader
{
$composerClass = '';
foreach (get_declared_classes() as $declaredClass) {
if (StringHelper::startsWith($declaredClass, 'ComposerAutoloaderInit') && method_exists($declaredClass, 'getLoader')) {
$composerClass = $declaredClass;
break;
}
}
if (! $composerClass) {
throw new \RuntimeException('Composer loader not found.');
}
return $composerClass::getLoader();
}

/**
* @param string $namespace
* @return string
*/
public static function getDirByNamespace(string $namespace): string
{
$dir = '';
$loader = self::findLoader();
$prefixesPsr4 = $loader->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;
}

}

0 comments on commit e55fae5

Please sign in to comment.