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