sfExecutionFilter.class.php 5.0 KB

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