PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Widget/Flush.php

https://github.com/putersham/widget
PHP | 73 lines | 27 code | 9 blank | 37 comment | 5 complexity | 61a4e233be670a3b654f12618ffb4401 MD5 | raw file
  1. <?php
  2. /**
  3. * Widget Framework
  4. *
  5. * @copyright Copyright (c) 2008-2013 Twin Huang
  6. * @license http://opensource.org/licenses/mit-license.php MIT License
  7. */
  8. namespace Widget;
  9. /**
  10. * A widget that flushes the content to browser immediately
  11. *
  12. * @author Twin Huang <twinhuang@qq.com>
  13. * @link http://php.net/manual/en/function.flush.php
  14. */
  15. class Flush extends Response
  16. {
  17. /**
  18. * Disable compress and implicit flush
  19. */
  20. public function prepare()
  21. {
  22. if (function_exists('apache_setenv')) {
  23. apache_setenv('no-gzip', '1');
  24. }
  25. /**
  26. * Disable zlib to compress output
  27. *
  28. * @link http://www.php.net/manual/en/zlib.configuration.php
  29. */
  30. if (extension_loaded('zlib') && !headers_sent()) {
  31. ini_set('zlib.output_compression', '0');
  32. }
  33. /**
  34. * Turn implicit flush on
  35. *
  36. * @link http://www.php.net/manual/en/function.ob-implicit-flush.php
  37. */
  38. ob_implicit_flush();
  39. }
  40. /**
  41. * Send response content
  42. *
  43. * @param string $content
  44. * @param int $status
  45. * @return Flush
  46. */
  47. public function send($content = null, $status = null)
  48. {
  49. $this->prepare();
  50. parent::send($content, $status);
  51. /**
  52. * Send blank characters for output_buffering
  53. *
  54. * @link http://www.php.net/manual/en/outcontrol.configuration.php
  55. */
  56. if ($length = ini_get('output_buffering')) {
  57. echo str_pad('', $length);
  58. }
  59. while (ob_get_level()) {
  60. ob_end_flush();
  61. }
  62. return $this;
  63. }
  64. }