Development

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

You must first sign up to be able to contribute.

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

Revision 24615, 8.9 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] updated page and action caching to consider GET parameters (closes #4708)

  • 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     if (sfConfig::get('sf_cache'))
78     {
79       $uri = $this->context->getViewCacheManager()->getCurrentCacheKey();
80       if (null !== $uri && $this->context->getViewCacheManager()->hasActionCache($uri))
81       {
82         // action in cache, so go to the view
83         return sfView::SUCCESS;
84       }
85     }
86
87     return $this->validateAction($filterChain, $actionInstance) ? $this->executeAction($actionInstance) : $this->handleErrorAction($actionInstance);
88   }
89
90   /**
91    * Validates an sfAction instance.
92    *
93    * @param  sfAction An sfAction instance
94    *
95    * @return boolean  True if the action is validated, false otherwise
96    */
97   protected function validateAction($filterChain, $actionInstance)
98   {
99     $moduleName = $actionInstance->getModuleName();
100     $actionName = $actionInstance->getActionName();
101
102     // set default validated status
103     $validated = true;
104
105     // the case of the first letter of the action is insignificant
106     // get the current action validation configuration
107     $validationConfigWithFirstLetterLower = strtolower(substr($actionName, 0, 1)).substr($actionName, 1).'.yml';
108     $validationConfigWithFirstLetterUpper = ucfirst($actionName).'.yml';
109
110     // determine $validateFile by testing both the uppercase and lowercase
111     // types of validation configurations.
112     $validateFile = null;
113     if (!is_null($testValidateFile = $this->context->getConfigCache()->checkConfig('modules/'.$moduleName.'/validate/'.$validationConfigWithFirstLetterLower, true)))
114     {
115       $validateFile = $testValidateFile;
116     }
117     else if (!is_null($testValidateFile = $this->context->getConfigCache()->checkConfig('modules/'.$moduleName.'/validate/'.$validationConfigWithFirstLetterUpper, true)))
118     {
119       $validateFile = $testValidateFile;
120     }
121
122     // load validation configuration
123     // do NOT use require_once
124     if (!is_null($validateFile))
125     {
126       // create validator manager
127       $validatorManager = new sfValidatorManager($this->context);
128
129       require($validateFile);
130
131       // process validators
132       $validated = $validatorManager->execute();
133     }
134
135     // process manual validation
136     $validateToRun = 'validate'.ucfirst($actionName);
137     $manualValidated = method_exists($actionInstance, $validateToRun) ? $actionInstance->$validateToRun() : $actionInstance->validate();
138
139     // action is validated if:
140     // - all validation methods (manual and automatic) return true
141     // - or automatic validation returns false but errors have been 'removed' by manual validation
142     $validated = ($manualValidated && $validated) || ($manualValidated && !$validated && !$this->context->getRequest()->hasErrors());
143
144     // register fill-in filter
145     if (null !== ($parameters = $this->context->getRequest()->getAttribute('symfony.fillin')))
146     {
147       $this->registerFillInFilter($filterChain, $parameters);
148     }
149
150     if (!$validated && sfConfig::get('sf_logging_enabled'))
151     {
152       $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array('Action validation failed')));
153     }
154
155     return $validated;
156   }
157
158   /**
159    * Executes the execute method of an action.
160    *
161    * @param  sfAction An sfAction instance
162    *
163    * @return string   The view type
164    */
165   protected function executeAction($actionInstance)
166   {
167     // execute the action
168     $actionInstance->preExecute();
169     $viewName = $actionInstance->execute($this->context->getRequest());
170     $actionInstance->postExecute();
171
172     return $viewName ? $viewName : sfView::SUCCESS;
173   }
174
175   /**
176    * Executes the handleError method of an action.
177    *
178    * @param  sfAction An sfAction instance
179    *
180    * @return string   The view type
181    */
182   protected function handleErrorAction($actionInstance)
183   {
184     // validation failed
185     $handleErrorToRun = 'handleError'.ucfirst($actionInstance->getActionName());
186     $viewName = method_exists($actionInstance, $handleErrorToRun) ? $actionInstance->$handleErrorToRun() : $actionInstance->handleError();
187
188     return $viewName ? $viewName : sfView::ERROR;
189   }
190
191   /**
192    * Handles the view.
193    *
194    * @param  sfFilterChain The current filter chain
195    * @param sfAction       An sfAction instance
196    * @param string         The view name
197    */
198   protected function handleView($filterChain, $actionInstance, $viewName)
199   {
200     switch ($viewName)
201     {
202       case sfView::HEADER_ONLY:
203         $this->context->getResponse()->setHeaderOnly(true);
204         return;
205       case sfView::NONE:
206         return;
207     }
208
209     $this->executeView($actionInstance->getModuleName(), $actionInstance->getActionName(), $viewName, $actionInstance->getVarHolder()->getAll());
210   }
211
212   /**
213    * Executes and renders the view.
214    *
215    * The behavior of this method depends on the controller render mode:
216    *
217    *   - sfView::NONE: Nothing happens.
218    *   - sfView::RENDER_CLIENT: View data populates the response content.
219    *   - sfView::RENDER_DATA: View data populates the data presentation variable.
220    *
221    * @param  string The module name
222    * @param  string The action name
223    * @param  string The view name
224    * @param  array  An array of view attributes
225    *
226    * @return string The view data
227    */
228   protected function executeView($moduleName, $actionName, $viewName, $viewAttributes)
229   {
230     $controller = $this->context->getController();
231
232     // get the view instance
233     $view = $controller->getView($moduleName, $actionName, $viewName);
234
235     // execute the view
236     $view->execute();
237
238     // pass attributes to the view
239     $view->getAttributeHolder()->add($viewAttributes);
240
241     // render the view
242     switch ($controller->getRenderMode())
243     {
244       case sfView::RENDER_NONE:
245         break;
246
247       case sfView::RENDER_CLIENT:
248         $viewData = $view->render();
249         $this->context->getResponse()->setContent($viewData);
250         break;
251
252       case sfView::RENDER_VAR:
253         $viewData = $view->render();
254         $controller->getActionStack()->getLastEntry()->setPresentation($viewData);
255         break;
256     }
257   }
258
259   /**
260    * Registers the fill in filter in the filter chain.
261    *
262    * @param sfFilterChain A sfFilterChain implementation instance
263    * @param array         An array of parameters to pass to the fill in filter.
264    */
265   protected function registerFillInFilter($filterChain, $parameters)
266   {
267     // automatically register the fill in filter if it is not already loaded in the chain
268     if (isset($parameters['enabled']) && $parameters['enabled'] && !$filterChain->hasFilter('sfFillInFormFilter'))
269     {
270       // register the fill in form filter
271       $fillInFormFilter = new sfFillInFormFilter($this->context, isset($parameters['param']) ? $parameters['param'] : array());
272       $filterChain->register($fillInFormFilter);
273     }
274   }
275 }
276
Note: See TracBrowser for help on using the browser.