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