-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRenderer.php
241 lines (195 loc) · 7.04 KB
/
Renderer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
declare(strict_types=1);
namespace ManaPHP\Rendering;
use ManaPHP\AliasInterface;
use ManaPHP\Context\ContextTrait;
use ManaPHP\Coroutine\Mutex;
use ManaPHP\Di\Attribute\Autowired;
use ManaPHP\Exception\FileNotFoundException;
use ManaPHP\Exception\MisuseException;
use ManaPHP\Exception\PreconditionException;
use ManaPHP\Rendering\Renderer\Event\RendererRendered;
use ManaPHP\Rendering\Renderer\Event\RendererRendering;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use function dirname;
class Renderer implements RendererInterface
{
use ContextTrait;
#[Autowired] protected EventDispatcherInterface $eventDispatcher;
#[Autowired] protected AliasInterface $alias;
#[Autowired] protected ContainerInterface $container;
#[Autowired] protected array $engines
= ['.phtml' => 'ManaPHP\Rendering\Engine\Php',
'.sword' => 'ManaPHP\Rendering\Engine\Sword'];
protected array $files = [];
protected Mutex $mutex;
protected function getMutex(): Mutex
{
return $this->mutex ??= new Mutex();
}
public function lock(): void
{
$this->getMutex()->lock();
}
public function unlock(): void
{
$this->getMutex()->unlock();
}
public function render(string $template, array $vars = [], bool $directOutput = false): ?string
{
/** @var RendererContext $context */
$context = $this->getContext();
if (!$this->getMutex()->isLocked()) {
throw new MisuseException('renderer is not locked');
}
if (DIRECTORY_SEPARATOR === '\\' && str_contains($template, '\\')) {
$template = str_replace('\\', '/', $template);
}
if (!str_contains($template, '/')) {
$template = dirname(end($context->templates)) . '/' . $template;
}
$template = $this->alias->resolve($template);
if (isset($this->files[$template])) {
list($file, $extension) = $this->files[$template];
} else {
$file = null;
$extension = null;
foreach ($this->engines as $extension => $engine) {
if (is_file($tmp = $template . $extension)) {
if (PHP_EOL !== "\n") {
$realPath = strtr(realpath($tmp), '\\', '/');
if ($tmp !== $realPath) {
trigger_error("File name ($realPath) case mismatch for $tmp", E_USER_ERROR);
}
}
$file = $tmp;
break;
}
}
if (!$file) {
$extensions = implode(', or ', array_keys($this->engines));
throw new FileNotFoundException(['`{1}` with `{2}` extension was not found', $template, $extensions]);
}
$this->files[$template] = [$file, $extension];
}
/** @var EngineInterface $engine */
$engine = $this->container->get($this->engines[$extension]);
if (isset($vars['renderer'])) {
throw new MisuseException('variable `renderer` is reserved for renderer');
}
$context->templates[] = $template;
$this->eventDispatcher->dispatch(new RendererRendering($this, $template, $file, $vars));
$vars['renderer'] = $this;
if ($directOutput) {
$engine->render($file, $vars);
$content = null;
} else {
ob_start();
ob_implicit_flush(false);
try {
$engine->render($file, $vars);
} finally {
$content = ob_get_clean();
}
}
$this->eventDispatcher->dispatch(new RendererRendered($this, $template, $file, $vars));
array_pop($context->templates);
return $content;
}
public function renderFile(string $file, array $vars = []): string
{
$this->lock();
try {
return $this->render($file, $vars);
} finally {
$this->unlock();
}
}
public function partial(string $path, array $vars = []): void
{
$this->render($path, $vars, true);
}
public function exists(string $template): bool
{
/** @var RendererContext $context */
$context = $this->getContext();
if (DIRECTORY_SEPARATOR === '\\' && str_contains($template, '\\')) {
$template = str_replace('\\', '/', $template);
}
if (!str_contains($template, '/')) {
$template = dirname(end($context->templates)) . '/' . $template;
}
$template = $this->alias->resolve($template);
if (isset($this->files[$template])) {
return true;
}
foreach ($this->engines as $extension => $_) {
if (is_file($file = $template . $extension)) {
if (PHP_EOL !== "\n") {
$realPath = strtr(realpath($file), '\\', '/');
if ($file !== $realPath) {
trigger_error("File name ($realPath) case mismatch for $file", E_USER_ERROR);
}
}
$this->files[$template] = [$file, $extension];
return true;
}
}
return false;
}
/**
* Get the string contents of a section.
*
* @param string $section
* @param string $default
*
* @return string
*/
public function getSection(string $section, string $default = ''): string
{
/** @var RendererContext $context */
$context = $this->getContext();
return $context->sections[$section] ?? $default;
}
public function startSection(string $section, ?string $default = null): void
{
/** @var RendererContext $context */
$context = $this->getContext();
if ($default === null) {
ob_start();
ob_implicit_flush(false);
$context->stack[] = $section;
} else {
$context->sections[$section] = $default;
}
}
public function stopSection(bool $overwrite = false): void
{
/** @var RendererContext $context */
$context = $this->getContext();
if (!$context->stack) {
throw new PreconditionException('cannot stop a section without first starting session');
}
$last = array_pop($context->stack);
if ($overwrite || !isset($context->sections[$last])) {
$context->sections[$last] = ob_get_clean();
} else {
$context->sections[$last] .= ob_get_clean();
}
}
public function appendSection(): void
{
/** @var RendererContext $context */
$context = $this->getContext();
if (!$context->stack) {
throw new PreconditionException('Cannot append a section without first starting one:');
}
$last = array_pop($context->stack);
if (isset($context->sections[$last])) {
$context->sections[$last] .= ob_get_clean();
} else {
$context->sections[$last] = ob_get_clean();
}
}
}