From ea844c33172168282cd56f2a68344483409a93db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 19 Apr 2023 12:35:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Container.php | 18 ++++++++++++++---- Interface/InjectProxyInterface.php | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 Interface/InjectProxyInterface.php diff --git a/Container.php b/Container.php index 3a56d5a..1f538c4 100644 --- a/Container.php +++ b/Container.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Kiri\Di; +use Kiri\Di\Interface\InjectProxyInterface; use Kiri\Router\Interface\ValidatorInterface; use Psr\Container\ContainerInterface; use ReflectionClass; @@ -185,7 +186,11 @@ class Container implements ContainerInterface if (!class_exists($attribute->getName())) { continue; } - $attribute->newInstance()->dispatch($object); + if ($object instanceof InjectProxyInterface) { + $attribute->newInstance()->dispatch($reflect->getFileName(), $object); + } else { + $attribute->newInstance()->dispatch($object); + } } $this->resolveProperties($reflect, $object); @@ -204,15 +209,20 @@ class Container implements ContainerInterface public function resolveProperties(ReflectionClass $getReflectionClass, object $class): void { $properties = $getReflectionClass->getProperties(); + + $filename = $getReflectionClass->getFileName(); foreach ($properties as $property) { $propertyAttributes = $property->getAttributes(); - foreach ($propertyAttributes as $attribute) { if (!class_exists($attribute->getName()) || in_array(ValidatorInterface::class, class_implements($attribute->getName()))) { continue; } - $attribute->newInstance()->dispatch($class, $property->getName()); + if ($class instanceof InjectProxyInterface) { + $attribute->newInstance()->dispatch($filename, $class, $property->getName()); + } else { + $attribute->newInstance()->dispatch($class, $property->getName()); + } } } } @@ -280,7 +290,7 @@ class Container implements ContainerInterface * @return array * @throws ReflectionException */ - private function resolveMethodParams(ReflectionMethod|ReflectionFunction $parameters): array + public function resolveMethodParams(ReflectionMethod|ReflectionFunction $parameters): array { $params = []; if ($parameters->getNumberOfParameters() < 1) { diff --git a/Interface/InjectProxyInterface.php b/Interface/InjectProxyInterface.php new file mode 100644 index 0000000..3d9aad3 --- /dev/null +++ b/Interface/InjectProxyInterface.php @@ -0,0 +1,17 @@ +