Skip to content

Commit 78fad1a

Browse files
committed
events: added default values & removed magic
1 parent 4b4f286 commit 78fad1a

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"nette/component-model": "^3.0",
2020
"nette/http": "^3.0.2",
2121
"nette/routing": "~3.0.0",
22-
"nette/utils": "^3.2"
22+
"nette/utils": "^3.2.1"
2323
},
2424
"suggest": {
2525
"nette/forms": "Allows to use Nette\\Application\\UI\\Form",

src/Application/Application.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Nette;
1313
use Nette\Routing\Router;
14+
use Nette\Utils\Arrays;
1415

1516

1617
/**
@@ -30,22 +31,22 @@ class Application
3031
public $errorPresenter;
3132

3233
/** @var callable[]&(callable(Application $sender): void)[]; Occurs before the application loads presenter */
33-
public $onStartup;
34+
public $onStartup = [];
3435

3536
/** @var callable[]&(callable(Application $sender, \Throwable $e = null): void)[]; Occurs before the application shuts down */
36-
public $onShutdown;
37+
public $onShutdown = [];
3738

3839
/** @var callable[]&(callable(Application $sender, Request $request): void)[]; Occurs when a new request is received */
39-
public $onRequest;
40+
public $onRequest = [];
4041

4142
/** @var callable[]&(callable(Application $sender, IPresenter $presenter): void)[]; Occurs when a presenter is created */
42-
public $onPresenter;
43+
public $onPresenter = [];
4344

4445
/** @var callable[]&(callable(Application $sender, Response $response): void)[]; Occurs when a new response is ready for dispatch */
45-
public $onResponse;
46+
public $onResponse = [];
4647

4748
/** @var callable[]&(callable(Application $sender, \Throwable $e): void)[]; Occurs when an unhandled exception occurs in the application */
48-
public $onError;
49+
public $onError = [];
4950

5051
/** @var Request[] */
5152
private $requests = [];
@@ -85,23 +86,23 @@ public function __construct(
8586
public function run(): void
8687
{
8788
try {
88-
$this->onStartup($this);
89+
Arrays::invoke($this->onStartup, $this);
8990
$this->processRequest($this->createInitialRequest());
90-
$this->onShutdown($this);
91+
Arrays::invoke($this->onShutdown, $this);
9192

9293
} catch (\Throwable $e) {
93-
$this->onError($this, $e);
94+
Arrays::invoke($this->onError, $this, $e);
9495
if ($this->catchExceptions && $this->errorPresenter) {
9596
try {
9697
$this->processException($e);
97-
$this->onShutdown($this, $e);
98+
Arrays::invoke($this->onShutdown, $this, $e);
9899
return;
99100

100101
} catch (\Throwable $e) {
101-
$this->onError($this, $e);
102+
Arrays::invoke($this->onError, $this, $e);
102103
}
103104
}
104-
$this->onShutdown($this, $e);
105+
Arrays::invoke($this->onShutdown, $this, $e);
105106
throw $e;
106107
}
107108
}
@@ -140,7 +141,7 @@ public function processRequest(Request $request): void
140141
}
141142

142143
$this->requests[] = $request;
143-
$this->onRequest($this, $request);
144+
Arrays::invoke($this->onRequest, $this, $request);
144145

145146
if (
146147
!$request->isMethod($request::FORWARD)
@@ -156,15 +157,15 @@ public function processRequest(Request $request): void
156157
? $e
157158
: new BadRequestException($e->getMessage(), 0, $e);
158159
}
159-
$this->onPresenter($this, $this->presenter);
160+
Arrays::invoke($this->onPresenter, $this, $this->presenter);
160161
$response = $this->presenter->run(clone $request);
161162

162163
if ($response instanceof Responses\ForwardResponse) {
163164
$request = $response->getRequest();
164165
goto process;
165166
}
166167

167-
$this->onResponse($this, $response);
168+
Arrays::invoke($this->onResponse, $this, $response);
168169
$response->send($this->httpRequest, $this->httpResponse);
169170
}
170171

src/Application/UI/Component.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class Component extends Nette\ComponentModel\Container implements Signa
2727
use Nette\ComponentModel\ArrayAccess;
2828

