Development

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

You must first sign up to be able to contribute.

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

Revision 21904, 9.8 kB (checked in by FabianLange, 5 years ago)

[1.2, 1.3] corrected deprecation message for component function debugMessage

  • 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 function is deprecated in favor of the logMessage function.', '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    * Generates a URL for the given route and arguments.
228    *
229    * This is a proxy method equivalent to:
230    *
231    * <code>$this->getContext()->getRouting()->generate(...)</code>
232    *
233    * @param string  The route name
234    * @param array   An array of parameters for the route
235    * @param Boolean Whether to generate an absolute URL or not
236    *
237    * @return string  The URL
238    */
239   public function generateUrl($route, $params = array(), $absolute = false)
240   {
241     return $this->context->getRouting()->generate($route, $params, $absolute);
242   }
243
244   /**
245    * Retrieves the current sfUser object.
246    *
247    * This is a proxy method equivalent to:
248    *
249    * <code>$this->getContext()->getUser()</code>
250    *
251    * @return sfUser The current sfUser implementation instance
252    */
253   public function getUser()
254   {
255     return $this->context->getUser();
256   }
257
258   /**
259    * Sets a variable for the template.
260    *
261    * If you add a safe value, the variable won't be output escaped
262    * by symfony, so this is your responsability to ensure that the
263    * value is escaped properly.
264    *
265    * @param string  $name  The variable name
266    * @param mixed   $value The variable value
267    * @param Boolean $safe  true if the value is safe for output (false by default)
268    */
269   public function setVar($name, $value, $safe = false)
270   {
271     $this->varHolder->set($name, $safe ? new sfOutputEscaperSafe($value) : $value);
272   }
273
274   /**
275    * Gets a variable set for the template.
276    *
277    * @param string $name The variable name
278    *
279    * @return mixed  The variable value
280    */
281   public function getVar($name)
282   {
283     return $this->varHolder->get($name);
284   }
285
286   /**
287    * Gets the sfParameterHolder object that stores the template variables.
288    *
289    * @return sfParameterHolder The variable holder.
290    */
291   public function getVarHolder()
292   {
293     return $this->varHolder;
294   }
295
296   /**
297    * Sets a variable for the template.
298    *
299    * This is a shortcut for:
300    *
301    * <code>$this->setVar('name', 'value')</code>
302    *
303    * @param string $key   The variable name
304    * @param string $value The variable value
305    *
306    * @return boolean always true
307    *
308    * @see setVar()
309    */
310   public function __set($key, $value)
311   {
312     return $this->varHolder->setByRef($key, $value);
313   }
314
315   /**
316    * Gets a variable for the template.
317    *
318    * This is a shortcut for:
319    *
320    * <code>$this->getVar('name')</code>
321    *
322    * @param string $key The variable name
323    *
324    * @return mixed The variable value
325    *
326    * @see getVar()
327    */
328   public function & __get($key)
329   {
330     return $this->varHolder->get($key);
331   }
332
333   /**
334    * Returns true if a variable for the template is set.
335    *
336    * This is a shortcut for:
337    *
338    * <code>$this->getVarHolder()->has('name')</code>
339    *
340    * @param string $name The variable name
341    *
342    * @return boolean true if the variable is set
343    */
344   public function __isset($name)
345   {
346     return $this->varHolder->has($name);
347   }
348
349   /**
350    * Removes a variable for the template.
351    *
352    * This is just really a shortcut for:
353    *
354    * <code>$this->getVarHolder()->remove('name')</code>
355    *
356    * @param string $name The variable Name
357    */
358   public function __unset($name)
359   {
360     $this->varHolder->remove($name);
361   }
362
363   /**
364    * Calls methods defined via sfEventDispatcher.
365    *
366    * @param string $method The method name
367    * @param array  $arguments The method arguments
368    *
369    * @return mixed The returned value of the called method
370    *
371    * @throws sfException If called method is undefined
372    */
373   public function __call($method, $arguments)
374   {
375     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'component.method_not_found', array('method' => $method, 'arguments' => $arguments)));
376     if (!$event->isProcessed())
377     {
378       throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
379     }
380
381     return $event->getReturnValue();
382   }
383 }
384
Note: See TracBrowser for help on using the browser.