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