1. sfComponent.class.php
  2. /** * sfComponent. * * @package symfony * @subpackage action * @author Fabien Potencier * @version SVN: $Id: sfComponent.class.php 23922 2009-11-14 14:58:38Z fabien $ */
  3. abstract class sfComponent
  4. {
  5. protected
  6. $moduleName = '',
  7. $actionName = '',
  8. $context = null,
  9. $dispatcher = null,
  10. $request = null,
  11. $response = null,
  12. $varHolder = null,
  13. $requestParameterHolder = null;
  14. /**
  15. * Class constructor.
  16. *
  17. * @see initialize()
  18. */
  19. public function __construct($context, $moduleName, $actionName)
  20. {
  21. $this->initialize($context, $moduleName, $actionName);
  22. }
  23. /**
  24. * Initializes this component.
  25. *
  26. * @param sfContext $context The current application context.
  27. * @param string $moduleName The module name.
  28. * @param string $actionName The action name.
  29. *
  30. * @return boolean true, if initialization completes successfully, otherwise false
  31. */
  32. public function initialize($context, $moduleName, $actionName)
  33. {
  34. $this->moduleName = $moduleName;
  35. $this->actionName = $actionName;
  36. $this->context = $context;
  37. $this->dispatcher = $context->getEventDispatcher();
  38. $this->varHolder = new sfParameterHolder();
  39. $this->request = $context->getRequest();
  40. $this->response = $context->getResponse();
  41. $this->requestParameterHolder = $this->request->getParameterHolder();
  42. }
  43. /**
  44. * Execute any application/business logic for this component.
  45. *
  46. * In a typical database-driven application, execute() handles application
  47. * logic itself and then proceeds to create a model instance. Once the model
  48. * instance is initialized it handles all business logic for the action.
  49. *
  50. * A model should represent an entity in your application. This could be a
  51. * user account, a shopping cart, or even a something as simple as a
  52. * single product.
  53. *
  54. * @param sfRequest $request The current sfRequest object
  55. *
  56. * @return mixed A string containing the view name associated with this action
  57. */
  58. abstract function execute($request);
  59. /**
  60. * Gets the module name associated with this component.
  61. *
  62. * @return string A module name
  63. */
  64. public function getModuleName()
  65. {
  66. return $this->moduleName;
  67. }
  68. /**
  69. * Gets the action name associated with this component.
  70. *
  71. * @return string An action name
  72. */
  73. public function getActionName()
  74. {
  75. return $this->actionName;
  76. }
  77. /**
  78. * Retrieves the current application context.
  79. *
  80. * @return sfContext The current sfContext instance
  81. */
  82. public final function getContext()
  83. {
  84. return $this->context;
  85. }
  86. /**
  87. * Retrieves the current logger instance.
  88. *
  89. * @return sfLogger The current sfLogger instance
  90. */
  91. public final function getLogger()
  92. {
  93. return $this->context->getLogger();
  94. }
  95. /**
  96. * Logs a message using the sfLogger object.
  97. *
  98. * @param mixed $message String or object containing the message to log
  99. * @param string $priority The priority of the message
  100. * (available priorities: emerg, alert, crit, err,
  101. * warning, notice, info, debug)
  102. *
  103. * @see sfLogger
  104. */
  105. public function logMessage($message, $priority = 'info')
  106. {
  107. if (sfConfig::get('sf_logging_enabled'))
  108. {
  109. $this->dispatcher->notify(new sfEvent($this, 'application.log', array($message, 'priority' => constant('sfLogger::'.strtoupper($priority)))));
  110. }
  111. }
  112. /**
  113. * Returns the value of a request parameter.
  114. *
  115. * This is a proxy method equivalent to:
  116. *
  117. * <code>$this->getRequest()->getParameterHolder()->get($name)</code>
  118. *
  119. * @param string $name The parameter name
  120. * @param mixed $default The default value if parameter does not exist
  121. *
  122. * @return string The request parameter value
  123. */
  124. public function getRequestParameter($name, $default = null)
  125. {
  126. return $this->requestParameterHolder->get($name, $default);
  127. }
  128. /**
  129. * Returns true if a request parameter exists.
  130. *
  131. * This is a proxy method equivalent to:
  132. *
  133. * <code>$this->getRequest()->getParameterHolder()->has($name)</code>
  134. *
  135. * @param string $name The parameter name
  136. * @return boolean true if the request parameter exists, false otherwise
  137. */
  138. public function hasRequestParameter($name)
  139. {
  140. return $this->requestParameterHolder->has($name);
  141. }
  142. /**
  143. * Retrieves the current sfRequest object.
  144. *
  145. * This is a proxy method equivalent to:
  146. *
  147. * <code>$this->getContext()->getRequest()</code>
  148. *
  149. * @return sfRequest The current sfRequest implementation instance
  150. */
  151. public function getRequest()
  152. {
  153. return $this->request;
  154. }
  155. /**
  156. * Retrieves the current sfResponse object.
  157. *
  158. * This is a proxy method equivalent to:
  159. *
  160. * <code>$this->getContext()->getResponse()</code>
  161. *
  162. * @return sfResponse The current sfResponse implementation instance
  163. */
  164. public function getResponse()
  165. {
  166. return $this->response;
  167. }
  168. /**
  169. * Retrieves the current sfController object.
  170. *
  171. * This is a proxy method equivalent to:
  172. *
  173. * <code>$this->getContext()->getController()</code>
  174. *
  175. * @return sfController The current sfController implementation instance
  176. */
  177. public function getController()
  178. {
  179. return $this->context->getController();
  180. }
  181. /**
  182. * Generates a URL for the given route and arguments.
  183. *
  184. * This is a proxy method equivalent to:
  185. *
  186. * <code>$this->getContext()->getRouting()->generate(...)</code>
  187. *
  188. * @param string The route name
  189. * @param array An array of parameters for the route
  190. * @param Boolean Whether to generate an absolute URL or not
  191. *
  192. * @return string The URL
  193. */
  194. public function generateUrl($route, $params = array(), $absolute = false)
  195. {
  196. return $this->context->getRouting()->generate($route, $params, $absolute);
  197. }
  198. /**
  199. * Retrieves the current sfUser object.
  200. *
  201. * This is a proxy method equivalent to:
  202. *
  203. * <code>$this->getContext()->getUser()</code>
  204. *
  205. * @return sfUser The current sfUser implementation instance
  206. */
  207. public function getUser()
  208. {
  209. return $this->context->getUser();
  210. }
  211. /**
  212. * Gets the current mailer instance.
  213. *
  214. * @return sfMailer A sfMailer instance
  215. */
  216. public function getMailer()
  217. {
  218. return $this->getContext()->getMailer();
  219. }
  220. /**
  221. * Sets a variable for the template.
  222. *
  223. * If you add a safe value, the variable won't be output escaped
  224. * by symfony, so this is your responsability to ensure that the
  225. * value is escaped properly.
  226. *
  227. * @param string $name The variable name
  228. * @param mixed $value The variable value
  229. * @param Boolean $safe true if the value is safe for output (false by default)
  230. */
  231. public function setVar($name, $value, $safe = false)
  232. {
  233. $this->varHolder->set($name, $safe ? new sfOutputEscaperSafe($value) : $value);
  234. }
  235. /**
  236. * Gets a variable set for the template.
  237. *
  238. * @param string $name The variable name
  239. *
  240. * @return mixed The variable value
  241. */
  242. public function getVar($name)
  243. {
  244. return $this->varHolder->get($name);
  245. }
  246. /**
  247. * Gets the sfParameterHolder object that stores the template variables.
  248. *
  249. * @return sfParameterHolder The variable holder.
  250. */
  251. public function getVarHolder()
  252. {
  253. return $this->varHolder;
  254. }
  255. /**
  256. * Sets a variable for the template.
  257. *
  258. * This is a shortcut for:
  259. *
  260. * <code>$this->setVar('name', 'value')</code>
  261. *
  262. * @param string $key The variable name
  263. * @param string $value The variable value
  264. *
  265. * @return boolean always true
  266. *
  267. * @see setVar()
  268. */
  269. public function __set($key, $value)
  270. {
  271. return $this->varHolder->setByRef($key, $value);
  272. }
  273. /**
  274. * Gets a variable for the template.
  275. *
  276. * This is a shortcut for:
  277. *
  278. * <code>$this->getVar('name')</code>
  279. *
  280. * @param string $key The variable name
  281. *
  282. * @return mixed The variable value
  283. *
  284. * @see getVar()
  285. */
  286. public function & __get($key)
  287. {
  288. return $this->varHolder->get($key);
  289. }
  290. /**
  291. * Returns true if a variable for the template is set.
  292. *
  293. * This is a shortcut for:
  294. *
  295. * <code>$this->getVarHolder()->has('name')</code>
  296. *
  297. * @param string $name The variable name
  298. *
  299. * @return boolean true if the variable is set
  300. */
  301. public function __isset($name)
  302. {
  303. return $this->varHolder->has($name);
  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 $name The variable Name
  313. */
  314. public function __unset($name)
  315. {
  316. $this->varHolder->remove($name);
  317. }
  318. /**
  319. * Calls methods defined via sfEventDispatcher.
  320. *
  321. * @param string $method The method name
  322. * @param array $arguments The method arguments
  323. *
  324. * @return mixed The returned value of the called method
  325. *
  326. * @throws sfException If called method is undefined
  327. */
  328. public function __call($method, $arguments)
  329. {
  330. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'component.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  331. if (!$event->isProcessed())
  332. {
  333. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  334. }
  335. return $event->getReturnValue();
  336. }
  337. }

Debug toolbar