diff --git a/Container.php b/Container.php index cbc3c79..8fe3fc9 100644 --- a/Container.php +++ b/Container.php @@ -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 diff --git a/Scanner.php b/Scanner.php index 277a5dd..2b792a9 100644 --- a/Scanner.php +++ b/Scanner.php @@ -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()); } } }