Skip to content

Commit e5164cc

Browse files
matej21dg
authored andcommitted
UIMacros: used SnippetBridge
1 parent 7634019 commit e5164cc

35 files changed

+141
-848
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
namespace Nette\Bridges\ApplicationLatte;
9+
10+
use Nette;
11+
use Latte\Runtime\ISnippetBridge;
12+
use Nette\Application\UI\Control;
13+
use Nette\Application\UI\IRenderable;
14+
15+
16+
/**
17+
* @internal
18+
*/
19+
class SnippetBridge implements ISnippetBridge
20+
{
21+
use Nette\SmartObject;
22+
23+
/** @var Control */
24+
private $control;
25+
26+
/** @var \stdClass|null */
27+
private $payload;
28+
29+
30+
public function __construct(Control $control)
31+
{
32+
$this->control = $control;
33+
}
34+
35+
36+
public function isSnippetMode()
37+
{
38+
return $this->control->snippetMode;
39+
}
40+
41+
42+
public function needsRedraw($name)
43+
{
44+
return $this->control->isControlInvalid($name);
45+
}
46+
47+
48+
public function markRedrawn($name)
49+
{
50+
if ($name !== '') {
51+
$this->control->redrawControl($name, FALSE);
52+
}
53+
}
54+
55+
56+
public function getHtmlId($name)
57+
{
58+
return $this->control->getSnippetId($name);
59+
}
60+
61+
62+
public function addSnippet($name, $content)
63+
{
64+
if ($this->payload === NULL) {
65+
$this->payload = $this->control->getPresenter()->getPayload();
66+
}
67+
$this->payload->snippets[$this->control->getSnippetId($name)] = $content;
68+
}
69+
70+
71+
public function renderChildren()
72+
{
73+
if ($this->control instanceof IRenderable) {
74+
$queue = [$this->control];
75+
do {
76+
foreach (array_shift($queue)->getComponents() as $child) {
77+
if ($child instanceof IRenderable) {
78+
if ($child->isControlInvalid()) {
79+
$child->snippetMode = TRUE;
80+
$child->render();
81+
$child->snippetMode = FALSE;
82+
}
83+
} elseif ($child instanceof Nette\ComponentModel\IContainer) {
84+
$queue[] = $child;
85+
}
86+
}
87+
} while ($queue);
88+
}
89+
}
90+
91+
}

src/Bridges/ApplicationLatte/TemplateFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public function createTemplate(UI\Control $control = NULL)
9494
$template->flashes = [];
9595
$latte->addProvider('uiControl', $control);
9696
$latte->addProvider('uiPresenter', $presenter);
97+
if ($control) {
98+
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($control));
99+
}
97100
$latte->addProvider('cacheStorage', $this->cacheStorage);
98101

99102
// back compatibility

