Skip to content

Commit bb59a51

Browse files
committed
Presenter & others: injected services are readonly
1 parent 3316126 commit bb59a51

4 files changed

Lines changed: 26 additions & 46 deletions

File tree

src/Application/UI/Link.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,11 @@
1616
*/
1717
final class Link
1818
{
19-
private Component $component;
20-
private string $destination;
21-
private array $params;
22-
23-
24-
/**
25-
* Link specification.
26-
*/
27-
public function __construct(Component $component, string $destination, array $params = [])
28-
{
29-
$this->component = $component;
30-
$this->destination = $destination;
31-
$this->params = $params;
19+
public function __construct(
20+
private readonly Component $component,
21+
private readonly string $destination,
22+
private array $params = [],
23+
) {
3224
}
3325

3426

src/Application/UI/Presenter.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ abstract class Presenter extends Control implements Application\IPresenter
104104
private bool $startupCheck = false;
105105
private ?Nette\Application\Request $lastCreatedRequest;
106106
private ?array $lastCreatedRequestFlag;
107-
private Nette\Http\IRequest $httpRequest;
108-
private Nette\Http\IResponse $httpResponse;
109-
private ?Nette\Http\Session $session = null;
110-
private ?Nette\Application\IPresenterFactory $presenterFactory = null;
111-
private ?Nette\Routing\Router $router = null;
112-
private ?Nette\Security\User $user = null;
113-
private ?TemplateFactory $templateFactory = null;
107+
private readonly Nette\Http\IRequest $httpRequest;
108+
private readonly Nette\Http\IResponse $httpResponse;
109+
private readonly ?Nette\Http\Session $session;
110+
private readonly ?Nette\Application\IPresenterFactory $presenterFactory;
111+
private readonly ?Nette\Routing\Router $router;
112+
private readonly ?Nette\Security\User $user;
113+
private readonly ?TemplateFactory $templateFactory;
114114
private Nette\Http\UrlScript $refUrlCache;
115115

116116

@@ -806,7 +806,7 @@ protected function createRequest(
806806
$presenter = $module . $sep . $presenter;
807807
}
808808

809-
if (!$this->presenterFactory) {
809+
if (empty($this->presenterFactory)) {
810810
throw new Nette\InvalidStateException('Unable to create link to other presenter, service PresenterFactory has not been set.');
811811
}
812812

@@ -985,7 +985,7 @@ protected function requestToUrl(Application\Request $request, ?bool $relative =
985985
$this->refUrlCache = new Http\UrlScript($url->getHostUrl() . $url->getScriptPath());
986986
}
987987

988-
if (!$this->router) {
988+
if (empty($this->router)) {
989989
throw new Nette\InvalidStateException('Unable to generate URL, service Router has not been set.');
990990
}
991991

@@ -1374,11 +1374,8 @@ final public function injectPrimary(
13741374
?Http\Session $session = null,
13751375
?Nette\Security\User $user = null,
13761376
?TemplateFactory $templateFactory = null,
1377-
) {
1378-
if (isset($this->presenterFactory)) {
1379-
throw new Nette\InvalidStateException('Method ' . __METHOD__ . ' is intended for initialization and should not be called more than once.');
1380-
}
1381-
1377+
): void
1378+
{
13821379
$this->presenterFactory = $presenterFactory;
13831380
$this->router = $router;
13841381
$this->httpRequest = $httpRequest;
@@ -1403,7 +1400,7 @@ final public function getHttpResponse(): Http\IResponse
14031400

14041401
final public function getSession(?string $namespace = null): Http\Session|Http\SessionSection
14051402
{
1406-
if (!$this->session) {
1403+
if (empty($this->session)) {
14071404
throw new Nette\InvalidStateException('Service Session has not been set.');
14081405
}
14091406

@@ -1415,20 +1412,12 @@ final public function getSession(?string $namespace = null): Http\Session|Http\S
14151412

14161413
final public function getUser(): Nette\Security\User
14171414
{
1418-
if (!$this->user) {
1419-
throw new Nette\InvalidStateException('Service User has not been set.');
1420-
}
1421-
1422-
return $this->user;
1415+
return $this->user ?? throw new Nette\InvalidStateException('Service User has not been set.');
14231416
}
14241417

14251418

14261419
final public function getTemplateFactory(): TemplateFactory
14271420
{
1428-
if (!$this->templateFactory) {
1429-
throw new Nette\InvalidStateException('Service TemplateFactory has not been set.');
1430-
}
1431-
1432-
return $this->templateFactory;
1421+
return $this->templateFactory ?? throw new Nette\InvalidStateException('Service TemplateFactory has not been set.');
14331422
}
14341423
}

src/Bridges/ApplicationLatte/SnippetRuntime.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ final class SnippetRuntime
2929
private array $stack = [];
3030
private int $nestingLevel = 0;
3131
private bool $renderingSnippets = false;
32-
private Control $control;
32+
3333
private ?\stdClass $payload;
3434

3535

36-
public function __construct(Control $control)
37-
{
38-
$this->control = $control;
36+
public function __construct(
37+
private readonly Control $control,
38+
) {
3939
}
4040

4141

src/Bridges/ApplicationLatte/Template.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818
*/
1919
class Template implements Nette\Application\UI\Template
2020
{
21-
private Latte\Engine $latte;
2221
private ?string $file = null;
2322

2423

25-
public function __construct(Latte\Engine $latte)
26-
{
27-
$this->latte = $latte;
24+
public function __construct(
25+
private readonly Latte\Engine $latte,
26+
) {
2827
}
2928

3029

0 commit comments

Comments
 (0)