1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Widgets; |
6
|
|
|
|
7
|
|
|
use Throwable; |
8
|
|
|
use Yiisoft\Aliases\Aliases; |
9
|
|
|
use Yiisoft\View\Exception\ViewNotFoundException; |
10
|
|
|
use Yiisoft\View\WebView; |
11
|
|
|
use Yiisoft\Widget\Widget; |
12
|
|
|
|
13
|
|
|
use function ob_get_clean; |
14
|
|
|
use function ob_implicit_flush; |
15
|
|
|
use function ob_start; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ContentDecorator records all output between {@see Widget::begin()} and {@see Widget::end()} calls, |
19
|
|
|
* passes it to the given view file as `$content` and then echoes rendering result. |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* <?= ContentDecorator::widget() |
23
|
|
|
* ->viewFile('@app/views/layouts/base.php') |
24
|
|
|
* ->parameters([]) |
25
|
|
|
* ->begin(); |
26
|
|
|
* ?> |
27
|
|
|
* |
28
|
|
|
* some content here |
29
|
|
|
* |
30
|
|
|
* <?= ContentDecorator::end() ?> |
31
|
|
|
* ``` |
32
|
|
|
*/ |
33
|
|
|
final class ContentDecorator extends Widget |
34
|
|
|
{ |
35
|
|
|
private Aliases $aliases; |
36
|
|
|
private array $parameters = []; |
37
|
|
|
private string $viewFile = ''; |
38
|
|
|
private WebView $webView; |
39
|
|
|
|
40
|
2 |
|
public function __construct(Aliases $aliases, WebView $webView) |
41
|
|
|
{ |
42
|
2 |
|
$this->aliases = $aliases; |
43
|
2 |
|
$this->webView = $webView; |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns a new instance with the specified parameters. |
48
|
|
|
* |
49
|
|
|
* @param array $value The parameters (name => value) to be extracted and made available in the decorative view. |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
1 |
|
public function parameters(array $value): self |
54
|
|
|
{ |
55
|
1 |
|
$new = clone $this; |
56
|
1 |
|
$new->parameters = $value; |
57
|
1 |
|
return $new; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns a new instance with the specified view file. |
62
|
|
|
* |
63
|
|
|
* @param string $value The view file that will be used to decorate the content enclosed by this widget. |
64
|
|
|
* This can be specified as either the view file path or alias path. |
65
|
|
|
* |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
2 |
|
public function viewFile(string $value): self |
69
|
|
|
{ |
70
|
2 |
|
$new = clone $this; |
71
|
2 |
|
$new->viewFile = $this->aliases->get($value); |
72
|
2 |
|
return $new; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function begin(): ?string |
76
|
|
|
{ |
77
|
1 |
|
parent::begin(); |
78
|
1 |
|
ob_start(); |
79
|
1 |
|
PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0); |
80
|
1 |
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Ends recording a clip. |
85
|
|
|
* |
86
|
|
|
* This method stops output buffering and saves the rendering result as a named clip in the controller. |
87
|
|
|
* |
88
|
|
|
* @throws Throwable|ViewNotFoundException |
89
|
|
|
* |
90
|
|
|
* @return string The result of widget execution to be outputted. |
91
|
|
|
*/ |
92
|
1 |
|
protected function run(): string |
93
|
|
|
{ |
94
|
1 |
|
$parameters = $this->parameters; |
95
|
1 |
|
$parameters['content'] = ob_get_clean(); |
96
|
|
|
|
97
|
|
|
/** render under the existing context */ |
98
|
1 |
|
return $this->webView->renderFile($this->viewFile, $parameters); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|