1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\LazyRendering\View; |
4
|
|
|
|
5
|
|
|
use App\Asset\AppAsset; |
6
|
|
|
use App\Widget\PerformanceMetrics; |
7
|
|
|
use Generator; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Yiisoft\Assets\AssetManager; |
10
|
|
|
use Yiisoft\Html\Html; |
11
|
|
|
|
12
|
|
|
class MainLayout |
13
|
|
|
{ |
14
|
|
|
private AssetManager $assetManager; |
15
|
|
|
private ?string $title = null; |
16
|
|
|
private array $metaTags = [ |
17
|
|
|
['charset' => 'UTF-8'], |
18
|
|
|
[ |
19
|
|
|
'name' => 'viewport', |
20
|
|
|
'content' => 'width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0', |
21
|
|
|
], |
22
|
|
|
['http-equiv' => 'X-UA-Compatible', 'content' => 'ie=edge'], |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public function __construct(AssetManager $assetManager) |
26
|
|
|
{ |
27
|
|
|
$this->assetManager = $assetManager; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function render(iterable $content, RequestInterface $request): iterable |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
ob_start(); |
33
|
|
|
foreach ($this->renderPage($content) as $contentPart) { |
34
|
|
|
yield ob_get_clean() . $contentPart; |
|
|
|
|
35
|
|
|
ob_start(); |
36
|
|
|
} |
37
|
|
|
ob_end_clean(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function renderPage(iterable $content): Generator |
41
|
|
|
{ |
42
|
|
|
$this->assetManager->register([AppAsset::class]); |
43
|
|
|
|
44
|
|
|
echo '<html><head>' . ($this->title !== null ? '<title>' . Html::encode($this->title) . '</title>' : ''); |
45
|
|
|
// Meta tags |
46
|
|
|
foreach ($this->metaTags as $value) { |
47
|
|
|
echo Html::tag('meta', '', $value); |
48
|
|
|
} |
49
|
|
|
// CSS files |
50
|
|
|
foreach ($this->assetManager->getCssFiles() as $value) { |
51
|
|
|
echo Html::cssFile($value['url'], $value['attributes']); |
52
|
|
|
} |
53
|
|
|
yield '</head><body><main role="main" class="container py-4">'; |
54
|
|
|
// Content |
55
|
|
|
yield from $content; |
56
|
|
|
echo '</main><footer class="container py-4">'; |
57
|
|
|
echo PerformanceMetrics::widget(); |
58
|
|
|
echo '</footer>'; |
59
|
|
|
// JS files |
60
|
|
|
foreach ($this->assetManager->getJsFiles() as $value) { |
61
|
|
|
echo Html::script($value['url'], $value['attributes']); |
62
|
|
|
} |
63
|
|
|
yield '</body></html>'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.