Completed
Push — master ( 69416a...547cab )
by Nikola
03:28
created

BufferManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 83
ccs 21
cts 23
cp 0.913
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A bufferize() 0 7 1
A render() 0 4 1
A display() 0 4 1
A getOutput() 0 19 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 = array();
40 2
        $this->output = null;
41 2
    }
42
43
    /**
44
     * Add closure to buffer execution queue.
45
     *
46
     * @param callable $callable
47
     * @param int $priority
48
     */
49 2
    public function bufferize(callable $callable, $priority = 0)
50
    {
51 2
        $templateBuffer = new TemplateBuffer($callable, $priority);
52
53 2
        $this->executionQueue->enqueue($templateBuffer);
54 2
        $this->renderingQueue[] = $templateBuffer;
55 2
    }
56
57
    /**
58
     * Get output.
59
     *
60
     * @return string
61
     */
62
    public function render()
63
    {
64
        return $this->getOutput();
65
    }
66
67
    /**
68
     * Display output.
69
     *
70
     * @return string
71
     */
72 2
    public function display()
73
    {
74 2
        echo $this->getOutput();
75 2
    }
76
77
    /**
78
     * Execute buffered templates and get output.
79
     *
80
     * @return string
81
     */
82 2
    private function getOutput()
83
    {
84 2
        if (is_null($this->output)) {
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