2929
/** @var callable[]&(callable(Component $sender): void)[]; Occurs when component is attached to presenter */
30-
public $onAnchor;
30+
public $onAnchor = [];
3131

3232
/** @var array */
3333
protected $params = [];
@@ -78,7 +78,7 @@ protected function validateParent(Nette\ComponentModel\IContainer $parent): void
7878
parent::validateParent($parent);
7979
$this->monitor(Presenter::class, function (Presenter $presenter): void {
8080
$this->loadState($presenter->popGlobalParameters($this->getUniqueId()));
81-
$this->onAnchor($this);
81+
Nette\Utils\Arrays::invoke($this->onAnchor, $this);
8282
});
8383
}
8484

src/Application/UI/Form.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class Form extends Nette\Forms\Form implements SignalReceiver
1919
{
2020
/** @var callable[]&(callable(Form $sender): void)[]; Occurs when form is attached to presenter */
21-
public $onAnchor;
21+
public $onAnchor = [];
2222

2323
/** @var bool */
2424
private $sameSiteProtection = true;
@@ -57,7 +57,7 @@ protected function validateParent(Nette\ComponentModel\IContainer $parent): void
5757
}
5858
}
5959

60-
$this->onAnchor($this);
60+
Nette\Utils\Arrays::invoke($this->onAnchor, $this);
6161
});
6262
}
6363

src/Application/UI/Presenter.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Nette\Application\Helpers;
1515
use Nette\Application\Responses;
1616
use Nette\Http;
17+
use Nette\Utils\Arrays;
1718

1819

1920
/**
@@ -49,13 +50,13 @@ abstract class Presenter extends Control implements Application\IPresenter
4950
public $invalidLinkMode;
5051

5152
/** @var callable[]&(callable(Presenter $sender): void)[]; Occurs when the presenter is starting */
52-
public $onStartup;
53+
public $onStartup = [];
5354

5455
/** @var callable[]&(callable(Presenter $sender): void)[]; Occurs when the presenter is rendering after beforeRender */
55-
public $onRender;
56+
public $onRender = [];
5657

5758
/** @var callable[]&(callable(Presenter $sender, Response $response): void)[]; Occurs when the presenter is shutting down */
58-
public $onShutdown;
59+
public $onShutdown = [];
5960

6061
/** @var bool automatically call canonicalize() */
6162
public $autoCanonicalize = true;
@@ -203,7 +204,7 @@ public function run(Application\Request $request): Application\Response
203204

204205
$this->initGlobalParameters();
205206
$this->checkRequirements(static::getReflection());
206-
$this->onStartup($this);
207+
Arrays::invoke($this->onStartup, $this);
207208
$this->startup();
208209
if (!$this->startupCheck) {
209210
$class = static::getReflection()->getMethod('startup')->getDeclaringClass()->getName();
@@ -230,7 +231,7 @@ public function run(Application\Request $request): Application\Response
230231

231232
// RENDERING VIEW
232233
$this->beforeRender();
233-
$this->onRender($this);
234+
Arrays::invoke($this->onRender, $this);
234235
// calls $this->render<View>()
235236
$this->tryCall(static::formatRenderMethod($this->view), $this->params);
236237
$this->afterRender();
@@ -268,7 +269,7 @@ public function run(Application\Request $request): Application\Response
268269
$this->response = new Responses\VoidResponse;
269270
}
270271

271-
$this->onShutdown($this, $this->response);
272+
Arrays::invoke($this->onShutdown, $this, $this->response);
272273
$this->shutdown($this->response);
273274

274275
return $this->response;

src/Bridges/ApplicationLatte/TemplateFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TemplateFactory implements UI\TemplateFactory
2222
use Nette\SmartObject;
2323

2424
/** @var callable[]&(callable(Template $template): void)[]; Occurs when a new template is created */
25-
public $onCreate;
25+
public $onCreate = [];
2626

2727
/** @var LatteFactory */
2828
private $latteFactory;
@@ -140,7 +140,7 @@ public function createTemplate(UI\Control $control = null, string $class = null)
140140
}
141141
$latte->addProvider('cacheStorage', $this->cacheStorage);
142142

143-
$this->onCreate($template);
143+
Nette\Utils\Arrays::invoke($this->onCreate, $template);
144144

145145
return $template;
146146
}

0 commit comments

Comments
 (0)