diff --git a/config/aop.php b/config/aop.php index 4d76cc4..a578717 100644 --- a/config/aop.php +++ b/config/aop.php @@ -2,8 +2,8 @@ return [ 'aspects' => [], - 'includes' => [], - 'excludes' => [], + 'proxy_dirs' => [], + 'except_dirs' => [], 'cacheable' => false, 'storage_dir' => sys_get_temp_dir(), ]; diff --git a/src/AopClassLoader.php b/src/AopClassLoader.php index 312bb4d..696282f 100644 --- a/src/AopClassLoader.php +++ b/src/AopClassLoader.php @@ -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; } @@ -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; } } @@ -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; } } diff --git a/src/AopConfig.php b/src/AopConfig.php index e9e0215..5c5d2f5 100644 --- a/src/AopConfig.php +++ b/src/AopConfig.php @@ -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 = '', ) @@ -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; } /**