From bc6770e7ab7d8c1fa8ec9c92b3f05ba7bace3d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=9F=8E=E9=93=AD?= Date: Thu, 18 Apr 2019 21:50:05 +0800 Subject: [PATCH] fix aop --- src/Aop/Aop.php | 6 +++++- src/Bean/Container.php | 12 +++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Aop/Aop.php b/src/Aop/Aop.php index 886e6057..e253d1bc 100644 --- a/src/Aop/Aop.php +++ b/src/Aop/Aop.php @@ -185,9 +185,11 @@ private function doPoint( * @param string $class Class name * @param string $method Method name * @param array $annotations The annotations of method + * @return bool */ - public function match(string $beanName, string $class, string $method, array $annotations) + public function match(string $beanName, string $class, string $method, array $annotations): bool { + $shouldProxy = false; foreach ($this->aspects as $aspectClass => $aspect) { if (!isset($aspect['point'], $aspect['advice'])) { continue; @@ -209,8 +211,10 @@ public function match(string $beanName, string $class, string $method, array $an if ($includeMath && ! $excludeMath) { $this->map[$class][$method][] = $aspect['advice']; + $shouldProxy = true; } } + return $shouldProxy; } /** diff --git a/src/Bean/Container.php b/src/Bean/Container.php index e1bcc9ac..e1216396 100644 --- a/src/Bean/Container.php +++ b/src/Bean/Container.php @@ -250,20 +250,22 @@ private function set(string $name, ObjectDefinition $objectDefinition) */ private function proxyBean(string $name, string $className, $object) { - if (!AspectCollector::hasClassname($className)) { - return $object; - } - /* @var Aop $aop */ $aop = App::getBean(Aop::class); $rc = new \ReflectionClass($className); $rms = $rc->getMethods(); + + $shouldProxy = false; foreach ($rms as $rm) { $method = $rm->getName(); $annotations = Collector::$methodAnnotations[$className][$method] ?? []; $annotations = array_unique($annotations); - $aop->match($name, $className, $method, $annotations); + $shouldProxy |= $aop->match($name, $className, $method, $annotations); + } + + if (! $shouldProxy) { + return $object; } $handler = new AopHandler($object);