From bb59a51bc36b7561a6e44d1f014e0e3d68e23dc2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 6 Mar 2024 12:12:05 +0100 Subject: [PATCH] Presenter & others: injected services are readonly --- src/Application/UI/Link.php | 18 +++------ src/Application/UI/Presenter.php | 39 +++++++------------ .../ApplicationLatte/SnippetRuntime.php | 8 ++-- src/Bridges/ApplicationLatte/Template.php | 7 ++-- 4 files changed, 26 insertions(+), 46 deletions(-) diff --git a/src/Application/UI/Link.php b/src/Application/UI/Link.php index 0d6ee93ad..68eea2d1c 100644 --- a/src/Application/UI/Link.php +++ b/src/Application/UI/Link.php @@ -16,19 +16,11 @@ */ final class Link { - private Component $component; - private string $destination; - private array $params; - - - /** - * Link specification. - */ - public function __construct(Component $component, string $destination, array $params = []) - { - $this->component = $component; - $this->destination = $destination; - $this->params = $params; + public function __construct( + private readonly Component $component, + private readonly string $destination, + private array $params = [], + ) { } diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 914d563c0..23f57f178 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -104,13 +104,13 @@ abstract class Presenter extends Control implements Application\IPresenter private bool $startupCheck = false; private ?Nette\Application\Request $lastCreatedRequest; private ?array $lastCreatedRequestFlag; - private Nette\Http\IRequest $httpRequest; - private Nette\Http\IResponse $httpResponse; - private ?Nette\Http\Session $session = null; - private ?Nette\Application\IPresenterFactory $presenterFactory = null; - private ?Nette\Routing\Router $router = null; - private ?Nette\Security\User $user = null; - private ?TemplateFactory $templateFactory = null; + private readonly Nette\Http\IRequest $httpRequest; + private readonly Nette\Http\IResponse $httpResponse; + private readonly ?Nette\Http\Session $session; + private readonly ?Nette\Application\IPresenterFactory $presenterFactory; + private readonly ?Nette\Routing\Router $router; + private readonly ?Nette\Security\User $user; + private readonly ?TemplateFactory $templateFactory; private Nette\Http\UrlScript $refUrlCache; @@ -806,7 +806,7 @@ protected function createRequest( $presenter = $module . $sep . $presenter; } - if (!$this->presenterFactory) { + if (empty($this->presenterFactory)) { throw new Nette\InvalidStateException('Unable to create link to other presenter, service PresenterFactory has not been set.'); } @@ -985,7 +985,7 @@ protected function requestToUrl(Application\Request $request, ?bool $relative = $this->refUrlCache = new Http\UrlScript($url->getHostUrl() . $url->getScriptPath()); } - if (!$this->router) { + if (empty($this->router)) { throw new Nette\InvalidStateException('Unable to generate URL, service Router has not been set.'); } @@ -1374,11 +1374,8 @@ final public function injectPrimary( ?Http\Session $session = null, ?Nette\Security\User $user = null, ?TemplateFactory $templateFactory = null, - ) { - if (isset($this->presenterFactory)) { - throw new Nette\InvalidStateException('Method ' . __METHOD__ . ' is intended for initialization and should not be called more than once.'); - } - + ): void + { $this->presenterFactory = $presenterFactory; $this->router = $router; $this->httpRequest = $httpRequest; @@ -1403,7 +1400,7 @@ final public function getHttpResponse(): Http\IResponse final public function getSession(?string $namespace = null): Http\Session|Http\SessionSection { - if (!$this->session) { + if (empty($this->session)) { throw new Nette\InvalidStateException('Service Session has not been set.'); } @@ -1415,20 +1412,12 @@ final public function getSession(?string $namespace = null): Http\Session|Http\S final public function getUser(): Nette\Security\User { - if (!$this->user) { - throw new Nette\InvalidStateException('Service User has not been set.'); - } - - return $this->user; + return $this->user ?? throw new Nette\InvalidStateException('Service User has not been set.'); } final public function getTemplateFactory(): TemplateFactory { - if (!$this->templateFactory) { - throw new Nette\InvalidStateException('Service TemplateFactory has not been set.'); - } - - return $this->templateFactory; + return $this->templateFactory ?? throw new Nette\InvalidStateException('Service TemplateFactory has not been set.'); } } diff --git a/src/Bridges/ApplicationLatte/SnippetRuntime.php b/src/Bridges/ApplicationLatte/SnippetRuntime.php index 5c24aaf09..1856c3318 100644 --- a/src/Bridges/ApplicationLatte/SnippetRuntime.php +++ b/src/Bridges/ApplicationLatte/SnippetRuntime.php @@ -29,13 +29,13 @@ final class SnippetRuntime private array $stack = []; private int $nestingLevel = 0; private bool $renderingSnippets = false; - private Control $control; + private ?\stdClass $payload; - public function __construct(Control $control) - { - $this->control = $control; + public function __construct( + private readonly Control $control, + ) { } diff --git a/src/Bridges/ApplicationLatte/Template.php b/src/Bridges/ApplicationLatte/Template.php index 4d985a8cc..4912b27c2 100644 --- a/src/Bridges/ApplicationLatte/Template.php +++ b/src/Bridges/ApplicationLatte/Template.php @@ -18,13 +18,12 @@ */ class Template implements Nette\Application\UI\Template { - private Latte\Engine $latte; private ?string $file = null; - public function __construct(Latte\Engine $latte) - { - $this->latte = $latte; + public function __construct( + private readonly Latte\Engine $latte, + ) { }