From fceb6ba8749b4c9b5f83b943b34ecffdbe0c02a1 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 28 Apr 2024 14:29:04 +0200 Subject: [PATCH] PresenterFactoryCallback: refactoring --- .../PresenterFactoryCallback.php | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/Bridges/ApplicationDI/PresenterFactoryCallback.php b/src/Bridges/ApplicationDI/PresenterFactoryCallback.php index 06449b7e6..7e60db7c0 100644 --- a/src/Bridges/ApplicationDI/PresenterFactoryCallback.php +++ b/src/Bridges/ApplicationDI/PresenterFactoryCallback.php @@ -30,36 +30,35 @@ public function __invoke(string $class): Nette\Application\IPresenter { $services = $this->container->findByType($class); if (count($services) > 1) { - $exact = array_keys(array_map($this->container->getServiceType(...), $services), $class, strict: true); - if (count($exact) === 1) { - return $this->container->createService($services[$exact[0]]); + $services = array_values(array_filter($services, fn($service) => $this->container->getServiceType($service) === $class)); + if (count($services) > 1) { + throw new Nette\Application\InvalidPresenterException("Multiple services of type $class found: " . implode(', ', $services) . '.'); } + } - throw new Nette\Application\InvalidPresenterException("Multiple services of type $class found: " . implode(', ', $services) . '.'); - - } elseif (!$services) { - if ($this->touchToRefresh) { - touch($this->touchToRefresh); - } + if (count($services) === 1) { + return $this->container->createService($services[0]); + } - try { - $presenter = $this->container->createInstance($class); - $this->container->callInjects($presenter); - } catch (Nette\DI\MissingServiceException | Nette\DI\ServiceCreationException $e) { - if ($this->touchToRefresh && class_exists($class)) { - throw new \Exception("Refresh your browser. New presenter $class was found.", 0, $e); - } + if ($this->touchToRefresh) { + touch($this->touchToRefresh); + } - throw $e; + try { + $presenter = $this->container->createInstance($class); + $this->container->callInjects($presenter); + } catch (Nette\DI\MissingServiceException | Nette\DI\ServiceCreationException $e) { + if ($this->touchToRefresh && class_exists($class)) { + throw new \Exception("Refresh your browser. New presenter $class was found.", 0, $e); } - if ($presenter instanceof Nette\Application\UI\Presenter && !isset($presenter->invalidLinkMode)) { - $presenter->invalidLinkMode = $this->invalidLinkMode; - } + throw $e; + } - return $presenter; + if ($presenter instanceof Nette\Application\UI\Presenter && !isset($presenter->invalidLinkMode)) { + $presenter->invalidLinkMode = $this->invalidLinkMode; } - return $this->container->createService($services[0]); + return $presenter; } }