Skip to content

Commit

Permalink
events: added default values & removed magic
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 11, 2021
1 parent 4b4f286 commit 78fad1a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"nette/component-model": "^3.0",
"nette/http": "^3.0.2",
"nette/routing": "~3.0.0",
"nette/utils": "^3.2"
"nette/utils": "^3.2.1"
},
"suggest": {
"nette/forms": "Allows to use Nette\\Application\\UI\\Form",
Expand Down
31 changes: 16 additions & 15 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Nette;
use Nette\Routing\Router;
use Nette\Utils\Arrays;


/**
Expand All @@ -30,22 +31,22 @@ class Application
public $errorPresenter;

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

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

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

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

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

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

/** @var Request[] */
private $requests = [];
Expand Down Expand Up @@ -85,23 +86,23 @@ public function __construct(
public function run(): void
{
try {
$this->onStartup($this);
Arrays::invoke($this->onStartup, $this);
$this->processRequest($this->createInitialRequest());
$this->onShutdown($this);
Arrays::invoke($this->onShutdown, $this);

} catch (\Throwable $e) {
$this->onError($this, $e);
Arrays::invoke($this->onError, $this, $e);
if ($this->catchExceptions && $this->errorPresenter) {
try {
$this->processException($e);
$this->onShutdown($this, $e);
Arrays::invoke($this->onShutdown, $this, $e);
return;

} catch (\Throwable $e) {
$this->onError($this, $e);
Arrays::invoke($this->onError, $this, $e);
}
}
$this->onShutdown($this, $e);
Arrays::invoke($this->onShutdown, $this, $e);
throw $e;
}
}
Expand Down Expand Up @@ -140,7 +141,7 @@ public function processRequest(Request $request): void
}

$this->requests[] = $request;
$this->onRequest($this, $request);
Arrays::invoke($this->onRequest, $this, $request);

if (
!$request->isMethod($request::FORWARD)
Expand All @@ -156,15 +157,15 @@ public function processRequest(Request $request): void
? $e
: new BadRequestException($e->getMessage(), 0, $e);
}
$this->onPresenter($this, $this->presenter);
Arrays::invoke($this->onPresenter, $this, $this->presenter);
$response = $this->presenter->run(clone $request);

if ($response instanceof Responses\ForwardResponse) {
$request = $response->getRequest();
goto process;
}

$this->onResponse($this, $response);
Arrays::invoke($this->onResponse, $this, $response);
$response->send($this->httpRequest, $this->httpResponse);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class Component extends Nette\ComponentModel\Container implements Signa
use Nette\ComponentModel\ArrayAccess;

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

/** @var array */
protected $params = [];
Expand Down Expand Up @@ -78,7 +78,7 @@ protected function validateParent(Nette\ComponentModel\IContainer $parent): void
parent::validateParent($parent);
$this->monitor(Presenter::class, function (Presenter $presenter): void {
$this->loadState($presenter->popGlobalParameters($this->getUniqueId()));
$this->onAnchor($this);
Nette\Utils\Arrays::invoke($this->onAnchor, $this);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Application/UI/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class Form extends Nette\Forms\Form implements SignalReceiver
{
/** @var callable[]&(callable(Form $sender): void)[]; Occurs when form is attached to presenter */
public $onAnchor;
public $onAnchor = [];

/** @var bool */
private $sameSiteProtection = true;
Expand Down Expand Up @@ -57,7 +57,7 @@ protected function validateParent(Nette\ComponentModel\IContainer $parent): void
}
}

$this->onAnchor($this);
Nette\Utils\Arrays::invoke($this->onAnchor, $this);
});
}

Expand Down
13 changes: 7 additions & 6 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Nette\Application\Helpers;
use Nette\Application\Responses;
use Nette\Http;
use Nette\Utils\Arrays;


/**
Expand Down Expand Up @@ -49,13 +50,13 @@ abstract class Presenter extends Control implements Application\IPresenter
public $invalidLinkMode;

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

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

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

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

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

// RENDERING VIEW
$this->beforeRender();
$this->onRender($this);
Arrays::invoke($this->onRender, $this);
// calls $this->render<View>()
$this->tryCall(static::formatRenderMethod($this->view), $this->params);
$this->afterRender();
Expand Down Expand Up @@ -268,7 +269,7 @@ public function run(Application\Request $request): Application\Response
$this->response = new Responses\VoidResponse;
}

$this->onShutdown($this, $this->response);
Arrays::invoke($this->onShutdown, $this, $this->response);
$this->shutdown($this->response);

return $this->response;
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/ApplicationLatte/TemplateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TemplateFactory implements UI\TemplateFactory
use Nette\SmartObject;

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

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

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

return $template;
}
Expand Down

0 comments on commit 78fad1a

Please sign in to comment.