Skip to content

Commit

Permalink
Presenter & others: injected services are readonly
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 11, 2024
1 parent 3316126 commit bb59a51
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 46 deletions.
18 changes: 5 additions & 13 deletions src/Application/UI/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
) {
}


Expand Down
39 changes: 14 additions & 25 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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.');
}

Expand Down Expand Up @@ -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.');
}

Expand Down Expand Up @@ -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;
Expand All @@ -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.');
}

Expand All @@ -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.');
}
}
8 changes: 4 additions & 4 deletions src/Bridges/ApplicationLatte/SnippetRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}


Expand Down
7 changes: 3 additions & 4 deletions src/Bridges/ApplicationLatte/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}


Expand Down

0 comments on commit bb59a51

Please sign in to comment.