src/Bridges/ApplicationLatte/UIMacros.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function initialize()
6161
public function finalize()
6262
{
6363
return [
64-
'if (Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->blockQueue)) return;',
64+
'Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this)',
6565
'',
6666
$this->extends ? '' : '$this->parentName = $this->parentName ?: ($this->blocks && !$this->getReferringTemplate()
6767
&& isset($this->global->uiControl) && $this->global->uiControl instanceof Nette\Application\UI\Presenter

src/Bridges/ApplicationLatte/UIRuntime.php

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class UIRuntime
2323
/**
2424
* @return bool
2525
*/
26-
public static function initialize(Latte\Template $template, $blockQueue)
26+
public static function initialize(Latte\Template $template)
2727
{
2828
// back compatiblity
2929
$params = $template->getParameters();
@@ -35,55 +35,6 @@ public static function initialize(Latte\Template $template, $blockQueue)
3535
trigger_error('Replace template variable $_presenter with provider: $latte->addProvider("uiPresenter", ...)', E_USER_DEPRECATED);
3636
$template->global->uiPresenter = $params['_presenter'];
3737
}
38-
39-
// snippet support
40-
if (!$template->getParentName() && !empty($template->global->uiControl->snippetMode)) {
41-
$tmp = $template;
42-
while (in_array($tmp->getReferenceType(), ['extends', 'include', NULL], TRUE) && ($tmp = $tmp->getReferringTemplate()));
43-
if (!$tmp) {
44-
self::renderSnippets($template->global->uiControl, $blockQueue, $params);
45-
return TRUE;
46-
}
47-
};
48-
}
49-
50-
51-
public static function renderSnippets(UI\Control $control, array $blockQueue = NULL, array $params = [])
52-
{
53-
$control->snippetMode = FALSE;
54-
$payload = $control->getPresenter()->getPayload();
55-
foreach ($blockQueue as $name => $function) {
56-
if ($name[0] !== '_' || !$control->isControlInvalid((string) substr($name, 1))) {
57-
continue;
58-
}
59-
ob_start(function () {});
60-
$function = reset($function);
61-
$snippets = $function($params + ['_snippetMode' => TRUE]);
62-
$payload->snippets[$id = $control->getSnippetId((string) substr($name, 1))] = ob_get_clean();
63-
if ($snippets !== NULL) { // pass FALSE from snippetArea
64-
if ($snippets) {
65-
$payload->snippets += $snippets;
66-
}
67-
unset($payload->snippets[$id]);
68-
}
69-
}
70-
$control->snippetMode = TRUE;
71-
if ($control instanceof UI\IRenderable) {
72-
$queue = [$control];
73-
do {
74-
foreach (array_shift($queue)->getComponents() as $child) {
75-
if ($child instanceof UI\IRenderable) {
76-
if ($child->isControlInvalid()) {
77-
$child->snippetMode = TRUE;
78-
$child->render();
79-
$child->snippetMode = FALSE;
80-
}
81-
} elseif ($child instanceof Nette\ComponentModel\IContainer) {
82-
$queue[] = $child;
83-
}
84-
}
85-
} while ($queue);
86-
}
8738
}
8839

8940
}

tests/Bridges.Latte/UIMacros.dynamicsnippets.alt.phpt

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/Bridges.Latte/UIMacros.dynamicsnippets.phpt

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/Bridges.Latte/UIMacros.renderSnippets.extends.phpt

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/Bridges.Latte/UIMacros.renderSnippets.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class InnerControl extends Nette\Application\UI\Control
1919
UIMacros::install($latte->getCompiler());
2020
$latte->addProvider('uiPresenter', $this->getPresenter());
2121
$latte->addProvider('uiControl', $this);
22+
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($this));
2223
$params['say'] = 'Hello';
2324
$latte->render(__DIR__ . '/templates/snippet-included.latte', $params);
2425
}
@@ -39,7 +40,7 @@ class TestPresenter extends Nette\Application\UI\Presenter
3940
{
4041
$latte = new Latte\Engine;
4142
UIMacros::install($latte->getCompiler());
42-
$latte->addProvider('uiControl', $this);
43+
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($this));
4344
$latte->render(__DIR__ . '/templates/snippet-include.latte');
4445
}
4546
}
@@ -61,8 +62,8 @@ Assert::same([
6162
'snippet--array2-2' => 'Value 2',
6263
'snippet--array2-3' => 'Value 3',
6364
'snippet--includeSay' => 'Hello include snippet',
64-
'snippet-multi-1-includeSay' => 'Hello',
6565
'snippet--nested1' => "\t<div id=\"snippet--nested2\">Foo</div>",
66+
'snippet-multi-1-includeSay' => 'Hello',
6667
],
6768
], (array) $presenter->payload);
6869

tests/Bridges.Latte/UIMacros.renderSnippets2.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
use Nette\Bridges\ApplicationLatte\UIMacros;
8+
use Nette\Bridges\ApplicationLatte\SnippetBridge;
89
use Tester\Assert;
910

1011

