Passed
Push — scrutinizer-migrate-to-new-eng... ( 58afd6 )
by Alexander
18:11
created

ContentDecorator::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\widgets;
9
10
use yii\base\InvalidConfigException;
11
use yii\base\Widget;
12
13
/**
14
 * ContentDecorator records all output between [[begin()]] and [[end()]] calls, passes it to the given view file
15
 * as `$content` and then echoes rendering result.
16
 *
17
 * ```php
18
 * <?php ContentDecorator::begin([
19
 *     'viewFile' => '@app/views/layouts/base.php',
20
 *     'params' => [],
21
 *     'view' => $this,
22
 * ]) ?>
23
 *
24
 * some content here
25
 *
26
 * <?php ContentDecorator::end() ?>
27
 * ```
28
 *
29
 * There are [[\yii\base\View::beginContent()]] and [[\yii\base\View::endContent()]] wrapper methods in the
30
 * [[\yii\base\View]] component to make syntax more friendly. In the view these could be used as follows:
31
 *
32
 * ```php
33
 * <?php $this->beginContent('@app/views/layouts/base.php') ?>
34
 *
35
 * some content here
36
 *
37
 * <?php $this->endContent() ?>
38
 * ```
39
 *
40
 * @author Qiang Xue <[email protected]>
41
 * @since 2.0
42
 */
43
class ContentDecorator extends Widget
44
{
45
    /**
46
     * @var string the view file that will be used to decorate the content enclosed by this widget.
47
     * This can be specified as either the view file path or [path alias](guide:concept-aliases).
48
     */
49
    public $viewFile;
50
    /**
51
     * @var array the parameters (name => value) to be extracted and made available in the decorative view.
52
     */
53
    public $params = [];
54
55
56
    /**
57
     * Starts recording a clip.
58
     */
59 1
    public function init()
60
    {
61 1
        parent::init();
62
63 1
        if ($this->viewFile === null) {
64
            throw new InvalidConfigException('ContentDecorator::viewFile must be set.');
65
        }
66 1
        ob_start();
67 1
        ob_implicit_flush(false);
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

67
        ob_implicit_flush(/** @scrutinizer ignore-type */ false);
Loading history...
68 1
    }
69
70
    /**
71
     * Ends recording a clip.
72
     * This method stops output buffering and saves the rendering result as a named clip in the controller.
73
     */
74
    public function run()
75
    {
76
        $params = $this->params;
77
        $params['content'] = ob_get_clean();
78
        // render under the existing context
79
        echo $this->view->renderFile($this->viewFile, $params);
80
    }
81
}
82