sfComponent.class.php 9.9 KB

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