Development

/branches/1.2/lib/plugins/sfCompat10Plugin/lib/filter/sfValidationExecutionFilter.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfCompat10Plugin/lib/filter/sfValidationExecutionFilter.class.php

Revision 11254, 8.9 kB (checked in by fabien, 6 years ago)

[1.2] added support for PUT and DELETE methods from a browser

  • 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  * (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  * sfExecutionFilter is the last filter registered for each filter chain. This
14  * filter does all action and view execution.
15  *
16  * @package    symfony
17  * @subpackage filter
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @author     Sean Kerr <sean@code-box.org>
20  * @version    SVN: $Id$
21  */
22 class sfValidationExecutionFilter extends sfFilter
23 {
24   /**
25    * Executes this filter.
26    *
27    * @param sfFilterChain The filter chain
28    *
29    * @throws <b>sfInitializeException</b> If an error occurs during view initialization.
30    * @throws <b>sfViewException</b>       If an error occurs while executing the view.
31    */
32   public function execute($filterChain)
33   {
34     // get the current action instance
35     $actionInstance = $this->context->getController()->getActionStack()->getLastEntry()->getActionInstance();
36
37     // validate and execute the action
38     if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
39     {
40       $timer = sfTimerManager::getTimer(sprintf('Action "%s/%s"', $actionInstance->getModuleName(), $actionInstance->getActionName()));
41     }
42
43     $viewName = $this->handleAction($filterChain, $actionInstance);
44
45     if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
46     {
47       $timer->addTime();
48     }
49
50     // execute and render the view
51     if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
52     {
53       $timer = sfTimerManager::getTimer(sprintf('View "%s" for "%s/%s"', $viewName, $actionInstance->getModuleName(), $actionInstance->getActionName()));
54     }
55
56     $this->handleView($filterChain, $actionInstance, $viewName);
57
58     if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
59     {
60       $timer->addTime();
61     }
62
63     // execute the filter chain (needed if fill-in filter is activated by the validation system)
64     $filterChain->execute();
65   }
66
67   /*
68    * Handles the action.
69    *
70    * @param  sfFilterChain The current filter chain
71    * @param  sfAction      An sfAction instance
72    *
73    * @return string        The view type
74    */
75   protected function handleAction($filterChain, $actionInstance)
76   {
77     $uri = $this->context->getRouting()->getCurrentInternalUri();
78
79     if (sfConfig::get('sf_cache') && !is_null($uri) && $this->context->getViewCacheManager()->hasActionCache($uri))
80     {
81       // action in cache, so go to the view
82       return sfView::SUCCESS;
83     }
84
85     return $this->validateAction($filterChain, $actionInstance) ? $this->executeAction($actionInstance) : $this->handleErrorAction($actionInstance);
86   }
87
88   /**
89    * Validates an sfAction instance.
90    *
91    * @param  sfAction An sfAction instance
92    *
93    * @return boolean  True if the action is validated, false otherwise
94    */
95   protected function validateAction($filterChain, $actionInstance)
96   {
97     $moduleName = $actionInstance->getModuleName();
98     $actionName = $actionInstance->getActionName();
99
100     // set default validated status
101     $validated = true;
102
103     // the case of the first letter of the action is insignificant
104     // get the current action validation configuration
105     $validationConfigWithFirstLetterLower = strtolower(substr($actionName, 0, 1)).substr($actionName, 1).'.yml';
106     $validationConfigWithFirstLetterUpper = ucfirst($actionName).'.yml';
107
108     // determine $validateFile by testing both the uppercase and lowercase
109     // types of validation configurations.
110     $validateFile = null;
111     if (!is_null($testValidateFile = $this->context->getConfigCache()->checkConfig('modules/'.$moduleName.'/validate/'.$validationConfigWithFirstLetterLower, true)))
112     {
113       $validateFile = $testValidateFile;
114     }
115     else if (!is_null($testValidateFile = $this->context->getConfigCache()->checkConfig('modules/'.$moduleName.'/validate/'.$validationConfigWithFirstLetterUpper, true)))
116     {
117       $validateFile = $testValidateFile;
118     }
119
120     // load validation configuration
121     // do NOT use require_once
122     if (!is_null($validateFile))
123     {
124       // create validator manager
125       $validatorManager = new sfValidatorManager($this->context);
126
127       require($validateFile);
128
129       // process validators
130       $validated = $validatorManager->execute();
131     }
132
133     // process manual validation
134     $validateToRun = 'validate'.ucfirst($actionName);
135     $manualValidated = method_exists($actionInstance, $validateToRun) ? $actionInstance->$validateToRun() : $actionInstance->validate();
136
137     // action is validated if:
138     // - all validation methods (manual and automatic) return true
139     // - or automatic validation returns false but errors have been 'removed' by manual validation
140     $validated = ($manualValidated && $validated) || ($manualValidated && !$validated && !$this->context->getRequest()->hasErrors());
141
142     // register fill-in filter
143     if (null !== ($parameters = $this->context->getRequest()->getAttribute('symfony.fillin')))
144     {
145       $this->registerFillInFilter($filterChain, $parameters);
146     }
147
148     if (!$validated && sfConfig::get('sf_logging_enabled'))
149     {
150       $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Action validation failed')));
151     }
152
153     return $validated;
154   }
155
156   /**
157    * Executes the execute method of an action.
158    *
159    * @param  sfAction An sfAction instance
160    *
161    * @return string   The view type
162    */
163   protected function executeAction($actionInstance)
164   {
165     // execute the action
166     $actionInstance->preExecute();
167     $viewName = $actionInstance->execute($this->context->getRequest());
168     $actionInstance->postExecute();
169
170     return $viewName ? $viewName : sfView::SUCCESS;
171   }
172
173   /**
174    * Executes the handleError method of an action.
175    *
176    * @param  sfAction An sfAction instance
177    *
178    * @return string   The view type
179    */
180   protected function handleErrorAction($actionInstance)
181   {
182     // validation failed
183     $handleErrorToRun = 'handleError'.ucfirst($actionInstance->getActionName());
184     $viewName = method_exists($actionInstance, $handleErrorToRun) ? $actionInstance->$handleErrorToRun() : $actionInstance->handleError();
185
186     return $viewName ? $viewName : sfView::ERROR;
187   }
188
189   /**
190    * Handles the view.
191    *
192    * @param  sfFilterChain The current filter chain
193    * @param sfAction       An sfAction instance
194    * @param string         The view name
195    */
196   protected function handleView($filterChain, $actionInstance, $viewName)
197   {
198     switch ($viewName)
199     {
200       case sfView::HEADER_ONLY:
201         $this->context->getResponse()->setHeaderOnly(true);
202         return;
203       case sfView::NONE:
204         return;
205     }
206
207     $this->executeView($actionInstance->getModuleName(), $actionInstance->getActionName(), $viewName, $actionInstance->getVarHolder()->getAll());
208   }
209
210   /**
211    * Executes and renders the view.
212    *
213    * The behavior of this method depends on the controller render mode:
214    *
215    *   - sfView::NONE: Nothing happens.
216    *   - sfView::RENDER_CLIENT: View data populates the response content.
217    *   - sfView::RENDER_DATA: View data populates the data presentation variable.
218    *
219    * @param  string The module name
220    * @param  string The action name
221    * @param  string The view name
222    * @param  array  An array of view attributes
223    *
224    * @return string The view data
225    */
226   protected function executeView($moduleName, $actionName, $viewName, $viewAttributes)
227   {
228     $controller = $this->context->getController();
229
230     // get the view instance
231     $view = $controller->getView($moduleName, $actionName, $viewName);
232
233     // execute the view
234     $view->execute();
235
236     // pass attributes to the view
237     $view->getAttributeHolder()->add($viewAttributes);
238
239     // render the view
240     switch ($controller->getRenderMode())
241     {
242       case sfView::RENDER_NONE:
243         break;
244
245       case sfView::RENDER_CLIENT:
246         $viewData = $view->render();
247         $this->context->getResponse()->setContent($viewData);
248         break;
249
250       case sfView::RENDER_VAR:
251         $viewData = $view->render();
252         $controller->getActionStack()->getLastEntry()->setPresentation($viewData);
253         break;
254     }
255   }
256
257   /**
258    * Registers the fill in filter in the filter chain.
259    *
260    * @param sfFilterChain A sfFilterChain implementation instance
261    * @param array         An array of parameters to pass to the fill in filter.
262    */
263   protected function registerFillInFilter($filterChain, $parameters)
264   {
265     // automatically register the fill in filter if it is not already loaded in the chain
266     if (isset($parameters['enabled']) && $parameters['enabled'] && !$filterChain->hasFilter('sfFillInFormFilter'))
267     {
268       // register the fill in form filter
269       $fillInFormFilter = new sfFillInFormFilter($this->context, isset($parameters['param']) ? $parameters['param'] : array());
270       $filterChain->register($fillInFormFilter);
271     }
272   }
273 }
274
Note: See TracBrowser for help on using the browser.