BufferManager::display()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * This file is part of the Twig Bufferized Template package, an RunOpenCode project.
4
 *
5
 * (c) 2017 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Twig\BufferizedTemplate\Buffer;
11
12
/**
13
 * Class BufferManager
14
 *
15
 * Buffer manager holds references to Twig template chunks, as well as their execution and rendering order.
16
 *
17
 * @package RunOpenCode\Twig\BufferizedTemplate\Buffer
18
 *
19
 * @internal
20
 */
21
final class BufferManager
22
{
23
    /**
24
     * @var BufferQueue
25
     */
26
    private $executionQueue;
27
28
    /**
29
     * @var array
30
     */
31
    private $renderingQueue;
32
33
    /**
34
     * @var string
35
     */
36
    private $output;
37
38 4
    public function __construct()
39
    {
40 4
        $this->executionQueue = new BufferQueue();
41 4
        $this->renderingQueue = [];
42 4
    }
43
44
    /**
45
     * Add closure to buffer execution queue.
46
     *
47
     * @param callable $callable
48
     * @param int $priority
49
     */
50 4
    public function bufferize(callable $callable, $priority = 0)
51
    {
52 4
        $templateBuffer = new TemplateBuffer($callable, $priority);
53
54 4
        $this->executionQueue->enqueue($templateBuffer);
55 4
        $this->renderingQueue[] = $templateBuffer;
56 4
    }
57
58
    /**
59
     * Get output.
60
     *
61
     * @return string
62
     */
63 1
    public function render()
64
    {
65 1
        return $this->getOutput();
66
    }
67
68
    /**
69
     * Display output.
70
     *
71
     * @return string
72
     */
73 4
    public function display()
74
    {
75 4
        echo $this->getOutput();
76 4
    }
77
78
    /**
79
     * Execute buffered templates and get output.
80
     *
81
     * @return string
82
     */
83 4
    private function getOutput()
84
    {
85 4
        if (null === $this->output) {
86
87 4
            $this->output = '';
88
89
            /**
90
             * @var TemplateBuffer $templateBuffer
91
             */
92 4
            foreach ($this->executionQueue as $templateBuffer) {
93 4
                $templateBuffer->execute();
94
            }
95
96 4
            foreach ($this->renderingQueue as $templateBuffer) {
97 4
                $this->output .= $templateBuffer->getOutput();
98
            }
99
        }
100
101 4
        return $this->output;
102
    }
103
}
104