From aac8dee9110b39ad89435bf72a3ed280f36d3dca Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 16 Oct 2019 13:20:00 +0200 Subject: [PATCH 1/4] Component, Form: added getPresenterIfExists() --- src/Application/UI/Component.php | 11 ++++++++++- src/Application/UI/Form.php | 11 ++++++++++- src/Application/UI/Presenter.php | 6 ++++++ src/Bridges/ApplicationLatte/TemplateFactory.php | 2 +- 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Application/UI/Component.php b/src/Application/UI/Component.php index f9b7f5eb2..7abc1290e 100644 --- a/src/Application/UI/Component.php +++ b/src/Application/UI/Component.php @@ -39,13 +39,22 @@ abstract class Component extends Nette\ComponentModel\Container implements ISign public function getPresenter(): ?Presenter { if (func_num_args()) { - trigger_error(__METHOD__ . '() parameter $throw is deprecated, use hasPresenter()', E_USER_DEPRECATED); + trigger_error(__METHOD__ . '() parameter $throw is deprecated, use getPresenterIfExists()', E_USER_DEPRECATED); $throw = func_get_arg(0); } return $this->lookup(Presenter::class, $throw ?? true); } + /** + * Returns the presenter where this component belongs to. + */ + public function getPresenterIfExists(): ?Presenter + { + return $this->lookup(Presenter::class, false); + } + + /** * Returns whether there is a presenter. */ diff --git a/src/Application/UI/Form.php b/src/Application/UI/Form.php index d1d55ed8b..a92809848 100644 --- a/src/Application/UI/Form.php +++ b/src/Application/UI/Form.php @@ -65,13 +65,22 @@ protected function validateParent(Nette\ComponentModel\IContainer $parent): void final public function getPresenter(): ?Presenter { if (func_num_args()) { - trigger_error(__METHOD__ . '() parameter $throw is deprecated, use hasPresenter()', E_USER_DEPRECATED); + trigger_error(__METHOD__ . '() parameter $throw is deprecated, use getPresenterIfExists()', E_USER_DEPRECATED); $throw = func_get_arg(0); } return $this->lookup(Presenter::class, $throw ?? true); } + /** + * Returns the presenter where this component belongs to. + */ + final public function getPresenterIfExists(): ?Presenter + { + return $this->lookup(Presenter::class, false); + } + + /** * Returns whether there is a presenter. */ diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 14945dfd9..0ed44094c 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -157,6 +157,12 @@ final public function getPresenter(): self } + final public function getPresenterIfExists(): self + { + return $this; + } + + final public function hasPresenter(): bool { return true; diff --git a/src/Bridges/ApplicationLatte/TemplateFactory.php b/src/Bridges/ApplicationLatte/TemplateFactory.php index 5ed5d1008..e34e0fec4 100644 --- a/src/Bridges/ApplicationLatte/TemplateFactory.php +++ b/src/Bridges/ApplicationLatte/TemplateFactory.php @@ -58,7 +58,7 @@ public function createTemplate(UI\Control $control = null): UI\ITemplate { $latte = $this->latteFactory->create(); $template = new $this->templateClass($latte); - $presenter = ($control && $control->hasPresenter()) ? $control->getPresenter() : null; + $presenter = $control ? $control->getPresenterIfExists() : null; if ($latte->onCompile instanceof \Traversable) { $latte->onCompile = iterator_to_array($latte->onCompile); From fa618e97a212d22f07cfd70e72fe9bc4629716be Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 16 Oct 2019 13:20:50 +0200 Subject: [PATCH 2/4] Component, Form: deprecated hasPresenter() --- src/Application/UI/Component.php | 4 +--- src/Application/UI/Form.php | 4 +--- src/Application/UI/Presenter.php | 1 + 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Application/UI/Component.php b/src/Application/UI/Component.php index 7abc1290e..411233a18 100644 --- a/src/Application/UI/Component.php +++ b/src/Application/UI/Component.php @@ -55,9 +55,7 @@ public function getPresenterIfExists(): ?Presenter } - /** - * Returns whether there is a presenter. - */ + /** @deprecated */ public function hasPresenter(): bool { return (bool) $this->lookup(Presenter::class, false); diff --git a/src/Application/UI/Form.php b/src/Application/UI/Form.php index a92809848..9c69289b8 100644 --- a/src/Application/UI/Form.php +++ b/src/Application/UI/Form.php @@ -81,9 +81,7 @@ final public function getPresenterIfExists(): ?Presenter } - /** - * Returns whether there is a presenter. - */ + /** @deprecated */ public function hasPresenter(): bool { return (bool) $this->lookup(Presenter::class, false); diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 0ed44094c..638ddeef9 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -163,6 +163,7 @@ final public function getPresenterIfExists(): self } + /** @deprecated */ final public function hasPresenter(): bool { return true; From e62db25e0a794fe59bde496ad339029a4686ad43 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 17 Oct 2019 21:42:39 +0200 Subject: [PATCH 3/4] opened 3.1-dev --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bdddad5de..54c6ff3c1 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,7 @@ "minimum-stability": "dev", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-master": "3.1-dev" } } } From 66afd460e3015c5808b9119899a979b815775c6a Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 17 Oct 2019 21:42:53 +0200 Subject: [PATCH 4/4] Application: uses Nette\Http\ResponseEmitter --- composer.json | 4 ++-- src/Application/Application.php | 18 ++++++++++++------ src/Application/UI/Presenter.php | 4 +--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 54c6ff3c1..d9e876efa 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require": { "php": ">=7.1", "nette/component-model": "^3.0", - "nette/http": "^3.0.2", + "nette/http": "^4.0", "nette/routing": "~3.0.0", "nette/utils": "^3.0" }, @@ -28,7 +28,7 @@ "require-dev": { "nette/tester": "^2.0.1", "nette/di": "^v3.0", - "nette/forms": "^3.0", + "nette/forms": "^4.0", "nette/robot-loader": "^3.2", "nette/security": "^3.0", "latte/latte": "^2.5.1", diff --git a/src/Application/Application.php b/src/Application/Application.php index fdb64b8b7..ffb23b3a5 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -65,6 +65,9 @@ class Application /** @var Router */ private $router; + /** @var Nette\Http\ResponseEmitter */ + private $emitter; + public function __construct(IPresenterFactory $presenterFactory, Router $router, Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse) { @@ -72,6 +75,7 @@ public function __construct(IPresenterFactory $presenterFactory, Router $router, $this->httpResponse = $httpResponse; $this->presenterFactory = $presenterFactory; $this->router = $router; + $this->emitter = new Nette\Http\ResponseEmitter; } @@ -83,6 +87,7 @@ public function run(): void try { $this->onStartup($this); $this->processRequest($this->createInitialRequest()); + $this->emitter->send($this->httpResponse); $this->onShutdown($this); } catch (\Throwable $e) { @@ -90,6 +95,7 @@ public function run(): void if ($this->catchExceptions && $this->errorPresenter) { try { $this->processException($e); + $this->emitter->send($this->httpResponse); $this->onShutdown($this, $e); return; @@ -154,18 +160,18 @@ public function processRequest(Request $request): void } $this->onResponse($this, $response); - $response->send($this->httpRequest, $this->httpResponse); + $this->httpResponse->setBody(function () use ($response) { + $response->send($this->httpRequest, $this->httpResponse); + }); } public function processException(\Throwable $e): void { - if (!$e instanceof BadRequestException && $this->httpResponse instanceof Nette\Http\Response) { - $this->httpResponse->warnOnBuffer = false; - } - if (!$this->httpResponse->isSent()) { - $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getHttpCode() ?: 404) : 500); + if (!$e instanceof BadRequestException) { + $this->emitter->warnOnBuffer = false; } + $this->httpResponse->setCode($e instanceof BadRequestException ? ($e->getHttpCode() ?: 404) : 500); $args = ['exception' => $e, 'request' => end($this->requests) ?: null]; if ($this->presenter instanceof UI\Presenter) { diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 638ddeef9..6c668e1aa 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -190,9 +190,7 @@ public function run(Application\Request $request): Application\IResponse $this->payload = $this->payload ?: new \stdClass; $this->setParent($this->getParent(), $request->getPresenterName()); - if (!$this->httpResponse->isSent()) { - $this->httpResponse->addHeader('Vary', 'X-Requested-With'); - } + $this->httpResponse->addHeader('Vary', 'X-Requested-With'); $this->initGlobalParameters(); $this->checkRequirements(static::getReflection());