sfResponse.class.php 3.9 KB

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