Skip to content

Commit

Permalink
PresenterFactoryCallback: refreshes page instead of creating presente…
Browse files Browse the repository at this point in the history
…rs dynamically
  • Loading branch information
dg committed Jun 11, 2024
1 parent 7df76e9 commit 6f1da00
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationDI/ApplicationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function loadConfiguration(): void
->setType(Nette\Application\IPresenterFactory::class)
->setFactory(Nette\Application\PresenterFactory::class, [new Definitions\Statement(
Nette\Bridges\ApplicationDI\PresenterFactoryCallback::class,
[1 => $this->invalidLinkMode, $touch ?? null],
[1 => $touch ?? null],
)]);

if ($config->mapping) {
Expand Down
23 changes: 5 additions & 18 deletions src/Bridges/ApplicationDI/PresenterFactoryCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class PresenterFactoryCallback
{
public function __construct(
private readonly Nette\DI\Container $container,
private readonly int $invalidLinkMode,
private readonly ?string $touchToRefresh,
) {
}
Expand All @@ -40,25 +39,13 @@ public function __invoke(string $class): Nette\Application\IPresenter
return $this->container->createService($services[0]);
}

if ($this->touchToRefresh) {
if ($this->touchToRefresh && class_exists($class)) {
touch($this->touchToRefresh);
echo 'Class ' . htmlspecialchars($class) . ' was not found in DI container.<br><br>If you just created this presenter, it should be enough to refresh the page. It will happen automatically in 5 seconds.<br><br>Otherwise, please check the configuration of your DI container.';
header('Refresh: 5');
exit;
}

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);
}

throw $e;
}

if ($presenter instanceof Nette\Application\UI\Presenter && !isset($presenter->invalidLinkMode)) {
$presenter->invalidLinkMode = $this->invalidLinkMode;
}

return $presenter;
throw new Nette\Application\InvalidPresenterException("No services of type $class found.");
}
}

0 comments on commit 6f1da00

Please sign in to comment.