Completed
Pull Request — master (#52)
by Alexander
03:31
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
 * <?= $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
 * @method static Block begin()
37
 * @method static Block end()
38
 */
39
class Block extends Widget
40
{
41
    /**
42
     * @var string $id
43
     */
44
    private $id;
45
46
    /**
47
     * @var bool whether to render the block content in place. Defaults to false, meaning the captured block content
48
     *           will not be displayed.
49
     */
50
    private $renderInPlace = false;
51
52
    /**
53
     * Starts recording a block.
54
     */
55 3
    public function init(): void
56
    {
57 3
        parent::init();
58
59 3
        ob_start();
60 3
        ob_implicit_flush(0);
61
    }
62
63
    /**
64
     * Ends recording a block.
65
     * This method stops output buffering and saves the rendering result as a named block in the view.
66
     *
67
     * @return string the result of widget execution to be outputted.
68
     */
69 3
    public function run(): string
70
    {
71 3
        $block = ob_get_clean();
72
73 3
        if ($this->renderInPlace) {
74 1
            return $block;
75
        }
76
77 2
        if (!empty($block)) {
78 1
            $this->getView()->setBlocks($this->id, $block);
79
        }
80
81 2
        return '';
82
    }
83
84
    /**
85
     * {@see renderInPlace}
86
     *
87
     * @param string $value
88
     *
89
     * @return $this
90
     */
91 2
    public function id(string $value): self
92
    {
93 2
        $this->id = $value;
94
95 2
        return $this;
96
    }
97
98
    /**
99
     * {@see renderInPlace}
100
     *
101
     * @param boolean $value
102
     *
103
     * @return $this
104
     */
105 1
    public function renderInPlace(bool $value): self
106
    {
107 1
        $this->renderInPlace = $value;
108
109 1
        return $this;
110
    }
111
}
112