Newer
Older
<?php
namespace Syna;
class View
{
protected $engine;
/** @var array */
protected $data = [];
/** @var string */
protected $path;
/** @var array */
protected $sections = [];
/** @var View */
protected $parentTemplate;
/** @var string */
protected $sectionName;
/** @var bool */
protected $appendSection = false;
public function __construct(Factory $engine, string $path, array $data = [])
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{
$this->engine = $engine;
$this->path = $path;
$this->data = array_merge($engine->getSharedData(), $data);
}
public function __toString()
{
return $this->render();
}
public function __call($name, $arguments)
{
return $this->engine->helper($this, $name, ...$arguments);
}
public function addData(array $data): self
{
$this->data = array_merge($this->data, $data);
return $this;
}
public function setSections(array $sections): self
{
$this->sections = $sections;
return $this;
}
public function getSections(): array
{
return $this->sections;
}
public function render(array $data = null): string
{
if ($data) {
$this->addData($data);
}
unset($data);
$v = $this;
$e = [$this, 'escape'];
extract($this->data, EXTR_SKIP);
$level = ob_get_level();
try {
ob_start();
include $this->path;
$content = trim(ob_get_clean());
if ($this->parentTemplate) {
$this->parentTemplate->setSections(array_merge($this->sections, ['content' => $content]));
$content = $this->parentTemplate->render();
}
return $content;
} catch (\Throwable $exception) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $exception;
}
}
public function extend(string $path, array $data = null)
{
$this->parentTemplate = $this->engine->view($path, $data ?? $this->data);
}
public function start(string $name, bool $append = false)
{
if ($name === 'content') {
throw new \LogicException('The section name "content" is reserved.');
}
if ($this->sectionName) {
throw new \LogicException('You cannot nest sections within other sections.');
}
$this->appendSection = $append;
$this->sectionName = $name;
ob_start();
}
public function end()
{
if (is_null($this->sectionName)) {
throw new \LogicException('You must start a section before you can end it.');
}
$this->provide($this->sectionName, ob_get_clean(), $this->appendSection);
$this->sectionName = null;
$this->appendSection = false;
}
public function provide(string $name, string $content, bool $append = false)
{
$content = trim($content, "\t\n\r\0\x0b");
if ($append && isset($this->sections[$name])) {
$this->sections[$name] .= $content;
} else {
$this->sections[$name] = $content;
}
}
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
public function section(string $name, string $default = null): string
{
if (!isset($this->sections[$name])) {
return $default;
}
return $this->sections[$name];
}
public function fetch($name, array $data = array())
{
return $this->engine->render($name, $data);
}
public function insert($name, array $data = [])
{
echo $this->engine->render($name, $data);
}
/**
* Apply multiple functions to variable.
* @param mixed $var
* @param string $functions
* @return mixed
*/
public function batch($var, $functions)
{
foreach (explode('|', $functions) as $function) {
$var = $this->engine->helper($this, $function, $var);
}
return $var;
}
/**
* Escape string.
* @param string $string
* @param null|string $functions
* @return string
*/
public function escape($string, $functions = null)
{
static $flags;
if (!isset($flags)) {
$flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0);
}
if ($functions) {
$string = $this->batch($string, $functions);
}
return htmlspecialchars($string, $flags, 'UTF-8');
}
}