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