Completed
Push — master ( 547cab...942530 )
by Nikola
02:51
created

BufferManager::getOutput()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 2
nop 0
crap 4
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
final class BufferManager
20
{
21
    /**
22
     * @var BufferQueue
23
     */
24
    private $executionQueue;
25
26
    /**
27
     * @var array
28
     */
29
    private $renderingQueue;
30
31
    /**
32
     * @var string
33
     */
34
    private $output;
35
36 2
    public function __construct()
37
    {
38 2
        $this->executionQueue = new BufferQueue();
39 2
        $this->renderingQueue = [];
40 2
    }
41
42
    /**
43
     * Add closure to buffer execution queue.
44
     *
45
     * @param callable $callable
46
     * @param int $priority
47
     */
48 2
    public function bufferize(callable $callable, $priority = 0)
49
    {
50 2
        $templateBuffer = new TemplateBuffer($callable, $priority);
51
52 2
        $this->executionQueue->enqueue($templateBuffer);
53 2
        $this->renderingQueue[] = $templateBuffer;
54 2
    }
55
56
    /**
57
     * Get output.
58
     *
59
     * @return string
60
     */
61
    public function render()
62
    {
63
        return $this->getOutput();
64
    }
65
66
    /**
67
     * Display output.
68
     *
69
     * @return string
70
     */
71 2
    public function display()
72
    {
73 2
        echo $this->getOutput();
74 2
    }
75
76
    /**
77
     * Execute buffered templates and get output.
78
     *
79
     * @return string
80
     */
81 2
    private function getOutput()
82
    {
83 2
        if (null === $this->output) {
84
85 2
            $this->output = '';
86
87
            /**
88
             * @var TemplateBuffer $templateBuffer
89
             */
90 2
            foreach ($this->executionQueue as $templateBuffer) {
91 2
                $templateBuffer->execute();
92
            }
93
94 2
            foreach ($this->renderingQueue as $templateBuffer) {
95 2
                $this->output .= $templateBuffer->getOutput();
96
            }
97
        }
98
99 2
        return $this->output;
100
    }
101
}
102