PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/data/symfony/plugins/sfCompat10Plugin/lib/filter/sfValidationExecutionFilter.class.php

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