| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 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 |
|
|---|
| 35 |
$actionInstance = $this->context->getController()->getActionStack()->getLastEntry()->getActionInstance(); |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 103 |
$validated = true; |
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 123 |
// do NOT use require_once |
|---|
| 124 |
if (!is_null($validateFile)) |
|---|
| 125 |
{ |
|---|
| 126 |
|
|---|
| 127 |
$validatorManager = new sfValidatorManager($this->context); |
|---|
| 128 |
|
|---|
| 129 |
require($validateFile); |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
$validated = $validatorManager->execute(); |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
$validateToRun = 'validate'.ucfirst($actionName); |
|---|
| 137 |
$manualValidated = method_exists($actionInstance, $validateToRun) ? $actionInstance->$validateToRun() : $actionInstance->validate(); |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 233 |
$view = $controller->getView($moduleName, $actionName, $viewName); |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
$view->execute(); |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
$view->getAttributeHolder()->add($viewAttributes); |
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 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 |
|
|---|
| 268 |
if (isset($parameters['enabled']) && $parameters['enabled'] && !$filterChain->hasFilter('sfFillInFormFilter')) |
|---|
| 269 |
{ |
|---|
| 270 |
|
|---|
| 271 |
$fillInFormFilter = new sfFillInFormFilter($this->context, isset($parameters['param']) ? $parameters['param'] : array()); |
|---|
| 272 |
$filterChain->register($fillInFormFilter); |
|---|
| 273 |
} |
|---|
| 274 |
} |
|---|
| 275 |
} |
|---|
| 276 |
|
|---|