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