|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Views\Engine\Native; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Container\ContainerInterface; |
|
15
|
|
|
use Spiral\Core\ContainerScope; |
|
16
|
|
|
use Spiral\Views\ContextInterface; |
|
17
|
|
|
use Spiral\Views\Exception\RenderException; |
|
18
|
|
|
use Spiral\Views\ViewInterface; |
|
19
|
|
|
use Spiral\Views\ViewSource; |
|
20
|
|
|
|
|
21
|
|
|
final class NativeView implements ViewInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/*** @var ViewSource */ |
|
24
|
|
|
protected $view; |
|
25
|
|
|
|
|
26
|
|
|
/** @var ContainerInterface */ |
|
27
|
|
|
protected $container = null; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ContextInterface */ |
|
30
|
|
|
protected $context; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param ViewSource $view |
|
34
|
|
|
* @param ContainerInterface $container |
|
35
|
|
|
* @param ContextInterface $context |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ViewSource $view, ContainerInterface $container, ContextInterface $context) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->view = $view; |
|
40
|
|
|
$this->context = $context; |
|
41
|
|
|
$this->container = $container; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function render(array $data = []): string |
|
48
|
|
|
{ |
|
49
|
|
|
ob_start(); |
|
50
|
|
|
$__outputLevel__ = ob_get_level(); |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
|
|
ContainerScope::runScope($this->container, function () use ($data): void { |
|
54
|
|
|
extract($data, EXTR_OVERWRITE); |
|
55
|
|
|
// render view in context and output buffer scope, context can be accessed using $this->context |
|
56
|
|
|
require $this->view->getFilename(); |
|
57
|
|
|
}); |
|
58
|
|
|
} catch (\Throwable $e) { |
|
59
|
|
|
while (ob_get_level() >= $__outputLevel__) { |
|
60
|
|
|
ob_end_clean(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
throw new RenderException($e); |
|
64
|
|
|
} finally { |
|
65
|
|
|
//Closing all nested buffers |
|
66
|
|
|
while (ob_get_level() > $__outputLevel__) { |
|
67
|
|
|
ob_end_clean(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return ob_get_clean(); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|