1. sfExecutionFilter.class.php
  2. /** * sfExecutionFilter is the last filter registered for each filter chain. This * filter does all action and view execution. * * @package symfony * @subpackage filter * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfExecutionFilter.class.php 24615 2009-11-30 22:30:46Z Kris.Wallsmith $ */
  3. class sfExecutionFilter extends sfFilter
  4. {
  5. /**
  6. * Executes this filter.
  7. *
  8. * @param sfFilterChain $filterChain The filter chain
  9. *
  10. * @throws <b>sfInitializeException</b> If an error occurs during view initialization.
  11. * @throws <b>sfViewException</b> If an error occurs while executing the view.
  12. */
  13. public function execute($filterChain)
  14. {
  15. // get the current action instance
  16. $actionInstance = $this->context->getController()->getActionStack()->getLastEntry()->getActionInstance();
  17. // execute the action, execute and render the view
  18. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  19. {
  20. $timer = sfTimerManager::getTimer(sprintf('Action "%s/%s"', $actionInstance->getModuleName(), $actionInstance->getActionName()));
  21. $viewName = $this->handleAction($filterChain, $actionInstance);
  22. $timer->addTime();
  23. $timer = sfTimerManager::getTimer(sprintf('View "%s" for "%s/%s"', $viewName, $actionInstance->getModuleName(), $actionInstance->getActionName()));
  24. $this->handleView($filterChain, $actionInstance, $viewName);
  25. $timer->addTime();
  26. }
  27. else
  28. {
  29. $viewName = $this->handleAction($filterChain, $actionInstance);
  30. $this->handleView($filterChain, $actionInstance, $viewName);
  31. }
  32. }
  33. /**
  34. * Handles the action.
  35. *
  36. * @param sfFilterChain $filterChain The current filter chain
  37. * @param sfAction $actionInstance An sfAction instance
  38. *
  39. * @return string The view type
  40. */
  41. protected function handleAction($filterChain, $actionInstance)
  42. {
  43. if (sfConfig::get('sf_cache'))
  44. {
  45. $uri = $this->context->getViewCacheManager()->getCurrentCacheKey();
  46. if (null !== $uri && $this->context->getViewCacheManager()->hasActionCache($uri))
  47. {
  48. // action in cache, so go to the view
  49. return sfView::SUCCESS;
  50. }
  51. }
  52. return $this->executeAction($actionInstance);
  53. }
  54. /**
  55. * Executes the execute method of an action.
  56. *
  57. * @param sfAction $actionInstance An sfAction instance
  58. *
  59. * @return string The view type
  60. */
  61. protected function executeAction($actionInstance)
  62. {
  63. // execute the action
  64. $actionInstance->preExecute();
  65. $viewName = $actionInstance->execute($this->context->getRequest());
  66. $actionInstance->postExecute();
  67. return null === $viewName ? sfView::SUCCESS : $viewName;
  68. }
  69. /**
  70. * Handles the view.
  71. *
  72. * @param sfFilterChain $filterChain The current filter chain
  73. * @param sfAction $actionInstance An sfAction instance
  74. * @param string $viewName The view name
  75. */
  76. protected function handleView($filterChain, $actionInstance, $viewName)
  77. {
  78. switch ($viewName)
  79. {
  80. case sfView::HEADER_ONLY:
  81. $this->context->getResponse()->setHeaderOnly(true);
  82. return;
  83. case sfView::NONE:
  84. return;
  85. }
  86. $this->executeView($actionInstance->getModuleName(), $actionInstance->getActionName(), $viewName, $actionInstance->getVarHolder()->getAll());
  87. }
  88. /**
  89. * Executes and renders the view.
  90. *
  91. * The behavior of this method depends on the controller render mode:
  92. *
  93. * - sfView::NONE: Nothing happens.
  94. * - sfView::RENDER_CLIENT: View data populates the response content.
  95. * - sfView::RENDER_DATA: View data populates the data presentation variable.
  96. *
  97. * @param string $moduleName The module name
  98. * @param string $actionName The action name
  99. * @param string $viewName The view name
  100. * @param array $viewAttributes An array of view attributes
  101. *
  102. * @return string The view data
  103. */
  104. protected function executeView($moduleName, $actionName, $viewName, $viewAttributes)
  105. {
  106. $controller = $this->context->getController();
  107. // get the view instance
  108. $view = $controller->getView($moduleName, $actionName, $viewName);
  109. // execute the view
  110. $view->execute();
  111. // pass attributes to the view
  112. $view->getAttributeHolder()->add($viewAttributes);
  113. // render the view
  114. switch ($controller->getRenderMode())
  115. {
  116. case sfView::RENDER_NONE:
  117. break;
  118. case sfView::RENDER_CLIENT:
  119. $viewData = $view->render();
  120. $this->context->getResponse()->setContent($viewData);
  121. break;
  122. case sfView::RENDER_VAR:
  123. $viewData = $view->render();
  124. $controller->getActionStack()->getLastEntry()->setPresentation($viewData);
  125. break;
  126. }
  127. }
  128. }

Debug toolbar