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

Block::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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\Widget;
11
12
/**
13
 * Block records all output between [[begin()]] and [[end()]] calls and stores it in [[\yii\base\View::$blocks]].
14
 * for later use.
15
 *
16
 * [[\yii\base\View]] component contains two methods [[\yii\base\View::beginBlock()]] and [[\yii\base\View::endBlock()]].
17
 * The general idea is that you're defining block default in a view or layout:
18
 *
19
 * ```php
20
 * <?php $this->beginBlock('messages', true) ?>
21
 * Nothing.
22
 * <?php $this->endBlock() ?>
23
 * ```
24
 *
25
 * And then overriding default in sub-views:
26
 *
27
 * ```php
28
 * <?php $this->beginBlock('username') ?>
29
 * Umm... hello?
30
 * <?php $this->endBlock() ?>
31
 * ```
32
 *
33
 * Second parameter defines if block content should be outputted which is desired when rendering its content but isn't
34
 * desired when redefining it in subviews.
35
 *
36
 * @author Qiang Xue <[email protected]>
37
 * @since 2.0
38
 */
39
class Block extends Widget
40
{
41
    /**
42
     * @var bool whether to render the block content in place. Defaults to false,
43
     * meaning the captured block content will not be displayed.
44
     */
45
    public $renderInPlace = false;
46
47
48
    /**
49
     * Starts recording a block.
50
     */
51 1
    public function init()
52
    {
53 1
        parent::init();
54
55 1
        ob_start();
56 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

56
        ob_implicit_flush(/** @scrutinizer ignore-type */ false);
Loading history...
57 1
    }
58
59
    /**
60
     * Ends recording a block.
61
     * This method stops output buffering and saves the rendering result as a named block in the view.
62
     */
63
    public function run()
64
    {
65
        $block = ob_get_clean();
66
        if ($this->renderInPlace) {
67
            echo $block;
68
        }
69
        $this->view->blocks[$this->getId()] = $block;
70
    }
71
}
72