Skip to content

Commit

Permalink
PresenterFactoryCallback: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 13, 2024
1 parent 27f6910 commit fceb6ba
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/Bridges/ApplicationDI/PresenterFactoryCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit fceb6ba

Please sign in to comment.