Passed
Push — master ( c03bea...c0a5b3 )
by Rustam
01:44
created

Block   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 71
ccs 20
cts 20
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A renderInPlace() 0 5 1
A __construct() 0 3 1
A id() 0 5 1
A run() 0 13 3
A start() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Widgets;
6
7
use Yiisoft\View\WebView;
8
use Yiisoft\Widget\Widget;
9
10
/**
11
 * Block records all output between {@see begin()} and {@see end()} calls and stores it in
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
class Block extends Widget
37
{
38
    private string $id;
39
40
    /**
41
     * @var bool whether to render the block content in place. Defaults to false, meaning the captured block content
42
     * will not be displayed.
43
     */
44
    private bool $renderInPlace = false;
45
46
    private WebView $webView;
47
48 2
    public function __construct(WebView $webView)
49
    {
50 2
        $this->webView = $webView;
51 2
    }
52
53
    /**
54
     * Starts recording a block.
55
     */
56 2
    public function start(): void
57
    {
58 2
        ob_start();
59 2
        ob_implicit_flush(0);
60 2
    }
61
62
    /**
63
     * Ends recording a block.
64
     * This method stops output buffering and saves the rendering result as a named block in the view.
65
     *
66
     * @return string the result of widget execution to be outputted.
67
     */
68 2
    public function run(): string
69
    {
70 2
        $block = ob_get_clean();
71
72 2
        if ($this->renderInPlace) {
73 1
            return $block;
74
        }
75
76 1
        if (!empty($block)) {
77 1
            $this->webView->setBlocks($this->id, $block);
78
        }
79
80 1
        return '';
81
    }
82
83
    /**
84
     * @param string $value
85
     *
86
     * @return Block
87
     */
88 2
    public function id(string $value): Block
89
    {
90 2
        $this->id = $value;
91
92 2
        return $this;
93
    }
94
95
    /**
96
     * {@see $renderInPlace}
97
     *
98
     * @param bool $value
99
     *
100
     * @return Block
101
     */
102 1
    public function renderInPlace(bool $value): Block
103
    {
104 1
        $this->renderInPlace = $value;
105
106 1
        return $this;
107
    }
108
}
109