Skip to content

Commit

Permalink
feature:not need scan now, just config.
Browse files Browse the repository at this point in the history
  • Loading branch information
陆云峰 committed Jun 24, 2021
1 parent 43984ec commit ff0b147
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 49 deletions.
4 changes: 2 additions & 2 deletions config/aop.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

return [
'aspects' => [],
'includes' => [],
'excludes' => [],
'proxy_dirs' => [],
'except_dirs' => [],
'cacheable' => false,
'storage_dir' => sys_get_temp_dir(),
];
62 changes: 21 additions & 41 deletions src/AopClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,15 @@ public static function init(array $config): void
*/
private function lazyLoad($file): string
{
$fileInfo = new SplFileInfo($file);
// 非指定代理文件,直接返回
if (self::isExcept($fileInfo->getRealPath()) || !self::isProxy($fileInfo->getRealPath())) {
return $file;
}

// 实例化代理(在初始化时生成代理代码的过程中,源代码相关信息会被存储在访客节点类中)
$proxy = new Proxy(new SplFileInfo($file), [$classVisitor = new ClassVisitor(), new MethodVisitor()]);
// 无须代理文件,直接返回
$proxy = new Proxy($fileInfo, [$classVisitor = new ClassVisitor(), new MethodVisitor()]);
// 无须代理的文件类型,直接返回
if ($classVisitor->isInterface()) {
return $file;
}
Expand All @@ -84,25 +90,19 @@ public function loadClass(string $class): void
*/
private function findFile(string $class): string
{
if (isset($this->classMap[$class])) {
return $this->classMap[$class];
} else {
// find file from composer loader.
$file = $this->composerLoader->findFile($class);

return self::isExclude($class) || !self::isInclude($class) ? $file : $this->lazyLoad($file);
}
return $this->classMap[$class] ?? $this->lazyLoad($this->composerLoader->findFile($class));
}

/**
* @param string $class
* @param string $fileRealPath
* @return bool
*/
private static function isExclude(string $class): bool
private static function isExcept(string $fileRealPath): bool
{
$excludes = AopConfig::instance()->getExcludes();
foreach ($excludes as $exclude) {
if (self::namespaceMatch($exclude, $class)) {
$exceptDirs = AopConfig::instance()->getExceptDirs();
array_unshift($exceptDirs, [dirname(__DIR__)]);
foreach ($exceptDirs as $exceptDir) {
if (str_starts_with($fileRealPath, $exceptDir)) {
return true;
}
}
Expand All @@ -111,38 +111,18 @@ private static function isExclude(string $class): bool
}

/**
* @param string $class
* @param string $fileRealPath
* @return bool
*/
private static function isInclude(string $class): bool
private static function isProxy(string $fileRealPath): bool
{
$includes = AopConfig::instance()->getIncludes();
foreach ($includes as $include) {
if (self::namespaceMatch($include, $class)) {
$proxyDirs = AopConfig::instance()->getProxyDirs();
foreach ($proxyDirs as $proxyDir) {
if (str_starts_with($fileRealPath, $proxyDir)) {
return true;
}
}

return empty($includes);
}

/**
* @param string $rule
* @param string $class
* @return bool
*/
private static function namespaceMatch(string $rule, string $class): bool
{
$strBefore = function (string $subject, string $search): string {
if ($search === '') {
return $subject;
}

$result = strstr($subject, (string)$search, true);

return $result === false ? $subject : $result;
};

return str_starts_with($strBefore($rule, '*'), $class);
return false;
}
}
12 changes: 6 additions & 6 deletions src/AopConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class AopConfig

private function __construct(
private array $aspects = [],
private array $includes = [],
private array $excludes = [],
private array $proxyDirs = [],
private array $exceptDirs = [],
private bool $cacheable = false,
private string $storageDir = '',
)
Expand Down Expand Up @@ -44,17 +44,17 @@ public function getAspects(): array
/**
* @return array
*/
public function getIncludes(): array
public function getProxyDirs(): array
{
return $this->includes;
return $this->proxyDirs;
}

/**
* @return array
*/
public function getExcludes(): array
public function getExceptDirs(): array
{
return $this->excludes;
return $this->exceptDirs;
}

/**
Expand Down

0 comments on commit ff0b147

Please sign in to comment.