1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the class {@see \AppUtils\OutputBuffering}. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage Output Buffering |
7
|
|
|
* @see \AppUtils\OutputBuffering |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Wrapper around the native PHP output buffering methods, |
16
|
|
|
* using exceptions to avoid having to check the return |
17
|
|
|
* values of the methods. |
18
|
|
|
* |
19
|
|
|
* It is limited to a typical output buffering usage: |
20
|
|
|
* starting a buffer, and retrieving or flushing its contents, |
21
|
|
|
* stopping the buffer in the process. Any more complex |
22
|
|
|
* buffer usage will have to use the PHP functions. |
23
|
|
|
* |
24
|
|
|
* @package Application Utils |
25
|
|
|
* @subpackage Output Buffering |
26
|
|
|
* @author Sebastian Mordziol <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class OutputBuffering |
29
|
|
|
{ |
30
|
|
|
const ERROR_CANNOT_START = 91501; |
31
|
|
|
const ERROR_CANNOT_GET_BUFFER = 91502; |
32
|
|
|
const ERROR_BUFFER_NOT_STARTED = 91503; |
33
|
|
|
const ERROR_CANNOT_STOP_BUFFER = 91504; |
34
|
|
|
const ERROR_CANNOT_FLUSH_BUFFER = 91505; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int[] |
38
|
|
|
*/ |
39
|
|
|
private static $stack = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Checks whether any level of output buffering is currently active, |
43
|
|
|
* but only the helper's output buffering. The native PHP buffering |
44
|
|
|
* is ignored. |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
* @see OutputBuffering::setBaseBufferLevel() |
48
|
|
|
*/ |
49
|
|
|
public static function isActive() : bool |
50
|
|
|
{ |
51
|
|
|
return self::getLevel() > 0; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
|
|
public static function getLevel() : int |
58
|
|
|
{ |
59
|
|
|
return count(self::$stack); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Starts a new output buffer. |
64
|
|
|
* |
65
|
|
|
* @throws OutputBuffering_Exception |
66
|
|
|
* @see OutputBuffering::ERROR_CANNOT_START |
67
|
|
|
*/ |
68
|
|
|
public static function start() : void |
69
|
|
|
{ |
70
|
|
|
self::$stack[] = 1; |
71
|
|
|
|
72
|
|
|
if(ob_start() === true) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new OutputBuffering_Exception( |
77
|
|
|
'Cannot start output buffering.', |
78
|
|
|
'ob_start returned false.', |
79
|
|
|
self::ERROR_CANNOT_START |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Stops the output buffer, discarding the captured content. |
85
|
|
|
* |
86
|
|
|
* @throws OutputBuffering_Exception |
87
|
|
|
* @see OutputBuffering::ERROR_BUFFER_NOT_STARTED |
88
|
|
|
* @see OutputBuffering::ERROR_CANNOT_STOP_BUFFER |
89
|
|
|
*/ |
90
|
|
|
public static function stop() : void |
91
|
|
|
{ |
92
|
|
|
self::_stop(); |
93
|
|
|
|
94
|
|
|
if(ob_end_clean() !== false) |
95
|
|
|
{ |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
throw new OutputBuffering_Exception( |
100
|
|
|
'Cannot stop the output buffer.', |
101
|
|
|
'The ob_end_clean call returned false.', |
102
|
|
|
self::ERROR_CANNOT_STOP_BUFFER |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @throws OutputBuffering_Exception |
108
|
|
|
*/ |
109
|
|
|
private static function _stop() : void |
110
|
|
|
{ |
111
|
|
|
if(empty(self::$stack)) |
112
|
|
|
{ |
113
|
|
|
throw new OutputBuffering_Exception( |
114
|
|
|
'Output buffering is not active', |
115
|
|
|
'Tried to get the output buffer, but none was active.', |
116
|
|
|
self::ERROR_BUFFER_NOT_STARTED |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
array_pop(self::$stack); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Flushes the captured output buffer to standard output. |
125
|
|
|
* |
126
|
|
|
* @throws OutputBuffering_Exception |
127
|
|
|
* @see OutputBuffering::ERROR_BUFFER_NOT_STARTED |
128
|
|
|
* @see OutputBuffering::ERROR_CANNOT_FLUSH_BUFFER |
129
|
|
|
*/ |
130
|
|
|
public static function flush() : void |
131
|
|
|
{ |
132
|
|
|
self::_stop(); |
133
|
|
|
|
134
|
|
|
if(ob_end_flush() !== false) |
135
|
|
|
{ |
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
throw new OutputBuffering_Exception( |
140
|
|
|
'Cannot flush the output buffer.', |
141
|
|
|
'ob_end_flush returned false.', |
142
|
|
|
self::ERROR_CANNOT_FLUSH_BUFFER |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Retrieves the current output buffer's contents, |
148
|
|
|
* and stops the output buffer. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
* |
152
|
|
|
* @throws OutputBuffering_Exception |
153
|
|
|
* @see OutputBuffering::ERROR_BUFFER_NOT_STARTED |
154
|
|
|
* @see OutputBuffering::ERROR_CANNOT_GET_BUFFER |
155
|
|
|
*/ |
156
|
|
|
public static function get() : string |
157
|
|
|
{ |
158
|
|
|
self::_stop(); |
159
|
|
|
|
160
|
|
|
$content = ob_get_clean(); |
161
|
|
|
|
162
|
|
|
if($content !== false) |
163
|
|
|
{ |
164
|
|
|
return $content; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
throw new OutputBuffering_Exception( |
168
|
|
|
'Cannot stop the output buffer.', |
169
|
|
|
'ob_get_contents returned false.', |
170
|
|
|
self::ERROR_CANNOT_GET_BUFFER |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|