Development

/branches/1.1/lib/action/sfComponent.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/action/sfComponent.class.php

Revision 9044, 9.3 kB (checked in by Carl.Vondrick, 6 years ago)

1.1: fixed @param phpdoc to fit specs in actions (refs #2991)

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