Completed
Pull Request — master (#56)
by
unknown
02:00
created

Block   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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