Skip to content

Commit 5868278

Browse files
committed
Component, Form: added getPresenterIfExists()
1 parent 5ae6bc1 commit 5868278

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

src/Application/UI/Component.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,22 @@ abstract class Component extends Nette\ComponentModel\Container implements ISign
3939
public function getPresenter(): ?Presenter
4040
{
4141
if (func_num_args()) {
42-
trigger_error(__METHOD__ . '() parameter $throw is deprecated, use hasPresenter()', E_USER_DEPRECATED);
42+
trigger_error(__METHOD__ . '() parameter $throw is deprecated, use getPresenterIfExists()', E_USER_DEPRECATED);
4343
$throw = func_get_arg(0);
4444
}
4545
return $this->lookup(Presenter::class, $throw ?? true);
4646
}
4747

4848

49+
/**
50+
* Returns the presenter where this component belongs to.
51+
*/
52+
public function getPresenterIfExists(): ?Presenter
53+
{
54+
return $this->lookup(Presenter::class, false);
55+
}
56+
57+
4958
/**
5059
* Returns whether there is a presenter.
5160
*/

src/Application/UI/Form.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,22 @@ protected function validateParent(Nette\ComponentModel\IContainer $parent): void
6565
final public function getPresenter(): ?Presenter
6666
{
6767
if (func_num_args()) {
68-
trigger_error(__METHOD__ . '() parameter $throw is deprecated, use hasPresenter()', E_USER_DEPRECATED);
68+
trigger_error(__METHOD__ . '() parameter $throw is deprecated, use getPresenterIfExists()', E_USER_DEPRECATED);
6969
$throw = func_get_arg(0);
7070
}
7171
return $this->lookup(Presenter::class, $throw ?? true);
7272
}
7373

7474

75+
/**
76+
* Returns the presenter where this component belongs to.
77+
*/
78+
final public function getPresenterIfExists(): ?Presenter
79+
{
80+
return $this->lookup(Presenter::class, false);
81+
}
82+
83+
7584
/**
7685
* Returns whether there is a presenter.
7786
*/

src/Application/UI/Presenter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ final public function getPresenter(): self
157157
}
158158

159159

160+
final public function getPresenterIfExists(): self
161+
{
162+
return $this;
163+
}
164+
165+
160166
final public function hasPresenter(): bool
161167
{
162168
return true;

src/Bridges/ApplicationLatte/TemplateFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function createTemplate(UI\Control $control = null): UI\ITemplate
5858
{
5959
$latte = $this->latteFactory->create();
6060
$template = new $this->templateClass($latte);
61-
$presenter = ($control && $control->hasPresenter()) ? $control->getPresenter() : null;
61+
$presenter = $control ? $control->getPresenterIfExists() : null;
6262

6363
if ($latte->onCompile instanceof \Traversable) {
6464
$latte->onCompile = iterator_to_array($latte->onCompile);

tests/Bridges.Latte/TemplateFactory.nonce.presenter.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ $response = Mockery::mock(Nette\Http\IResponse::class);
2424
$response->shouldReceive('getHeader')->with('Content-Security-Policy')->andReturn("hello 'nonce-abcd123==' world");
2525

2626
$presenter = Mockery::mock(UI\Presenter::class);
27-
$presenter->shouldReceive('hasPresenter')->andReturn(true);
28-
$presenter->shouldReceive('getPresenter')->andReturn($presenter);
27+
$presenter->shouldReceive('getPresenterIfExists')->andReturn($presenter);
2928
$presenter->shouldReceive('getHttpResponse')->andReturn($response);
3029
$presenter->shouldIgnoreMissing();
3130

0 commit comments

Comments
 (0)