1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Twig Bufferized Template package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2015 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
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->executionQueue = new BufferQueue(); |
39
|
|
|
$this->renderingQueue = array(); |
40
|
|
|
$this->output = null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Add closure to buffer execution queue. |
45
|
|
|
* |
46
|
|
|
* @param callable $callable |
47
|
|
|
* @param int $priority |
48
|
|
|
*/ |
49
|
|
|
public function bufferize(callable $callable, $priority = 0) |
50
|
|
|
{ |
51
|
|
|
$templateBuffer = new TemplateBuffer($callable, $priority); |
52
|
|
|
|
53
|
|
|
$this->executionQueue->enqueue($templateBuffer); |
54
|
|
|
$this->renderingQueue[] = $templateBuffer; |
55
|
|
|
} |
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
|
|
|
public function display() |
73
|
|
|
{ |
74
|
|
|
echo $this->getOutput(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Execute buffered templates and get output. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
private function getOutput() |
83
|
|
|
{ |
84
|
|
|
if (is_null($this->output)) { |
85
|
|
|
$this->output = ''; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var TemplateBuffer $templateBuffer |
89
|
|
|
*/ |
90
|
|
|
foreach ($templateBuffers = $this->executionQueue as $templateBuffer) { |
91
|
|
|
$templateBuffer->execute(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
foreach ($templateBuffers = $this->renderingQueue as $templateBuffer) { |
95
|
|
|
$this->output .= $templateBuffer->getOutput(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->output; |
100
|
|
|
} |
101
|
|
|
} |