Development

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

You must first sign up to be able to contribute.

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

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

1.1: added deprecated warnings to all sfCompatPlugin classes (closes #3882)

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