COutputProcessor.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * COutputProcessor class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * COutputProcessor transforms the content into a different format.
  12. *
  13. * COutputProcessor captures the output generated by an action or a view fragment
  14. * and passes it to its {@link onProcessOutput} event handlers for further processing.
  15. *
  16. * The event handler may process the output and store it back to the {@link COutputEvent::output}
  17. * property. By setting the {@link CEvent::handled handled} property of the event parameter
  18. * to true, the output will not be echoed anymore. Otherwise (by default), the output will be echoed.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @package system.web.widgets
  22. * @since 1.0
  23. */
  24. class COutputProcessor extends CFilterWidget
  25. {
  26. /**
  27. * Initializes the widget.
  28. * This method starts the output buffering.
  29. */
  30. public function init()
  31. {
  32. ob_start();
  33. ob_implicit_flush(false);
  34. }
  35. /**
  36. * Executes the widget.
  37. * This method stops output buffering and processes the captured output.
  38. */
  39. public function run()
  40. {
  41. $output=ob_get_clean();
  42. $this->processOutput($output);
  43. }
  44. /**
  45. * Processes the captured output.
  46. *
  47. * The default implementation raises an {@link onProcessOutput} event.
  48. * If the event is not handled by any event handler, the output will be echoed.
  49. *
  50. * @param string $output the captured output to be processed
  51. */
  52. public function processOutput($output)
  53. {
  54. if($this->hasEventHandler('onProcessOutput'))
  55. {
  56. $event=new COutputEvent($this,$output);
  57. $this->onProcessOutput($event);
  58. if(!$event->handled)
  59. echo $output;
  60. }
  61. else
  62. echo $output;
  63. }
  64. /**
  65. * Raised when the output has been captured.
  66. * @param COutputEvent $event event parameter
  67. */
  68. public function onProcessOutput($event)
  69. {
  70. $this->raiseEvent('onProcessOutput',$event);
  71. }
  72. }