forked from flourishlib/flourish-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fBuffer.php
269 lines (239 loc) · 6.83 KB
/
fBuffer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* Provides a single, simplified interface for [http://php.net/outcontrol output buffering] to prevent nested buffering issues and provide a more logical API
*
* @copyright Copyright (c) 2008-2010 Will Bond
* @author Will Bond [wb] <[email protected]>
* @license http://flourishlib.com/license
*
* @package Flourish
* @link http://flourishlib.com/fBuffer
*
* @version 1.0.0b3
* @changes 1.0.0b3 Added a check to ensure the zlib extension is installd when doing gzipped buffering [wb, 2010-05-20]
* @changes 1.0.0b2 Added the `$gzip` parameter to ::start() [wb, 2010-05-19]
* @changes 1.0.0b The initial implementation [wb, 2008-03-16]
*/
class fBuffer
{
// The following constants allow for nice looking callbacks to static methods
const erase = 'fBuffer::erase';
const get = 'fBuffer::get';
const isStarted = 'fBuffer::isStarted';
const replace = 'fBuffer::replace';
const reset = 'fBuffer::reset';
const start = 'fBuffer::start';
const startCapture = 'fBuffer::startCapture';
const stop = 'fBuffer::stop';
const stopCapture = 'fBuffer::stopCapture';
/**
* If output capturing is currently active
*
* @var boolean
*/
static private $capturing = FALSE;
/**
* If output buffering has been started
*
* @var integer
*/
static private $started = FALSE;
/**
* Erases the output buffer
*
* @return void
*/
static public function erase()
{
if (!self::$started) {
throw new fProgrammerException(
'The output buffer can not be erased since output buffering has not been started'
);
}
if (self::$capturing) {
throw new fProgrammerException(
'Output capturing is currently active and it must be stopped before the buffer can be erased'
);
}
ob_clean();
}
/**
* Returns the contents of output buffer
*
* @return string The contents of the output buffer
*/
static public function get()
{
if (!self::$started) {
throw new fProgrammerException(
'The output buffer can not be retrieved because it has not been started'
);
}
if (self::$capturing) {
throw new fProgrammerException(
'Output capturing is currently active and it must be stopped before the buffer can be retrieved'
);
}
return ob_get_contents();
}
/**
* Checks if buffering has been started
*
* @return boolean If buffering has been started
*/
static public function isStarted()
{
return self::$started;
}
/**
* Replaces a value in the output buffer
*
* @param string $find The string to find
* @param string $replace The string to replace
* @return void
*/
static public function replace($find, $replace)
{
if (!self::$started) {
throw new fProgrammerException(
'A replacement can not be made since output buffering has not been started'
);
}
if (self::$capturing) {
throw new fProgrammerException(
'Output capturing is currently active and it must be stopped before a replacement can be made'
);
}
// ob_get_clean() actually turns off output buffering, so we do it the long way
$contents = ob_get_contents();
ob_clean();
echo str_replace($find, $replace, $contents);
}
/**
* Resets the configuration and buffer of the class
*
* @internal
*
* @return void
*/
static public function reset()
{
if (self::$capturing) {
self::stopCapture();
}
if (self::$started) {
self::erase();
self::stop();
}
}
/**
* Starts output buffering
*
* @param boolean $gzip If the buffered output should be gzipped using [http://php.net/ob_gzhandler `ob_gzhandler()`]
* @return void
*/
static public function start($gzip=FALSE)
{
if (self::$started) {
throw new fProgrammerException(
'Output buffering has already been started'
);
}
if (self::$capturing) {
throw new fProgrammerException(
'Output capturing is currently active and it must be stopped before the buffering can be started'
);
}
if ($gzip && !extension_loaded('zlib')) {
throw new fEnvironmentException(
'The PHP %s extension is required for gzipped buffering, however is does not appear to be loaded',
'zlib'
);
}
ob_start($gzip ? 'ob_gzhandler' : NULL);
self::$started = TRUE;
}
/**
* Starts capturing output, should be used with ::stopCapture() to grab output from code that does not offer an option of returning a value instead of outputting it
*
* @return void
*/
static public function startCapture()
{
if (self::$capturing) {
throw new fProgrammerException(
'Output capturing has already been started'
);
}
ob_start();
self::$capturing = TRUE;
}
/**
* Stops output buffering, flushing everything to the browser
*
* @return void
*/
static public function stop()
{
if (!self::$started) {
throw new fProgrammerException(
'Output buffering can not be stopped since it has not been started'
);
}
if (self::$capturing) {
throw new fProgrammerException(
'Output capturing is currently active and it must be stopped before buffering can be stopped'
);
}
// Only flush if there is content to push out, otherwise
// we might prevent headers from being sent
if (ob_get_contents()) {
ob_end_flush();
} else {
ob_end_clean();
}
self::$started = FALSE;
}
/**
* Stops capturing output, returning what was captured
*
* @return string The captured output
*/
static public function stopCapture()
{
if (!self::$capturing) {
throw new fProgrammerException(
'Output capturing can not be stopped since it has not been started'
);
}
self::$capturing = FALSE;
return ob_get_clean();
}
/**
* Forces use as a static class
*
* @return fBuffer
*/
private function __construct() { }
}
/**
* Copyright (c) 2008-2010 Will Bond <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/