/Website/ext/fluorish/fBuffer.php

https://github.com/shalinipriya/combaticus · PHP · 269 lines · 134 code · 30 blank · 105 comment · 18 complexity · eda56081c6a9098fd1abf7251bc3ff2b MD5 · raw file

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