| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
abstract class sfRequest |
|---|
| 24 |
{ |
|---|
| 25 |
const GET = 'GET'; |
|---|
| 26 |
const POST = 'POST'; |
|---|
| 27 |
const PUT = 'PUT'; |
|---|
| 28 |
const DELETE = 'DELETE'; |
|---|
| 29 |
const HEAD = 'HEAD'; |
|---|
| 30 |
|
|---|
| 31 |
protected |
|---|
| 32 |
$dispatcher = null, |
|---|
| 33 |
$method = null, |
|---|
| 34 |
$options = array(), |
|---|
| 35 |
$parameterHolder = null, |
|---|
| 36 |
$attributeHolder = null; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
* Class constructor. |
|---|
| 40 |
* |
|---|
| 41 |
* @see initialize() |
|---|
| 42 |
*/ |
|---|
| 43 |
public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array()) |
|---|
| 44 |
{ |
|---|
| 45 |
$this->initialize($dispatcher, $parameters, $attributes, $options); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* Initializes this sfRequest. |
|---|
| 50 |
* |
|---|
| 51 |
* Available options: |
|---|
| 52 |
* |
|---|
| 53 |
* * logging: Whether to enable logging or not (false by default) |
|---|
| 54 |
* |
|---|
| 55 |
* @param sfEventDispatcher $dispatcher An sfEventDispatcher instance |
|---|
| 56 |
* @param array $parameters An associative array of initialization parameters |
|---|
| 57 |
* @param array $attributes An associative array of initialization attributes |
|---|
| 58 |
* @param array $options An associative array of options |
|---|
| 59 |
* |
|---|
| 60 |
* @return bool true, if initialization completes successfully, otherwise false |
|---|
| 61 |
* |
|---|
| 62 |
* @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest |
|---|
| 63 |
*/ |
|---|
| 64 |
public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array()) |
|---|
| 65 |
{ |
|---|
| 66 |
$this->dispatcher = $dispatcher; |
|---|
| 67 |
|
|---|
| 68 |
$this->options = $options; |
|---|
| 69 |
|
|---|
| 70 |
if (!isset($this->options['logging'])) |
|---|
| 71 |
{ |
|---|
| 72 |
$this->options['logging'] = false; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
$this->parameterHolder = new sfParameterHolder(); |
|---|
| 77 |
$this->attributeHolder = new sfParameterHolder(); |
|---|
| 78 |
|
|---|
| 79 |
$this->parameterHolder->add($parameters); |
|---|
| 80 |
$this->attributeHolder->add($attributes); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
* Extracts parameter values from the request. |
|---|
| 85 |
* |
|---|
| 86 |
* @param array $names An indexed array of parameter names to extract |
|---|
| 87 |
* |
|---|
| 88 |
* @return array An associative array of parameters and their values. If |
|---|
| 89 |
* a specified parameter doesn't exist an empty string will |
|---|
| 90 |
* be returned for its value |
|---|
| 91 |
*/ |
|---|
| 92 |
public function extractParameters($names) |
|---|
| 93 |
{ |
|---|
| 94 |
$array = array(); |
|---|
| 95 |
|
|---|
| 96 |
$parameters = $this->parameterHolder->getAll(); |
|---|
| 97 |
foreach ($parameters as $key => $value) |
|---|
| 98 |
{ |
|---|
| 99 |
if (in_array($key, $names)) |
|---|
| 100 |
{ |
|---|
| 101 |
$array[$key] = $value; |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
return $array; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
* Gets the request method. |
|---|
| 110 |
* |
|---|
| 111 |
* @return string The request method |
|---|
| 112 |
*/ |
|---|
| 113 |
public function getMethod() |
|---|
| 114 |
{ |
|---|
| 115 |
return $this->method; |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
* Sets the request method. |
|---|
| 120 |
* |
|---|
| 121 |
* @param string $method The request method |
|---|
| 122 |
* |
|---|
| 123 |
* @throws <b>sfException</b> - If the specified request method is invalid |
|---|
| 124 |
*/ |
|---|
| 125 |
public function setMethod($method) |
|---|
| 126 |
{ |
|---|
| 127 |
if (!in_array(strtoupper($method), array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD))) |
|---|
| 128 |
{ |
|---|
| 129 |
throw new sfException(sprintf('Invalid request method: %s.', $method)); |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
$this->method = strtoupper($method); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
* Retrieves the parameters for the current request. |
|---|
| 137 |
* |
|---|
| 138 |
* @return sfParameterHolder The parameter holder |
|---|
| 139 |
*/ |
|---|
| 140 |
public function getParameterHolder() |
|---|
| 141 |
{ |
|---|
| 142 |
return $this->parameterHolder; |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
* Retrieves the attributes holder. |
|---|
| 147 |
* |
|---|
| 148 |
* @return sfParameterHolder The attribute holder |
|---|
| 149 |
*/ |
|---|
| 150 |
public function getAttributeHolder() |
|---|
| 151 |
{ |
|---|
| 152 |
return $this->attributeHolder; |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
* Retrieves an attribute from the current request. |
|---|
| 157 |
* |
|---|
| 158 |
* @param string $name Attribute name |
|---|
| 159 |
* @param string $default Default attribute value |
|---|
| 160 |
* |
|---|
| 161 |
* @return mixed An attribute value |
|---|
| 162 |
*/ |
|---|
| 163 |
public function getAttribute($name, $default = null) |
|---|
| 164 |
{ |
|---|
| 165 |
return $this->attributeHolder->get($name, $default); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
* Indicates whether or not an attribute exist for the current request. |
|---|
| 170 |
* |
|---|
| 171 |
* @param string $name Attribute name |
|---|
| 172 |
* |
|---|
| 173 |
* @return bool true, if the attribute exists otherwise false |
|---|
| 174 |
*/ |
|---|
| 175 |
public function hasAttribute($name) |
|---|
| 176 |
{ |
|---|
| 177 |
return $this->attributeHolder->has($name); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
* Sets an attribute for the request. |
|---|
| 182 |
* |
|---|
| 183 |
* @param string $name Attribute name |
|---|
| 184 |
* @param string $value Value for the attribute |
|---|
| 185 |
* |
|---|
| 186 |
*/ |
|---|
| 187 |
public function setAttribute($name, $value) |
|---|
| 188 |
{ |
|---|
| 189 |
$this->attributeHolder->set($name, $value); |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
* Retrieves a paramater for the current request. |
|---|
| 194 |
* |
|---|
| 195 |
* @param string $name Parameter name |
|---|
| 196 |
* @param string $default Parameter default value |
|---|
| 197 |
* |
|---|
| 198 |
*/ |
|---|
| 199 |
public function getParameter($name, $default = null) |
|---|
| 200 |
{ |
|---|
| 201 |
return $this->parameterHolder->get($name, $default); |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
* Indicates whether or not a parameter exist for the current request. |
|---|
| 206 |
* |
|---|
| 207 |
* @param string $name Parameter name |
|---|
| 208 |
* |
|---|
| 209 |
* @return bool true, if the paramater exists otherwise false |
|---|
| 210 |
*/ |
|---|
| 211 |
public function hasParameter($name) |
|---|
| 212 |
{ |
|---|
| 213 |
return $this->parameterHolder->has($name); |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
* Sets a parameter for the current request. |
|---|
| 218 |
* |
|---|
| 219 |
* @param string $name Parameter name |
|---|
| 220 |
* @param string $value Parameter value |
|---|
| 221 |
* |
|---|
| 222 |
*/ |
|---|
| 223 |
public function setParameter($name, $value) |
|---|
| 224 |
{ |
|---|
| 225 |
$this->parameterHolder->set($name, $value); |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
* Calls methods defined via sfEventDispatcher. |
|---|
| 230 |
* |
|---|
| 231 |
* @param string $method The method name |
|---|
| 232 |
* @param array $arguments The method arguments |
|---|
| 233 |
* |
|---|
| 234 |
* @return mixed The returned value of the called method |
|---|
| 235 |
* |
|---|
| 236 |
* @throws <b>sfException</b> if call fails |
|---|
| 237 |
*/ |
|---|
| 238 |
public function __call($method, $arguments) |
|---|
| 239 |
{ |
|---|
| 240 |
$event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments))); |
|---|
| 241 |
if (!$event->isProcessed()) |
|---|
| 242 |
{ |
|---|
| 243 |
throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method)); |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
return $event->getReturnValue(); |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
public function __clone() |
|---|
| 250 |
{ |
|---|
| 251 |
$this->parameterHolder = clone $this->parameterHolder; |
|---|
| 252 |
$this->attributeHolder = clone $this->attributeHolder; |
|---|
| 253 |
} |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|