1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\View; |
6
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Psr\EventDispatcher\StoppableEventInterface; |
9
|
|
|
use Yiisoft\View\Event\AfterRenderEventInterface; |
10
|
|
|
use Yiisoft\View\Event\View\AfterRender; |
11
|
|
|
use Yiisoft\View\Event\View\BeforeRender; |
12
|
|
|
use Yiisoft\View\Event\View\PageBegin; |
13
|
|
|
use Yiisoft\View\Event\View\PageEnd; |
14
|
|
|
use Yiisoft\View\State\ViewState; |
15
|
|
|
|
16
|
|
|
use function ob_end_flush; |
17
|
|
|
use function ob_implicit_flush; |
18
|
|
|
use function ob_start; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* View represents an instance of a view for use in an any environment. |
22
|
|
|
* |
23
|
|
|
* View provides a set of methods (e.g. {@see View::render()}) for rendering purpose. |
24
|
|
|
*/ |
25
|
|
|
final class View implements ViewInterface |
26
|
|
|
{ |
27
|
|
|
use ViewTrait; |
28
|
|
|
|
29
|
|
|
private ViewState $state; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $basePath The full path to the base directory of views. |
33
|
|
|
* @param EventDispatcherInterface $eventDispatcher The event dispatcher instance. |
34
|
|
|
*/ |
35
|
33 |
|
public function __construct(string $basePath, EventDispatcherInterface $eventDispatcher) |
36
|
|
|
{ |
37
|
33 |
|
$this->basePath = $basePath; |
38
|
33 |
|
$this->state = new ViewState(); |
39
|
33 |
|
$this->eventDispatcher = $eventDispatcher; |
40
|
33 |
|
$this->setPlaceholderSalt(__DIR__); |
41
|
33 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a new instance with cleared state (blocks, parameters, etc.) |
45
|
|
|
* |
46
|
|
|
* @return static |
47
|
|
|
*/ |
48
|
2 |
|
public function withClearedState(): self |
49
|
|
|
{ |
50
|
2 |
|
$new = clone $this; |
51
|
2 |
|
$new->state = new ViewState(); |
52
|
2 |
|
return $new; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Marks the beginning of a view. |
57
|
|
|
*/ |
58
|
1 |
|
public function beginPage(): void |
59
|
|
|
{ |
60
|
1 |
|
ob_start(); |
61
|
|
|
/** @psalm-suppress InvalidArgument */ |
62
|
1 |
|
PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0); |
63
|
|
|
|
64
|
1 |
|
$this->eventDispatcher->dispatch(new PageBegin($this)); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Marks the ending of a view. |
69
|
|
|
*/ |
70
|
1 |
|
public function endPage(): void |
71
|
|
|
{ |
72
|
1 |
|
$this->eventDispatcher->dispatch(new PageEnd($this)); |
73
|
|
|
|
74
|
1 |
|
ob_end_flush(); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
9 |
|
protected function createBeforeRenderEvent(string $viewFile, array $parameters): StoppableEventInterface |
78
|
|
|
{ |
79
|
9 |
|
return new BeforeRender($this, $viewFile, $parameters); |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
protected function createAfterRenderEvent( |
83
|
|
|
string $viewFile, |
84
|
|
|
array $parameters, |
85
|
|
|
string $result |
86
|
|
|
): AfterRenderEventInterface { |
87
|
8 |
|
return new AfterRender($this, $viewFile, $parameters, $result); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|