Completed
Pull Request — master (#52)
by Wilmer
01:36
created

Block   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 16
dl 0
loc 71
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 13 3
A id() 0 5 1
A renderInPlace() 0 5 1
A init() 0 6 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Yiisoft\Widget;
5
6
/**
7
 * Block records all output between {@see begin()} and {@see end()} calls and stores it in
8
 * {@see \Yiisoft\View\View::$blocks}.
9
 *
10
 * {@see \Yiisoft\View\View} component contains two methods {\Yiisoft\View\View::beginBlock()} and
11
 * {[\Yiisoft\View\View::endBlock()}.
12
 *
13
 * The general idea is that you're defining block default in a view or layout:
14
 *
15
 * ```php
16
 * <?php $this->beginBlock('index') ?>
17
 * Nothing.
18
 * <?php $this->endBlock() ?>
19
 * ```
20
 *
21
 * And then overriding default in views:
22
 *
23
 * ```php
24
 * <?php $this->beginBlock('index') ?>
25
 * Umm... hello?
26
 * <?php $this->endBlock() ?>
27
 * ```
28
 *
29
 * in subviews show block:
30
 *
31
 * <?php echo $this->getBlock('index');
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
class Block extends Widget
37
{
38
    /**
39
     * @var string $id
40
     */
41
    private $id;
42
43
    /**
44
     * @var bool whether to render the block content in place. Defaults to false, meaning the captured block content
45
     *           will not be displayed.
46
     */
47
    private $renderInPlace = false;
48
49
    /**
50
     * Starts recording a block.
51
     */
52 3
    public function init(): void
53
    {
54 3
        parent::init();
55
56 3
        ob_start();
57 3
        ob_implicit_flush(0);
58
    }
59
60
    /**
61
     * Ends recording a block.
62
     * This method stops output buffering and saves the rendering result as a named block in the view.
63
     *
64
     * @return string the result of widget execution to be outputted.
65
     */
66 3
    public function run(): string
67
    {
68 3
        $block = ob_get_clean();
69
70 3
        if ($this->renderInPlace) {
71 1
            return $block;
72
        }
73
74 2
        if (!empty($block)) {
75 1
            $this->getView()->setBlocks($this->id, $block);
76
        }
77
78 2
        return '';
79
    }
80
81
    /**
82
     * {@see renderInPlace}
83
     *
84
     * @param boolean $value
85
     *
86
     * @return Widget
87
     */
88 2
    public function id(string $value): Widget
89
    {
90 2
        $this->id = $value;
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * {@see renderInPlace}
97
     *
98
     * @param boolean $value
99
     *
100
     * @return Widget
101
     */
102 1
    public function renderInPlace(bool $value): Widget
103
    {
104 1
        $this->renderInPlace = $value;
105
106 1
        return $this;
107
    }
108
}
109