Passed
Push — master ( 99f331...02d9ba )
by Alexander
02:17
created

ContentDecorator::begin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $flag of ob_implicit_flush(). ( Ignorable by Annotation )

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

48
        PHP_VERSION_ID >= 80000 ? ob_implicit_flush(/** @scrutinizer ignore-type */ false) : ob_implicit_flush(0);
Loading history...
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);
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
     * @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