1. sfResponse.class.php
  2. /** * sfResponse provides methods for manipulating client response information such * as headers, cookies and content. * * @package symfony * @subpackage response * @author Fabien Potencier * @version SVN: $Id: sfResponse.class.php 14598 2009-01-11 09:32:32Z dwhittle $ */
  3. abstract class sfResponse implements Serializable
  4. {
  5. protected
  6. $options = array(),
  7. $dispatcher = null,
  8. $content = '';
  9. /**
  10. * Class constructor.
  11. *
  12. * @see initialize()
  13. */
  14. public function __construct(sfEventDispatcher $dispatcher, $options = array())
  15. {
  16. $this->initialize($dispatcher, $options);
  17. }
  18. /**
  19. * Initializes this sfResponse.
  20. *
  21. * Available options:
  22. *
  23. * * logging: Whether to enable logging or not (false by default)
  24. *
  25. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
  26. * @param array $options An array of options
  27. *
  28. * @return bool true, if initialization completes successfully, otherwise false
  29. *
  30. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfResponse
  31. */
  32. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  33. {
  34. $this->dispatcher = $dispatcher;
  35. $this->options = $options;
  36. if (!isset($this->options['logging']))
  37. {
  38. $this->options['logging'] = false;
  39. }
  40. }
  41. /**
  42. * Sets the event dispatcher.
  43. *
  44. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
  45. */
  46. public function setEventDispatcher(sfEventDispatcher $dispatcher)
  47. {
  48. $this->dispatcher = $dispatcher;
  49. }
  50. /**
  51. * Sets the response content
  52. *
  53. * @param string $content
  54. */
  55. public function setContent($content)
  56. {
  57. $this->content = $content;
  58. }
  59. /**
  60. * Gets the current response content
  61. *
  62. * @return string Content
  63. */
  64. public function getContent()
  65. {
  66. return $this->content;
  67. }
  68. /**
  69. * Outputs the response content
  70. */
  71. public function sendContent()
  72. {
  73. $event = $this->dispatcher->filter(new sfEvent($this, 'response.filter_content'), $this->getContent());
  74. $content = $event->getReturnValue();
  75. if ($this->options['logging'])
  76. {
  77. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send content (%s o)', strlen($content)))));
  78. }
  79. echo $content;
  80. }
  81. /**
  82. * Sends the content.
  83. */
  84. public function send()
  85. {
  86. $this->sendContent();
  87. }
  88. /**
  89. * Returns the options.
  90. *
  91. * @return array The options.
  92. */
  93. public function getOptions()
  94. {
  95. return $this->options;
  96. }
  97. /**
  98. * Calls methods defined via sfEventDispatcher.
  99. *
  100. * @param string $method The method name
  101. * @param array $arguments The method arguments
  102. *
  103. * @return mixed The returned value of the called method
  104. *
  105. * @throws <b>sfException</b> If the calls fails
  106. */
  107. public function __call($method, $arguments)
  108. {
  109. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'response.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  110. if (!$event->isProcessed())
  111. {
  112. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  113. }
  114. return $event->getReturnValue();
  115. }
  116. /**
  117. * Serializes the current instance.
  118. *
  119. * @return array Objects instance
  120. */
  121. public function serialize()
  122. {
  123. return serialize($this->content);
  124. }
  125. /**
  126. * Unserializes a sfResponse instance.
  127. *
  128. * You need to inject a dispatcher after unserializing a sfResponse instance.
  129. *
  130. * @param string $serialized A serialized sfResponse instance
  131. *
  132. */
  133. public function unserialize($serialized)
  134. {
  135. $this->content = unserialize($serialized);
  136. }
  137. }

Debug toolbar