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 Exception;
use Kiri\Di\Interface\InjectProxyInterface;
use Kiri\Di\Interface\InjectTargetInterface;
use Kiri\Router\Interface\ValidatorInterface;
use Psr\Container\ContainerInterface;
use ReflectionAttribute;
@@ -212,10 +213,8 @@ class Container implements ContainerInterface
foreach ($attributes as $attribute) {
if (class_exists($attribute->getName())) {
$instance = $attribute->newInstance();
if ($object instanceof InjectProxyInterface) {
$instance->dispatch($reflect->getFileName(), $object);
} else {
$instance->dispatch($object);
if ($instance instanceof InjectTargetInterface) {
$instance->dispatch($object::class);
}
}
}
@@ -248,7 +247,6 @@ class Container implements ContainerInterface
public function resolveProperties(ReflectionClass $reflectionClass, object $class): void
{
$properties = $reflectionClass->getProperties();
$filename = $reflectionClass->getFileName();
foreach ($properties as $property) {
$propertyAttributes = $property->getAttributes();
foreach ($propertyAttributes as $attribute) {
@@ -256,11 +254,7 @@ class Container implements ContainerInterface
continue;
}
$instance = $attribute->newInstance();
if ($class instanceof InjectProxyInterface) {
$instance->dispatch($filename, $class, $property->getName());
} else {
$instance->dispatch($class, $property->getName());
}
$instance->dispatch($class::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;
}
+2 -2
View File
@@ -8,10 +8,10 @@ interface InjectProxyInterface
/**
* @param string $fileName
* @param object $class
* @param string $class
* @param string $method
* @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())) {
continue;
}
$attribute->newInstance()->dispatch($reflect, $method->getName());
$attribute->newInstance()->dispatch($class, $method->getName());
}
}
}