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