@@ -26,6 +27,7 @@ class InnerControl extends Nette\Application\UI\Control
2627
UIMacros::install($latte->getCompiler());
2728
$latte->addProvider('uiPresenter', $this->getPresenter());
2829
$latte->addProvider('uiControl', $this);
30+
$latte->addProvider('snippetBridge', new SnippetBridge($this));
2931
$params['say'] = 'Hello';
3032
$latte->render('{snippet testA}{$say}{/snippet}', $params);
3133
}
@@ -37,6 +39,7 @@ class InnerControl extends Nette\Application\UI\Control
3739
UIMacros::install($latte->getCompiler());
3840
$latte->addProvider('uiPresenter', $this->getPresenter());
3941
$latte->addProvider('uiControl', $this);
42+
$latte->addProvider('snippetBridge', new SnippetBridge($this));
4043
$params['say'] = 'world';
4144
$latte->render('{snippet testB}{$say}{/snippet}', $params);
4245
}
@@ -58,6 +61,7 @@ class TestPresenter extends Nette\Application\UI\Presenter
5861
$latte->setLoader(new Latte\Loaders\StringLoader);
5962
UIMacros::install($latte->getCompiler());
6063
$latte->addProvider('uiControl', $this);
64+
$latte->addProvider('snippetBridge', new SnippetBridge($this));
6165
$latte->render('');
6266
}
6367
}
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Test: UIMacros, renderSnippets and dynamic snippetArea with included template
4+
* Test: UIMacros, renderSnippets and control wrapped in a snippet
55
*/
66

77
use Nette\Bridges\ApplicationLatte\UIMacros;
@@ -10,27 +10,60 @@ use Tester\Assert;
1010

1111
require __DIR__ . '/../bootstrap.php';
1212

13+
14+
class TestControl extends Nette\Application\UI\Control
15+
{
16+
public function render()
17+
{
18+
$latte = new Latte\Engine;
19+
$latte->setLoader(new Latte\Loaders\StringLoader);
20+
UIMacros::install($latte->getCompiler());
21+
$latte->addProvider('uiPresenter', $this->getPresenter());
22+
$latte->addProvider('uiControl', $this);
23+
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($this));
24+
$latte->render('{snippet foo}hello{/snippet}');
25+
}
26+
27+
28+
}
29+
1330
class TestPresenter extends Nette\Application\UI\Presenter
1431
{
32+
function createComponentTest()
33+
{
34+
return new TestControl;
35+
}
1536

1637
public function render()
1738
{
1839
$latte = new Latte\Engine;
40+
$latte->setLoader(new Latte\Loaders\StringLoader);
1941
UIMacros::install($latte->getCompiler());
2042
$latte->addProvider('uiControl', $this);
21-
$latte->render(__DIR__ . '/templates/snippetArea-include.latte');
43+
$latte->addProvider('snippetBridge', new Nette\Bridges\ApplicationLatte\SnippetBridge($this));
44+
$latte->render('{snippet foo}{control test}{/snippet}');
2245
}
2346
}
2447

2548

2649
$presenter = new TestPresenter;
2750
$presenter->snippetMode = TRUE;
2851
$presenter->redrawControl('foo');
29-
$presenter->redrawControl('data');
52+
$presenter['test']->redrawControl('foo');
53+
$presenter->render();
54+
Assert::same([
55+
'snippets' => [
56+
'snippet--foo' => '<div id="snippet-test-foo">hello</div>',
57+
],
58+
], (array) $presenter->payload);
59+
60+
61+
$presenter = new TestPresenter;
62+
$presenter->snippetMode = TRUE;
63+
$presenter['test']->redrawControl('foo');
3064
$presenter->render();
3165
Assert::same([
3266
'snippets' => [
33-
'snippet--bar-1' => "1\n",
34-
'snippet--bar-2' => "2\n",
67+
'snippet-test-foo' => 'hello',
3568
],
3669
], (array) $presenter->payload);

0 commit comments

Comments
 (0)