Passed
Push — master ( c03bea...c0a5b3 )
by Rustam
01:44
created

ContentDecorator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 70
ccs 17
cts 17
cp 1
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A viewFile() 0 5 1
A start() 0 5 1
A params() 0 5 1
A __construct() 0 3 1
A run() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Widgets;
6
7
use Yiisoft\View\Exception\ViewNotFoundException;
8
use Yiisoft\View\WebView;
9
use Yiisoft\Widget\Widget;
10
11
/**
12
 * ContentDecorator records all output between {@see begin()} and {@see end()]} calls, passes it to the given view file
13
 * as `$content` and then echoes rendering result.
14
 *
15
 * ```php
16
 * <?= ContentDecorator::begin()
17
 *     ->viewFile('@app/views/layouts/base.php')
18
 *     ->params([])
19
 *     ->view($this)
20
 *     ->start(); ?>
21
 *
22
 * some content here
23
 *
24
 * <?= ContentDecorator::end() ?>
25
 * ```
26
 */
27
class ContentDecorator extends Widget
28
{
29
    /**
30
     * @var array the parameters (name => value) to be extracted and made available in the decorative view.
31
     */
32
    private array $params = [];
33
34
    /**
35
     * @var string the view file that will be used to decorate the content enclosed by this widget. This can be
36
     * specified as either the view file path or alias path.
37
     */
38
    private ?string $viewFile = null;
39
40
    private WebView $webView;
41
42 1
    public function __construct(WebView $webView)
43
    {
44 1
        $this->webView = $webView;
45 1
    }
46
47 1
    public function start(): void
48
    {
49
        /* Starts recording a clip. */
50 1
        ob_start();
51 1
        ob_implicit_flush(0);
52 1
    }
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
     * @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);
0 ignored issues
show
Bug introduced by
It seems like $this->viewFile can also be of type null; however, parameter $viewFile of Yiisoft\View\View::renderFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        return $this->webView->renderFile(/** @scrutinizer ignore-type */ $this->viewFile, $params);
Loading history...
69
    }
70
71
    /**
72
     * {@see $params}
73
     *
74
     * @param array $value
75
     *
76
     * @return ContentDecorator
77
     */
78 1
    public function params(array $value): ContentDecorator
79
    {
80 1
        $this->params = $value;
81
82 1
        return $this;
83
    }
84
85
    /**
86
     * {@see $viewFile}
87
     *
88
     * @param string $value
89
     *
90
     * @return ContentDecorator
91
     */
92 1
    public function viewFile(string $value): ContentDecorator
93
    {
94 1
        $this->viewFile = $value;
95
96 1
        return $this;
97
    }
98
}
99