1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Yiisoft\Aliases\Aliases; |
7
|
|
|
use Yiisoft\Strings\Inflector; |
8
|
|
|
use Yiisoft\View\ViewContextInterface; |
9
|
|
|
use Yiisoft\View\WebView; |
10
|
|
|
use Yiisoft\Yii\Web\Data\DataResponseFactoryInterface; |
11
|
|
|
use Yiisoft\Yii\Web\User\User; |
12
|
|
|
|
13
|
|
|
final class ViewRenderer implements ViewContextInterface |
14
|
|
|
{ |
15
|
|
|
protected ?string $name = null; |
16
|
|
|
protected DataResponseFactoryInterface $responseFactory; |
17
|
|
|
protected User $user; |
18
|
|
|
|
19
|
|
|
private Aliases $aliases; |
20
|
|
|
private WebView $view; |
21
|
|
|
private string $layout; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
DataResponseFactoryInterface $responseFactory, |
25
|
|
|
User $user, |
26
|
|
|
Aliases $aliases, |
27
|
|
|
WebView $view |
28
|
|
|
) |
29
|
|
|
{ |
30
|
|
|
$this->responseFactory = $responseFactory; |
31
|
|
|
$this->user = $user; |
32
|
|
|
$this->aliases = $aliases; |
33
|
|
|
$this->view = $view; |
34
|
|
|
$this->layout = $aliases->get('@views') . '/layout/main'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getViewPath(): string |
38
|
|
|
{ |
39
|
|
|
return $this->aliases->get('@views') . '/' . $this->name; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function render(string $view, array $parameters = []): ResponseInterface |
43
|
|
|
{ |
44
|
|
|
$contentRenderer = fn() => $this->renderProxy($view, $parameters); |
45
|
|
|
|
46
|
|
|
return $this->responseFactory->createResponse($contentRenderer); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function renderPartial(string $view, array $parameters = []): ResponseInterface |
50
|
|
|
{ |
51
|
|
|
$content = $this->view->render($view, $parameters, $this); |
52
|
|
|
|
53
|
|
|
return $this->responseFactory->createResponse($content); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function withControllerName(string $name): self |
57
|
|
|
{ |
58
|
|
|
$new = clone $this; |
59
|
|
|
$new->name = $name; |
60
|
|
|
|
61
|
|
|
return $new; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function withLayout(string $layout): self |
65
|
|
|
{ |
66
|
|
|
$new = clone $this; |
67
|
|
|
$new->layout = $layout; |
68
|
|
|
|
69
|
|
|
return $new; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function renderProxy(string $view, array $parameters = []): string |
73
|
|
|
{ |
74
|
|
|
$content = $this->view->render($view, $parameters, $this); |
75
|
|
|
$user = $this->user->getIdentity(); |
76
|
|
|
$layout = $this->findLayoutFile($this->layout); |
77
|
|
|
|
78
|
|
|
if ($layout === null) { |
|
|
|
|
79
|
|
|
return $content; |
80
|
|
|
} |
81
|
|
|
return $this->view->renderFile( |
82
|
|
|
$layout, |
83
|
|
|
[ |
84
|
|
|
'content' => $content, |
85
|
|
|
'user' => $user, |
86
|
|
|
], |
87
|
|
|
$this |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function findLayoutFile(?string $file): ?string |
92
|
|
|
{ |
93
|
|
|
if ($file === null) { |
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (pathinfo($file, PATHINFO_EXTENSION) !== '') { |
98
|
|
|
return $file; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $file . '.' . $this->view->getDefaultExtension(); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|