| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
http://php.net/outcontrol |
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fBuffer |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
|
|---|
| 17 |
class fBuffer |
|---|
| 18 |
{ |
|---|
| 19 |
|
|---|
| 20 |
const erase = 'fBuffer::erase'; |
|---|
| 21 |
const get = 'fBuffer::get'; |
|---|
| 22 |
const isStarted = 'fBuffer::isStarted'; |
|---|
| 23 |
const replace = 'fBuffer::replace'; |
|---|
| 24 |
const reset = 'fBuffer::reset'; |
|---|
| 25 |
const start = 'fBuffer::start'; |
|---|
| 26 |
const startCapture = 'fBuffer::startCapture'; |
|---|
| 27 |
const stop = 'fBuffer::stop'; |
|---|
| 28 |
const stopCapture = 'fBuffer::stopCapture'; |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
@var |
|---|
| 35 |
|
|---|
| 36 |
static private $capturing = FALSE; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
@var |
|---|
| 42 |
|
|---|
| 43 |
static private $started = FALSE; |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
@return |
|---|
| 50 |
|
|---|
| 51 |
static public function erase() |
|---|
| 52 |
{ |
|---|
| 53 |
if (!self::$started) { |
|---|
| 54 |
throw new fProgrammerException( |
|---|
| 55 |
'The output buffer can not be erased since output buffering has not been started' |
|---|
| 56 |
); |
|---|
| 57 |
} |
|---|
| 58 |
if (self::$capturing) { |
|---|
| 59 |
throw new fProgrammerException( |
|---|
| 60 |
'Output capturing is currently active and it must be stopped before the buffer can be erased' |
|---|
| 61 |
); |
|---|
| 62 |
} |
|---|
| 63 |
ob_clean(); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
@return |
|---|
| 71 |
|
|---|
| 72 |
static public function get() |
|---|
| 73 |
{ |
|---|
| 74 |
if (!self::$started) { |
|---|
| 75 |
throw new fProgrammerException( |
|---|
| 76 |
'The output buffer can not be retrieved because it has not been started' |
|---|
| 77 |
); |
|---|
| 78 |
} |
|---|
| 79 |
if (self::$capturing) { |
|---|
| 80 |
throw new fProgrammerException( |
|---|
| 81 |
'Output capturing is currently active and it must be stopped before the buffer can be retrieved' |
|---|
| 82 |
); |
|---|
| 83 |
} |
|---|
| 84 |
return ob_get_contents(); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
@return |
|---|
| 92 |
|
|---|
| 93 |
static public function isStarted() |
|---|
| 94 |
{ |
|---|
| 95 |
return self::$started; |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
@param |
|---|
| 103 |
@param |
|---|
| 104 |
@return |
|---|
| 105 |
|
|---|
| 106 |
static public function replace($find, $replace) |
|---|
| 107 |
{ |
|---|
| 108 |
if (!self::$started) { |
|---|
| 109 |
throw new fProgrammerException( |
|---|
| 110 |
'A replacement can not be made since output buffering has not been started' |
|---|
| 111 |
); |
|---|
| 112 |
} |
|---|
| 113 |
if (self::$capturing) { |
|---|
| 114 |
throw new fProgrammerException( |
|---|
| 115 |
'Output capturing is currently active and it must be stopped before a replacement can be made' |
|---|
| 116 |
); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
$contents = ob_get_contents(); |
|---|
| 121 |
ob_clean(); |
|---|
| 122 |
|
|---|
| 123 |
echo str_replace($find, $replace, $contents); |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
@internal |
|---|
| 131 |
|
|---|
| 132 |
@return |
|---|
| 133 |
|
|---|
| 134 |
static public function reset() |
|---|
| 135 |
{ |
|---|
| 136 |
if (self::$capturing) { |
|---|
| 137 |
self::stopCapture(); |
|---|
| 138 |
} |
|---|
| 139 |
if (self::$started) { |
|---|
| 140 |
self::erase(); |
|---|
| 141 |
self::stop(); |
|---|
| 142 |
} |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
@param http://php.net/ob_gzhandler |
|---|
| 150 |
@return |
|---|
| 151 |
|
|---|
| 152 |
static public function start($gzip=FALSE) |
|---|
| 153 |
{ |
|---|
| 154 |
if (self::$started) { |
|---|
| 155 |
throw new fProgrammerException( |
|---|
| 156 |
'Output buffering has already been started' |
|---|
| 157 |
); |
|---|
| 158 |
} |
|---|
| 159 |
if (self::$capturing) { |
|---|
| 160 |
throw new fProgrammerException( |
|---|
| 161 |
'Output capturing is currently active and it must be stopped before the buffering can be started' |
|---|
| 162 |
); |
|---|
| 163 |
} |
|---|
| 164 |
if ($gzip && !extension_loaded('zlib')) { |
|---|
| 165 |
throw new fEnvironmentException( |
|---|
| 166 |
'The PHP %s extension is required for gzipped buffering, however is does not appear to be loaded', |
|---|
| 167 |
'zlib' |
|---|
| 168 |
); |
|---|
| 169 |
} |
|---|
| 170 |
ob_start($gzip ? 'ob_gzhandler' : NULL); |
|---|
| 171 |
self::$started = TRUE; |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
@return |
|---|
| 179 |
|
|---|
| 180 |
static public function startCapture() |
|---|
| 181 |
{ |
|---|
| 182 |
if (self::$capturing) { |
|---|
| 183 |
throw new fProgrammerException( |
|---|
| 184 |
'Output capturing has already been started' |
|---|
| 185 |
); |
|---|
| 186 |
} |
|---|
| 187 |
ob_start(); |
|---|
| 188 |
self::$capturing = TRUE; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
@return |
|---|
| 196 |
|
|---|
| 197 |
static public function stop() |
|---|
| 198 |
{ |
|---|
| 199 |
if (!self::$started) { |
|---|
| 200 |
throw new fProgrammerException( |
|---|
| 201 |
'Output buffering can not be stopped since it has not been started' |
|---|
| 202 |
); |
|---|
| 203 |
} |
|---|
| 204 |
if (self::$capturing) { |
|---|
| 205 |
throw new fProgrammerException( |
|---|
| 206 |
'Output capturing is currently active and it must be stopped before buffering can be stopped' |
|---|
| 207 |
); |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
if (ob_get_contents()) { |
|---|
| 213 |
ob_end_flush(); |
|---|
| 214 |
} else { |
|---|
| 215 |
ob_end_clean(); |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
self::$started = FALSE; |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
@return |
|---|
| 226 |
|
|---|
| 227 |
static public function stopCapture() |
|---|
| 228 |
{ |
|---|
| 229 |
if (!self::$capturing) { |
|---|
| 230 |
throw new fProgrammerException( |
|---|
| 231 |
'Output capturing can not be stopped since it has not been started' |
|---|
| 232 |
); |
|---|
| 233 |
} |
|---|
| 234 |
self::$capturing = FALSE; |
|---|
| 235 |
return ob_get_clean(); |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
@return |
|---|
| 243 |
|
|---|
| 244 |
private function __construct() { } |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
will@flourishlib.com |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|