From b4ad530ca5674d5266defbc5ca9b7a6ca87af104 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 27 Apr 2024 17:44:00 +0200 Subject: [PATCH] Container: refactoring --- src/Bridges/DITracy/ContainerPanel.php | 9 +++++---- src/DI/Container.php | 26 ++++++-------------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/Bridges/DITracy/ContainerPanel.php b/src/Bridges/DITracy/ContainerPanel.php index f8301b062..377dd3fef 100644 --- a/src/Bridges/DITracy/ContainerPanel.php +++ b/src/Bridges/DITracy/ContainerPanel.php @@ -50,11 +50,12 @@ public function getTab(): string */ public function getPanel(): string { - $methods = (fn() => $this->methods)->bindTo($this->container, Container::class)(); $services = []; - foreach ($methods as $name => $foo) { - $name = lcfirst(str_replace('__', '.', substr($name, 13))); - $services[$name] = $this->container->getServiceType($name); + foreach (get_class_methods($this->container) as $method) { + if (preg_match('#^createService.#', $method)) { + $name = lcfirst(str_replace('__', '.', substr($method, 13))); + $services[$name] = $this->container->getServiceType($name); + } } ksort($services, SORT_NATURAL); diff --git a/src/DI/Container.php b/src/DI/Container.php index 3d1462e55..b726587ca 100644 --- a/src/DI/Container.php +++ b/src/DI/Container.php @@ -48,10 +48,7 @@ class Container public function __construct(array $params = []) { $this->parameters = $params + $this->getStaticParameters(); - $this->methods = array_flip(array_filter( - get_class_methods($this), - fn($s) => preg_match('#^createService.#', $s), - )); + $this->methods = array_flip(get_class_methods($this)); } @@ -257,22 +254,11 @@ public function getByType(string $type, bool $throw = true): ?object } elseif ($throw) { if (!class_exists($type) && !interface_exists($type)) { throw new MissingServiceException(sprintf("Service of type '%s' not found. Check the class name because it cannot be found.", $type)); + } elseif ($this->findByType($type)) { + throw new MissingServiceException(sprintf("Service of type %s is not autowired or is missing in di\u{a0}›\u{a0}export\u{a0}›\u{a0}types.", $type)); + } else { + throw new MissingServiceException(sprintf('Service of type %s not found. Did you add it to configuration file?', $type)); } - - foreach ($this->methods as $method => $foo) { - $methodType = (new \ReflectionMethod(static::class, $method))->getReturnType()->getName(); - if (is_a($methodType, $type, allow_string: true)) { - throw new MissingServiceException(sprintf( - "Service of type %s is not autowired or is missing in di\u{a0}›\u{a0}export\u{a0}›\u{a0}types.", - $type, - )); - } - } - - throw new MissingServiceException(sprintf( - 'Service of type %s not found. Did you add it to configuration file?', - $type, - )); } return null; @@ -383,7 +369,7 @@ private function autowireArguments(\ReflectionFunctionAbstract $function, array /** * Returns the method name for creating a service. */ - public static function getMethodName(string $name): string + final public static function getMethodName(string $name): string { if ($name === '') { throw new Nette\InvalidArgumentException('Service name must be a non-empty string.');