This commit is contained in:
2023-12-18 22:23:16 +08:00
parent c6e9133566
commit ae8ba9ca4e
5 changed files with 43 additions and 18 deletions
+4 -10
View File
@@ -13,6 +13,7 @@ namespace Kiri\Di;
use Closure; use Closure;
use Exception; use Exception;
use Kiri\Di\Interface\InjectProxyInterface; use Kiri\Di\Interface\InjectProxyInterface;
use Kiri\Di\Interface\InjectTargetInterface;
use Kiri\Router\Interface\ValidatorInterface; use Kiri\Router\Interface\ValidatorInterface;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use ReflectionAttribute; use ReflectionAttribute;
@@ -212,10 +213,8 @@ class Container implements ContainerInterface
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
if (class_exists($attribute->getName())) { if (class_exists($attribute->getName())) {
$instance = $attribute->newInstance(); $instance = $attribute->newInstance();
if ($object instanceof InjectProxyInterface) { if ($instance instanceof InjectTargetInterface) {
$instance->dispatch($reflect->getFileName(), $object); $instance->dispatch($object::class);
} else {
$instance->dispatch($object);
} }
} }
} }
@@ -248,7 +247,6 @@ class Container implements ContainerInterface
public function resolveProperties(ReflectionClass $reflectionClass, object $class): void public function resolveProperties(ReflectionClass $reflectionClass, object $class): void
{ {
$properties = $reflectionClass->getProperties(); $properties = $reflectionClass->getProperties();
$filename = $reflectionClass->getFileName();
foreach ($properties as $property) { foreach ($properties as $property) {
$propertyAttributes = $property->getAttributes(); $propertyAttributes = $property->getAttributes();
foreach ($propertyAttributes as $attribute) { foreach ($propertyAttributes as $attribute) {
@@ -256,11 +254,7 @@ class Container implements ContainerInterface
continue; continue;
} }
$instance = $attribute->newInstance(); $instance = $attribute->newInstance();
if ($class instanceof InjectProxyInterface) { $instance->dispatch($class::class, $property->getName());
$instance->dispatch($filename, $class, $property->getName());
} else {
$instance->dispatch($class, $property->getName());
}
} }
} }
} }
+16
View File
@@ -0,0 +1,16 @@
<?php
namespace Kiri\Di\Interface;
interface InjectMethodInterface
{
/**
* @param string $class
* @param string $method
* @return void
*/
public function dispatch(string $class, string $method): void;
}
+7 -7
View File
@@ -6,12 +6,12 @@ interface InjectProxyInterface
{ {
/** /**
* @param string $fileName * @param string $fileName
* @param object $class * @param string $class
* @param string $method * @param string $method
* @return void * @return void
*/ */
public function dispatch(string $fileName, object $class, string $method): void; public function dispatch(string $fileName, string $class, string $method): void;
} }
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Kiri\Di\Interface;
interface InjectTargetInterface
{
/**
* @param string $class
* @return void
*/
public function dispatch(string $class): void;
}
+1 -1
View File
@@ -109,7 +109,7 @@ class Scanner extends Component
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$attribute->newInstance()->dispatch($reflect, $method->getName()); $attribute->newInstance()->dispatch($class, $method->getName());
} }
} }
} }