| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
abstract class sfComponent |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$context = null, |
|---|
| 23 |
$request = null, |
|---|
| 24 |
$response = null, |
|---|
| 25 |
$varHolder = null, |
|---|
| 26 |
$requestParameterHolder = null; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
* Execute any application/business logic for this component. |
|---|
| 30 |
* |
|---|
| 31 |
* In a typical database-driven application, execute() handles application |
|---|
| 32 |
* logic itself and then proceeds to create a model instance. Once the model |
|---|
| 33 |
* instance is initialized it handles all business logic for the action. |
|---|
| 34 |
* |
|---|
| 35 |
* A model should represent an entity in your application. This could be a |
|---|
| 36 |
* user account, a shopping cart, or even a something as simple as a |
|---|
| 37 |
* single product. |
|---|
| 38 |
* |
|---|
| 39 |
* @return mixed A string containing the view name associated with this action |
|---|
| 40 |
*/ |
|---|
| 41 |
abstract function execute(); |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* Gets the module name associated with this component. |
|---|
| 45 |
* |
|---|
| 46 |
* @return string A module name |
|---|
| 47 |
*/ |
|---|
| 48 |
public function getModuleName() |
|---|
| 49 |
{ |
|---|
| 50 |
return $this->getContext()->getModuleName(); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* Gets the action name associated with this component. |
|---|
| 55 |
* |
|---|
| 56 |
* @return string An action name |
|---|
| 57 |
*/ |
|---|
| 58 |
public function getActionName() |
|---|
| 59 |
{ |
|---|
| 60 |
return $this->getContext()->getActionName(); |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* Initializes this component. |
|---|
| 65 |
* |
|---|
| 66 |
* @param sfContext The current application context |
|---|
| 67 |
* |
|---|
| 68 |
* @return boolean true, if initialization completes successfully, otherwise false |
|---|
| 69 |
*/ |
|---|
| 70 |
public function initialize($context) |
|---|
| 71 |
{ |
|---|
| 72 |
$this->context = $context; |
|---|
| 73 |
$this->varHolder = new sfParameterHolder(); |
|---|
| 74 |
$this->request = $context->getRequest(); |
|---|
| 75 |
$this->response = $context->getResponse(); |
|---|
| 76 |
$this->requestParameterHolder = $this->request->getParameterHolder(); |
|---|
| 77 |
|
|---|
| 78 |
return true; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
* Retrieves the current application context. |
|---|
| 83 |
* |
|---|
| 84 |
* @return sfContext The current sfContext instance |
|---|
| 85 |
*/ |
|---|
| 86 |
public final function getContext() |
|---|
| 87 |
{ |
|---|
| 88 |
return $this->context; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
* Retrieves the current logger instance. |
|---|
| 93 |
* |
|---|
| 94 |
* @return sfLogger The current sfLogger instance |
|---|
| 95 |
*/ |
|---|
| 96 |
public final function getLogger() |
|---|
| 97 |
{ |
|---|
| 98 |
return $this->context->getLogger(); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
* Logs a message using the sfLogger object. |
|---|
| 103 |
* |
|---|
| 104 |
* @param mixed String or object containing the message to log |
|---|
| 105 |
* @param string The priority of the message |
|---|
| 106 |
* (available priorities: emerg, alert, crit, err, warning, notice, info, debug) |
|---|
| 107 |
* |
|---|
| 108 |
* @see sfLogger |
|---|
| 109 |
*/ |
|---|
| 110 |
public function logMessage($message, $priority = 'info') |
|---|
| 111 |
{ |
|---|
| 112 |
if (sfConfig::get('sf_logging_enabled')) |
|---|
| 113 |
{ |
|---|
| 114 |
$this->context->getLogger()->log($message, constant('SF_LOG_'.strtoupper($priority))); |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
* Displays a message as a short message in the sfWebDebug toolbar. |
|---|
| 120 |
* |
|---|
| 121 |
* @param string The message text |
|---|
| 122 |
* |
|---|
| 123 |
* @see sfWebDebug |
|---|
| 124 |
*/ |
|---|
| 125 |
public function debugMessage($message) |
|---|
| 126 |
{ |
|---|
| 127 |
if (sfConfig::get('sf_web_debug')) |
|---|
| 128 |
{ |
|---|
| 129 |
sfWebDebug::getInstance()->logShortMessage($message); |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
* Returns the value of a request parameter. |
|---|
| 135 |
* |
|---|
| 136 |
* This is a proxy method equivalent to: |
|---|
| 137 |
* |
|---|
| 138 |
* <code>$this->getRequest()->getParameterHolder()->get($name)</code> |
|---|
| 139 |
* |
|---|
| 140 |
* @param string The parameter name |
|---|
| 141 |
* |
|---|
| 142 |
* @return string The request parameter value |
|---|
| 143 |
*/ |
|---|
| 144 |
public function getRequestParameter($name, $default = null) |
|---|
| 145 |
{ |
|---|
| 146 |
return $this->requestParameterHolder->get($name, $default); |
|---|
| 147 |
} |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
* Returns true if a request parameter exists. |
|---|
| 151 |
* |
|---|
| 152 |
* This is a proxy method equivalent to: |
|---|
| 153 |
* |
|---|
| 154 |
* <code>$this->getRequest()->getParameterHolder()->has($name)</code> |
|---|
| 155 |
* |
|---|
| 156 |
* @param string The parameter name |
|---|
| 157 |
* @return boolean true if the request parameter exists, false otherwise |
|---|
| 158 |
*/ |
|---|
| 159 |
public function hasRequestParameter($name) |
|---|
| 160 |
{ |
|---|
| 161 |
return $this->requestParameterHolder->has($name); |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
* Retrieves the current sfRequest object. |
|---|
| 166 |
* |
|---|
| 167 |
* This is a proxy method equivalent to: |
|---|
| 168 |
* |
|---|
| 169 |
* <code>$this->getContext()->getRequest()</code> |
|---|
| 170 |
* |
|---|
| 171 |
* @return sfRequest The current sfRequest implementation instance |
|---|
| 172 |
*/ |
|---|
| 173 |
public function getRequest() |
|---|
| 174 |
{ |
|---|
| 175 |
return $this->request; |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
* Retrieves the current sfResponse object. |
|---|
| 180 |
* |
|---|
| 181 |
* This is a proxy method equivalent to: |
|---|
| 182 |
* |
|---|
| 183 |
* <code>$this->getContext()->getResponse()</code> |
|---|
| 184 |
* |
|---|
| 185 |
* @return sfResponse The current sfResponse implementation instance |
|---|
| 186 |
*/ |
|---|
| 187 |
public function getResponse() |
|---|
| 188 |
{ |
|---|
| 189 |
return $this->response; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
* Retrieves the current sfController object. |
|---|
| 194 |
* |
|---|
| 195 |
* This is a proxy method equivalent to: |
|---|
| 196 |
* |
|---|
| 197 |
* <code>$this->getContext()->getController()</code> |
|---|
| 198 |
* |
|---|
| 199 |
* @return sfController The current sfController implementation instance |
|---|
| 200 |
*/ |
|---|
| 201 |
public function getController() |
|---|
| 202 |
{ |
|---|
| 203 |
return $this->getContext()->getController(); |
|---|
| 204 |
} |
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
* Retrieves the current sfUser object. |
|---|
| 208 |
* |
|---|
| 209 |
* This is a proxy method equivalent to: |
|---|
| 210 |
* |
|---|
| 211 |
* <code>$this->getContext()->getUser()</code> |
|---|
| 212 |
* |
|---|
| 213 |
* @return sfUser The current sfUser implementation instance |
|---|
| 214 |
*/ |
|---|
| 215 |
public function getUser() |
|---|
| 216 |
{ |
|---|
| 217 |
return $this->getContext()->getUser(); |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
* Sets a variable for the template. |
|---|
| 222 |
* |
|---|
| 223 |
* @param string The variable name |
|---|
| 224 |
* @param mixed The variable value |
|---|
| 225 |
*/ |
|---|
| 226 |
public function setVar($name, $value) |
|---|
| 227 |
{ |
|---|
| 228 |
$this->varHolder->set($name, $value); |
|---|
| 229 |
} |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
* Gets a variable set for the template. |
|---|
| 233 |
* |
|---|
| 234 |
* @param string The variable name |
|---|
| 235 |
* @return mixed The variable value |
|---|
| 236 |
*/ |
|---|
| 237 |
public function getVar($name) |
|---|
| 238 |
{ |
|---|
| 239 |
return $this->varHolder->get($name); |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
* Gets the sfParameterHolder object that stores the template variables. |
|---|
| 244 |
* |
|---|
| 245 |
* @return sfParameterHolder The variable holder. |
|---|
| 246 |
*/ |
|---|
| 247 |
public function getVarHolder() |
|---|
| 248 |
{ |
|---|
| 249 |
return $this->varHolder; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
* Sets a variable for the template. |
|---|
| 254 |
* |
|---|
| 255 |
* This is a shortcut for: |
|---|
| 256 |
* |
|---|
| 257 |
* <code>$this->setVar('name', 'value')</code> |
|---|
| 258 |
* |
|---|
| 259 |
* @param string The variable name |
|---|
| 260 |
* @param string The variable value |
|---|
| 261 |
* |
|---|
| 262 |
* @return boolean always true |
|---|
| 263 |
* |
|---|
| 264 |
* @see setVar() |
|---|
| 265 |
*/ |
|---|
| 266 |
public function __set($key, $value) |
|---|
| 267 |
{ |
|---|
| 268 |
return $this->varHolder->setByRef($key, $value); |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
* Gets a variable for the template. |
|---|
| 273 |
* |
|---|
| 274 |
* This is a shortcut for: |
|---|
| 275 |
* |
|---|
| 276 |
* <code>$this->getVar('name')</code> |
|---|
| 277 |
* |
|---|
| 278 |
* @param string The variable name |
|---|
| 279 |
* |
|---|
| 280 |
* @return mixed The variable value |
|---|
| 281 |
* |
|---|
| 282 |
* @see getVar() |
|---|
| 283 |
*/ |
|---|
| 284 |
public function & __get($key) |
|---|
| 285 |
{ |
|---|
| 286 |
return $this->varHolder->get($key); |
|---|
| 287 |
} |
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
* Returns true if a variable for the template is set. |
|---|
| 291 |
* |
|---|
| 292 |
* This is a shortcut for: |
|---|
| 293 |
* |
|---|
| 294 |
* <code>$this->getVarHolder()->has('name')</code> |
|---|
| 295 |
* |
|---|
| 296 |
* @param string The variable name |
|---|
| 297 |
* |
|---|
| 298 |
* @return boolean true if the variable is set |
|---|
| 299 |
*/ |
|---|
| 300 |
public function __isset($name) |
|---|
| 301 |
{ |
|---|
| 302 |
return $this->varHolder->has($name); |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
* Removes a variable for the template. |
|---|
| 307 |
* |
|---|
| 308 |
* This is just really a shortcut for: |
|---|
| 309 |
* |
|---|
| 310 |
* <code>$this->getVarHolder()->remove('name')</code> |
|---|
| 311 |
* |
|---|
| 312 |
* @param string The variable Name |
|---|
| 313 |
*/ |
|---|
| 314 |
public function __unset($name) |
|---|
| 315 |
{ |
|---|
| 316 |
$this->varHolder->remove($name); |
|---|
| 317 |
} |
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
* Sets a flash variable that will be passed to the very next action. |
|---|
| 321 |
* |
|---|
| 322 |
* @param string The name of the flash variable |
|---|
| 323 |
* @param string The value of the flash variable |
|---|
| 324 |
* @param boolean true if the flash have to persist for the following request (true by default) |
|---|
| 325 |
*/ |
|---|
| 326 |
public function setFlash($name, $value, $persist = true) |
|---|
| 327 |
{ |
|---|
| 328 |
$this->getUser()->setAttribute($name, $value, 'symfony/flash'); |
|---|
| 329 |
|
|---|
| 330 |
if ($persist) |
|---|
| 331 |
{ |
|---|
| 332 |
|
|---|
| 333 |
$this->getUser()->getAttributeHolder()->remove($name, 'symfony/flash/remove'); |
|---|
| 334 |
} |
|---|
| 335 |
else |
|---|
| 336 |
{ |
|---|
| 337 |
$this->getUser()->setAttribute($name, true, 'symfony/flash/remove'); |
|---|
| 338 |
} |
|---|
| 339 |
} |
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
* Gets a flash variable. |
|---|
| 343 |
* |
|---|
| 344 |
* @param string The name of the flash variable |
|---|
| 345 |
* |
|---|
| 346 |
* @return mixed The value of the flash variable |
|---|
| 347 |
*/ |
|---|
| 348 |
public function getFlash($name) |
|---|
| 349 |
{ |
|---|
| 350 |
return $this->getUser()->getAttribute($name, null, 'symfony/flash'); |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
* Returns true if a flash variable of the specified name exists. |
|---|
| 355 |
* |
|---|
| 356 |
* @param string The name of the flash variable |
|---|
| 357 |
* |
|---|
| 358 |
* @return boolean true if the variable exists, false otherwise |
|---|
| 359 |
*/ |
|---|
| 360 |
public function hasFlash($name) |
|---|
| 361 |
{ |
|---|
| 362 |
return $this->getUser()->hasAttribute($name, 'symfony/flash'); |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
* Sends and email from the current action. |
|---|
| 367 |
* |
|---|
| 368 |
* This methods calls a module/action with the sfMailView class. |
|---|
| 369 |
* |
|---|
| 370 |
* This is a shortcut for |
|---|
| 371 |
* |
|---|
| 372 |
* <code>$this->getController()->sendEmail($module, $action)</code> |
|---|
| 373 |
* |
|---|
| 374 |
* @param string A module name |
|---|
| 375 |
* @param string An action name |
|---|
| 376 |
* |
|---|
| 377 |
* @return string The generated mail content |
|---|
| 378 |
* |
|---|
| 379 |
* @see sfMailView, getPresentationFor(), sfController |
|---|
| 380 |
*/ |
|---|
| 381 |
public function sendEmail($module, $action) |
|---|
| 382 |
{ |
|---|
| 383 |
return $this->getController()->getPresentationFor($module, $action, 'sfMail'); |
|---|
| 384 |
} |
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
* Returns the rendered view presentation of a given module/action. |
|---|
| 388 |
* |
|---|
| 389 |
* This is a shortcut for |
|---|
| 390 |
* |
|---|
| 391 |
* <code>$this->getController()->getPresentationFor($module, $action, $viewName)</code> |
|---|
| 392 |
* |
|---|
| 393 |
* @param string A module name |
|---|
| 394 |
* @param string An action name |
|---|
| 395 |
* @param string A View class name |
|---|
| 396 |
* |
|---|
| 397 |
* @return string The generated content |
|---|
| 398 |
* |
|---|
| 399 |
* @see sfController |
|---|
| 400 |
*/ |
|---|
| 401 |
public function getPresentationFor($module, $action, $viewName = null) |
|---|
| 402 |
{ |
|---|
| 403 |
return $this->getController()->getPresentationFor($module, $action, $viewName); |
|---|
| 404 |
} |
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
* Calls methods defined via the sfMixer class. |
|---|
| 408 |
* |
|---|
| 409 |
* @param string The method name |
|---|
| 410 |
* @param array The method arguments |
|---|
| 411 |
* |
|---|
| 412 |
* @return mixed The returned value of the called method |
|---|
| 413 |
* |
|---|
| 414 |
* @see sfMixer |
|---|
| 415 |
*/ |
|---|
| 416 |
public function __call($method, $arguments) |
|---|
| 417 |
{ |
|---|
| 418 |
if (!$callable = sfMixer::getCallable('sfComponent:'.$method)) |
|---|
| 419 |
{ |
|---|
| 420 |
throw new sfException(sprintf('Call to undefined method sfComponent::%s', $method)); |
|---|
| 421 |
} |
|---|
| 422 |
|
|---|
| 423 |
array_unshift($arguments, $this); |
|---|
| 424 |
|
|---|
| 425 |
return call_user_func_array($callable, $arguments); |
|---|
| 426 |
} |
|---|
| 427 |
} |
|---|
| 428 |
|
|---|