This commit is contained in:
2023-12-18 21:55:43 +08:00
parent 4a168cb4a0
commit c6e9133566
2 changed files with 28 additions and 5 deletions
+26
View File
@@ -174,6 +174,32 @@ class Container implements ContainerInterface
}
/**
* @param ReflectionClass $reflect
* @param array $construct
* @param array $config
* @return object|null
* @throws ReflectionException
*/
public function makeReflection(ReflectionClass $reflect, array $construct = [], array $config = []): ?object
{
if (isset($this->_singletons[$reflect->getName()])) {
return $this->_singletons[$reflect->getName()];
}
if (!$reflect->isInstantiable()) {
throw new ReflectionException('Class ' . $reflect->getName() . ' cannot be instantiated');
}
if (($handler = $reflect->getConstructor()) !== null) {
$construct = $this->getMethodParams($handler);
}
$newInstance = $reflect->newInstanceArgs($construct);
return $this->runInit($reflect, static::configure($newInstance, $config));
}
/**
* @param ReflectionClass $reflect
* @param object $object
+2 -5
View File
@@ -93,7 +93,7 @@ class Scanner extends Component
if (class_exists($class)) {
$reflect = $this->container->getReflectionClass($class);
if ($reflect->isInstantiable()) {
if ($reflect->isTrait() || $reflect->isEnum()) {
if ($reflect->isTrait() || $reflect->isEnum() || $reflect->isInterface()) {
return;
}
$attributes = $this->skipNames($reflect);
@@ -105,14 +105,11 @@ class Scanner extends Component
continue;
}
$attributes = $method->getAttributes();
if (count($attributes) > 0) {
$object = $this->container->parse($class);
}
foreach ($attributes as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$attribute->newInstance()->dispatch($object, $method->getName());
$attribute->newInstance()->dispatch($reflect, $method->getName());
}
}
}