Development

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

You must first sign up to be able to contribute.

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

Revision 9477, 15.8 kB (checked in by fabien, 6 years ago)

refactored renderPartial() and renderComponent() in sfAction (code has been splitted to create a getPartial() and a getComponent() method)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
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  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfAction executes all the logic for the current request.
14  *
15  * @package    symfony
16  * @subpackage action
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 abstract class sfAction extends sfComponent
22 {
23   protected
24     $security = array();
25
26   /**
27    * Initializes this action.
28    *
29    * @param sfContext $context    The current application context.
30    * @param string    $moduleName The module name.
31    * @param string    $actionName The action name.
32    *
33    * @return bool true, if initialization completes successfully, otherwise false
34    */
35   public function initialize($context, $moduleName, $actionName)
36   {
37     parent::initialize($context, $moduleName, $actionName);
38
39     // include security configuration
40     if ($file = $context->getConfigCache()->checkConfig('modules/'.$this->getModuleName().'/config/security.yml', true))
41     {
42       require($file);
43     }
44   }
45
46   /**
47    * Executes an application defined process prior to execution of this sfAction object.
48    *
49    * By default, this method is empty.
50    */
51   public function preExecute()
52   {
53   }
54
55   /**
56    * Execute an application defined process immediately after execution of this sfAction object.
57    *
58    * By default, this method is empty.
59    */
60   public function postExecute()
61   {
62   }
63
64   /**
65    * Forwards current action to the default 404 error action.
66    *
67    * @param string $message Message of the generated exception
68    *
69    * @throws sfError404Exception
70    *
71    */
72   public function forward404($message = null)
73   {
74     throw new sfError404Exception($this->get404Message($message));
75   }
76
77   /**
78    * Forwards current action to the default 404 error action unless the specified condition is true.
79    *
80    * @param bool    $condition  A condition that evaluates to true or false
81    * @param string  $message    Message of the generated exception
82    *
83    * @throws sfError404Exception
84    */
85   public function forward404Unless($condition, $message = null)
86   {
87     if (!$condition)
88     {
89       throw new sfError404Exception($this->get404Message($message));
90     }
91   }
92
93   /**
94    * Forwards current action to the default 404 error action if the specified condition is true.
95    *
96    * @param bool    $condition  A condition that evaluates to true or false
97    * @param string  $message    Message of the generated exception
98    *
99    * @throws sfError404Exception
100    */
101   public function forward404If($condition, $message = null)
102   {
103     if ($condition)
104     {
105       throw new sfError404Exception($this->get404Message($message));
106     }
107   }
108
109   /**
110    * Redirects current action to the default 404 error action (with browser redirection).
111    *
112    * This method stops the current code flow.
113    */
114   public function redirect404()
115   {
116     return $this->redirect('/'.sfConfig::get('sf_error_404_module').'/'.sfConfig::get('sf_error_404_action'));
117   }
118
119   /**
120    * Forwards current action to a new one (without browser redirection).
121    *
122    * This method stops the action. So, no code is executed after a call to this method.
123    *
124    * @param  string  $module  A module name
125    * @param  string  $action  An action name
126    *
127    * @throws sfStopException
128    */
129   public function forward($module, $action)
130   {
131     if (sfConfig::get('sf_logging_enabled'))
132     {
133       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Forward to action "%s/%s"', $module, $action))));
134     }
135
136     $this->getController()->forward($module, $action);
137
138     throw new sfStopException();
139   }
140
141   /**
142    * If the condition is true, forwards current action to a new one (without browser redirection).
143    *
144    * This method stops the action. So, no code is executed after a call to this method.
145    *
146    * @param  bool   $condition  A condition that evaluates to true or false
147    * @param  string $module     A module name
148    * @param  string $action     An action name
149    *
150    * @throws sfStopException
151    */
152   public function forwardIf($condition, $module, $action)
153   {
154     if ($condition)
155     {
156       $this->forward($module, $action);
157     }
158   }
159
160   /**
161    * Unless the condition is true, forwards current action to a new one (without browser redirection).
162    *
163    * This method stops the action. So, no code is executed after a call to this method.
164    *
165    * @param  bool   $condition  A condition that evaluates to true or false
166    * @param  string $module     A module name
167    * @param  string $action     An action name
168    *
169    * @throws sfStopException
170    */
171   public function forwardUnless($condition, $module, $action)
172   {
173     if (!$condition)
174     {
175       $this->forward($module, $action);
176     }
177   }
178
179   /**
180    * Redirects current request to a new URL.
181    *
182    * 2 URL formats are accepted :
183    *  - a full URL: http://www.google.com/
184    *  - an internal URL (url_for() format): module/action
185    *
186    * This method stops the action. So, no code is executed after a call to this method.
187    *
188    * @param  string $url         Url
189    * @param  string $statusCode  Status code (default to 302)
190    *
191    * @throws sfStopException
192    */
193   public function redirect($url, $statusCode = 302)
194   {
195     $this->getController()->redirect($url, 0, $statusCode);
196
197     throw new sfStopException();
198   }
199
200   /**
201    * Redirects current request to a new URL, only if specified condition is true.
202    *
203    * This method stops the action. So, no code is executed after a call to this method.
204    *
205    * @param  bool   $condition  A condition that evaluates to true or false
206    * @param  string $url        Url
207    *
208    * @throws sfStopException
209    *
210    * @see redirect
211    */
212   public function redirectIf($condition, $url)
213   {
214     if ($condition)
215     {
216       $this->redirect($url);
217     }
218   }
219
220   /**
221    * Redirects current request to a new URL, unless specified condition is true.
222    *
223    * This method stops the action. So, no code is executed after a call to this method.
224    *
225    * @param  bool   $condition  A condition that evaluates to true or false
226    * @param  string $url        Url
227    *
228    * @throws sfStopException
229    *
230    * @see redirect
231    */
232   public function redirectUnless($condition, $url)
233   {
234     if (!$condition)
235     {
236       $this->redirect($url);
237     }
238   }
239
240   /**
241    * Appends the given text to the response content and bypasses the built-in view system.
242    *
243    * This method must be called as with a return:
244    *
245    * <code>return $this->renderText('some text')</code>
246    *
247    * @param string $text Text to append to the response
248    *
249    * @return sfView::NONE
250    */
251   public function renderText($text)
252   {
253     $this->getResponse()->setContent($this->getResponse()->getContent().$text);
254
255     return sfView::NONE;
256   }
257
258   /**
259    * Returns the partial rendered content.
260    *
261    * If the vars parameter is omitted, the action's internal variables
262    * will be passed, just as it would to a normal template.
263    *
264    * If the vars parameter is set then only those values are
265    * available in the partial.
266    *
267    * @param  string $templateName partial name
268    * @param  array  $vars         vars
269    *
270    * @return string The partial content
271    */
272   public function getPartial($templateName, $vars = null)
273   {
274     sfLoader::loadHelpers('Partial');
275
276     $vars = !is_null($vars) ? $vars : $this->varHolder->getAll();
277
278     return get_partial($templateName, $vars);
279   }
280
281   /**
282    * Appends the result of the given partial execution to the response content.
283    *
284    * This method must be called as with a return:
285    *
286    * <code>return $this->renderPartial('foo/bar')</code>
287    *
288    * @param  string $templateName partial name
289    * @param  array  $vars         vars
290    *
291    * @return sfView::NONE
292    *
293    * @see    getPartial
294    */
295   public function renderPartial($templateName, $vars = null)
296   {
297     return $this->renderText($this->getPartial($templateName, $vars));
298   }
299
300   /**
301    * Returns the component rendered content.
302    *
303    * If the vars parameter is omitted, the action's internal variables
304    * will be passed, just as it would to a normal template.
305    *
306    * If the vars parameter is set then only those values are
307    * available in the component.
308    *
309    * @param  string  $moduleName    module name
310    * @param  string  $componentNae  component name
311    * @param  array   $vars          vars
312    *
313    * @return string  The component rendered content
314    */
315   public function getComponent($moduleName, $componentName, $vars = null)
316   {
317     sfLoader::loadHelpers('Partial');
318
319     $vars = !is_null($vars) ? $vars : $this->varHolder->getAll();
320
321     return get_component($moduleName, $componentName, $vars);
322   }
323
324   /**
325    * Appends the result of the given component execution to the response content.
326    *
327    * This method must be called as with a return:
328    *
329    * <code>return $this->renderComponent('foo', 'bar')</code>
330    *
331    * @param  string  $moduleName    module name
332    * @param  string  $componentNae  component name
333    * @param  array   $vars          vars
334    *
335    * @return sfView::NONE
336    *
337    * @see    getComponent
338    */
339   public function renderComponent($moduleName, $componentName, $vars = null)
340   {
341     return $this->renderText($this->getComponent($moduleName, $componentName, $vars));
342   }
343
344   /**
345    * Retrieves the default view to be executed when a given request is not served by this action.
346    *
347    * @return string A string containing the view name associated with this action
348    */
349   public function getDefaultView()
350   {
351     if (!sfConfig::get('sf_compat_10'))
352     {
353       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
354     }
355
356     return sfView::INPUT;
357   }
358
359   /**
360    * Retrieves the request methods on which this action will process validation and execution.
361    *
362    * @return int One of the following values:
363    *
364    * - sfRequest::GET
365    * - sfRequest::POST
366    * - sfRequest::PUT
367    * - sfRequest::DELETE
368    * - sfRequest::HEAD
369    * - sfRequest::NONE
370    *
371    * @see sfRequest
372    */
373   public function getRequestMethods()
374   {
375     if (!sfConfig::get('sf_compat_10'))
376     {
377       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
378     }
379
380     return sfRequest::GET
381            | sfRequest::POST
382            | sfRequest::PUT
383            | sfRequest::DELETE
384            | sfRequest::HEAD
385            | sfRequest::NONE;
386   }
387
388   /**
389    * Executes any post-validation error application logic.
390    *
391    * @return string A string containing the view name associated with this action
392    */
393   public function handleError()
394   {
395     if (!sfConfig::get('sf_compat_10'))
396     {
397       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
398     }
399
400     return sfView::ERROR;
401   }
402
403   /**
404    * Validates manually files and parameters.
405    *
406    * @return bool true, if validation completes successfully, otherwise false.
407    */
408   public function validate()
409   {
410     if (!sfConfig::get('sf_compat_10'))
411     {
412       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
413     }
414
415     return true;
416   }
417
418   /**
419    * Returns the security configuration for this module.
420    *
421    * @return string Current security configuration as an array
422    */
423   public function getSecurityConfiguration()
424   {
425     return $this->security;
426   }
427
428   /**
429    * Overrides the current security configuration for this module.
430    *
431    * @param array $security The new security configuration
432    */
433   public function setSecurityConfiguration($security)
434   {
435     $this->security = $security;
436   }
437
438   /**
439    * Indicates that this action requires security.
440    *
441    * @return bool true, if this action requires security, otherwise false.
442    */
443   public function isSecure()
444   {
445     $actionName = strtolower($this->getActionName());
446
447     if (isset($this->security[$actionName]['is_secure']))
448     {
449       return $this->security[$actionName]['is_secure'];
450     }
451
452     if (isset($this->security['all']['is_secure']))
453     {
454       return $this->security['all']['is_secure'];
455     }
456
457     return false;
458   }
459
460   /**
461    * Gets credentials the user must have to access this action.
462    *
463    * @return mixed An array or a string describing the credentials the user must have to access this action
464    */
465   public function getCredential()
466   {
467     $actionName = strtolower($this->getActionName());
468
469     if (isset($this->security[$actionName]['credentials']))
470     {
471       $credentials = $this->security[$actionName]['credentials'];
472     }
473     else if (isset($this->security['all']['credentials']))
474     {
475       $credentials = $this->security['all']['credentials'];
476     }
477     else
478     {
479       $credentials = null;
480     }
481
482     return $credentials;
483   }
484
485   /**
486    * Sets an alternate template for this sfAction.
487    *
488    * See 'Naming Conventions' in the 'Symfony View' documentation.
489    *
490    * @param string $name    Template name
491    * @param string $module  The module (current if null)
492    */
493   public function setTemplate($name, $module = null)
494   {
495     if (sfConfig::get('sf_logging_enabled'))
496     {
497       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Change template to "%s/%s"', is_null($module) ? 'CURRENT' : $module, $name))));
498     }
499
500     if (!is_null($module))
501     {
502       $name = sfConfig::get('sf_app_dir').'/modules/'.$module.'/templates/'.$name;
503     }
504
505     sfConfig::set('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_template', $name);
506   }
507
508   /**
509    * Gets the name of the alternate template for this sfAction.
510    *
511    * WARNING: It only returns the template you set with the setTemplate() method,
512    *          and does not return the template that you configured in your view.yml.
513    *
514    * See 'Naming Conventions' in the 'Symfony View' documentation.
515    *
516    * @return string Template name. Returns null if no template has been set within the action
517    */
518   public function getTemplate()
519   {
520     return sfConfig::get('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_template');
521   }
522
523   /**
524    * Sets an alternate layout for this sfAction.
525    *
526    * To de-activate the layout, set the layout name to false.
527    *
528    * To revert the layout to the one configured in the view.yml, set the template name to null.
529    *
530    * @param mixed $name Layout name or false to de-activate the layout
531    */
532   public function setLayout($name)
533   {
534     if (sfConfig::get('sf_logging_enabled'))
535     {
536       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Change layout to "%s"', $name))));
537     }
538
539     sfConfig::set('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_layout', $name);
540   }
541
542   /**
543    * Gets the name of the alternate layout for this sfAction.
544    *
545    * WARNING: It only returns the layout you set with the setLayout() method,
546    *          and does not return the layout that you configured in your view.yml.
547    *
548    * @return mixed Layout name. Returns null if no layout has been set within the action
549    */
550   public function getLayout()
551   {
552     return sfConfig::get('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_layout');
553   }
554
555   /**
556    * Changes the default view class used for rendering the template associated with the current action.
557    *
558    * @param string $class View class name
559    */
560   public function setViewClass($class)
561   {
562     sfConfig::set('mod_'.strtolower($this->getModuleName()).'_view_class', $class);
563   }
564
565   /**
566    * Returns a formatted message for a 404 error.
567    *
568    * @param  string $message An error message (null by default)
569    *
570    * @return string The error message or a default one if null
571    */
572   protected function get404Message($message = null)
573   {
574     return is_null($message) ? sprintf('This request has been forwarded to a 404 error page by the action "%s/%s".', $this->getModuleName(), $this->getActionName()) : $message;
575   }
576 }
577
Note: See TracBrowser for help on using the browser.