sfComponent.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 9044 2008-05-19 06:18:41Z Carl.Vondrick $
  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. * Retrieves the current sfUser object.
  211. *
  212. * This is a proxy method equivalent to:
  213. *
  214. * <code>$this->getContext()->getUser()</code>
  215. *
  216. * @return sfUser The current sfUser implementation instance
  217. */
  218. public function getUser()
  219. {
  220. return $this->context->getUser();
  221. }
  222. /**
  223. * Sets a variable for the template.
  224. *
  225. * If you add a safe value, the variable won't be output escaped
  226. * by symfony, so this is your responsability to ensure that the
  227. * value is escaped properly.
  228. *
  229. * @param string $name The variable name
  230. * @param mixed $value The variable value
  231. * @param Boolean $safe true if the value is safe for output (false by default)
  232. */
  233. public function setVar($name, $value, $safe = false)
  234. {
  235. $this->varHolder->set($name, $safe ? new sfOutputEscaperSafe($value) : $value);
  236. }
  237. /**
  238. * Gets a variable set for the template.
  239. *
  240. * @param string $name The variable name
  241. * @return mixed The variable value
  242. */
  243. public function getVar($name)
  244. {
  245. return $this->varHolder->get($name);
  246. }
  247. /**
  248. * Gets the sfParameterHolder object that stores the template variables.
  249. *
  250. * @return sfParameterHolder The variable holder.
  251. */
  252. public function getVarHolder()
  253. {
  254. return $this->varHolder;
  255. }
  256. /**
  257. * Sets a variable for the template.
  258. *
  259. * This is a shortcut for:
  260. *
  261. * <code>$this->setVar('name', 'value')</code>
  262. *
  263. * @param string $key The variable name
  264. * @param string $value The variable value
  265. *
  266. * @return boolean always true
  267. *
  268. * @see setVar()
  269. */
  270. public function __set($key, $value)
  271. {
  272. return $this->varHolder->setByRef($key, $value);
  273. }
  274. /**
  275. * Gets a variable for the template.
  276. *
  277. * This is a shortcut for:
  278. *
  279. * <code>$this->getVar('name')</code>
  280. *
  281. * @param string $key The variable name
  282. *
  283. * @return mixed The variable value
  284. *
  285. * @see getVar()
  286. */
  287. public function & __get($key)
  288. {
  289. return $this->varHolder->get($key);
  290. }
  291. /**
  292. * Returns true if a variable for the template is set.
  293. *
  294. * This is a shortcut for:
  295. *
  296. * <code>$this->getVarHolder()->has('name')</code>
  297. *
  298. * @param string $name The variable name
  299. *
  300. * @return boolean true if the variable is set
  301. */
  302. public function __isset($name)
  303. {
  304. return $this->varHolder->has($name);
  305. }
  306. /**
  307. * Removes a variable for the template.
  308. *
  309. * This is just really a shortcut for:
  310. *
  311. * <code>$this->getVarHolder()->remove('name')</code>
  312. *
  313. * @param string $name The variable Name
  314. */
  315. public function __unset($name)
  316. {
  317. $this->varHolder->remove($name);
  318. }
  319. /**
  320. * Calls methods defined via sfEventDispatcher.
  321. *
  322. * @param string $method The method name
  323. * @param array $arguments The method arguments
  324. *
  325. * @return mixed The returned value of the called method
  326. */
  327. public function __call($method, $arguments)
  328. {
  329. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'component.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  330. if (!$event->isProcessed())
  331. {
  332. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  333. }
  334. return $event->getReturnValue();
  335. }
  336. }