1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Stream\Data; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Aliases\Aliases; |
8
|
|
|
use Yiisoft\View\WebView; |
9
|
|
|
|
10
|
|
|
class WebViewConverter implements Converter, \Yiisoft\View\ViewContextInterface |
11
|
|
|
{ |
12
|
|
|
protected WebView $webView; |
13
|
|
|
protected Aliases $aliases; |
14
|
|
|
protected ?string $viewPath = null; |
15
|
|
|
|
16
|
|
|
public function __construct(WebView $webView, Aliases $aliases) |
17
|
|
|
{ |
18
|
|
|
$this->webView = $webView; |
19
|
|
|
$this->aliases = $aliases; |
20
|
|
|
} |
21
|
|
|
public static function getFormat(): string |
22
|
|
|
{ |
23
|
|
|
return 'text/html'; |
24
|
|
|
} |
25
|
|
|
public function convert($data, array $params = []): string |
26
|
|
|
{ |
27
|
|
|
if (!array_key_exists('view', $params)) { |
28
|
|
|
throw new \InvalidArgumentException('View should be defined in the params array'); |
29
|
|
|
} |
30
|
|
|
if (array_key_exists('viewPath', $params)) { |
31
|
|
|
$this->viewPath = $this->aliases->get($params['viewPath']); |
32
|
|
|
} |
33
|
|
|
$page = $this->webView->render( |
34
|
|
|
$this->aliases->get($params['view']), |
35
|
|
|
$data, |
36
|
|
|
$this->viewPath !== null ? $this : null |
37
|
|
|
); |
38
|
|
|
// render layout |
39
|
|
|
if (!array_key_exists('layout', $params)) { |
40
|
|
|
return $page; |
41
|
|
|
} else { |
42
|
|
|
return $this->webView->renderFile( |
43
|
|
|
$this->aliases->get($params['layout']), |
44
|
|
|
$this->getLayoutParams($page), |
45
|
|
|
$this->viewPath !== null ? $this : null |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
public function getViewPath(): string |
50
|
|
|
{ |
51
|
|
|
return $this->viewPath; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
protected function getLayoutParams(string $pageContent): array |
54
|
|
|
{ |
55
|
|
|
return ['content' => $pageContent]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|