-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathsfExecutionFilter.class.php
170 lines (149 loc) · 5.1 KB
/
sfExecutionFilter.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <[email protected]>
* (c) 2004-2006 Sean Kerr <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* 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 <[email protected]>
* @author Sean Kerr <[email protected]>
* @version SVN: $Id$
*/
class sfExecutionFilter extends sfFilter
{
/**
* Executes this filter.
*
* @param sfFilterChain $filterChain The filter chain
*
* @throws <b>sfInitializeException</b> If an error occurs during view initialization.
* @throws <b>sfViewException</b> If an error occurs while executing the view.
*/
public function execute($filterChain)
{
// get the current action instance
/** @var sfAction $actionInstance */
$actionInstance = $this->context->getController()->getActionStack()->getLastEntry()->getActionInstance();
// execute the action, execute and render the view
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
{
$timer = sfTimerManager::getTimer(sprintf('Action "%s/%s"', $actionInstance->getModuleName(), $actionInstance->getActionName()));
$viewName = $this->handleAction($filterChain, $actionInstance);
$timer->addTime();
$timer = sfTimerManager::getTimer(sprintf('View "%s" for "%s/%s"', $viewName, $actionInstance->getModuleName(), $actionInstance->getActionName()));
$this->handleView($filterChain, $actionInstance, $viewName);
$timer->addTime();
}
else
{
$viewName = $this->handleAction($filterChain, $actionInstance);
$this->handleView($filterChain, $actionInstance, $viewName);
}
}
/**
* Handles the action.
*
* @param sfFilterChain $filterChain The current filter chain
* @param sfAction $actionInstance An sfAction instance
*
* @return string The view type
*/
protected function handleAction($filterChain, $actionInstance)
{
if (sfConfig::get('sf_cache'))
{
$uri = $this->context->getViewCacheManager()->getCurrentCacheKey();
if (null !== $uri && $this->context->getViewCacheManager()->hasActionCache($uri))
{
// action in cache, so go to the view
return sfView::SUCCESS;
}
}
return $this->executeAction($actionInstance);
}
/**
* Executes the execute method of an action.
*
* @param sfAction $actionInstance An sfAction instance
*
* @return string The view type
*/
protected function executeAction($actionInstance)
{
// execute the action
$actionInstance->preExecute();
$viewName = $this->context->getService('sf_parameter_resolver')
->setRequest($this->context->getRequest())
->setComponent($actionInstance)
->execute();
$actionInstance->postExecute();
return null === $viewName ? sfView::SUCCESS : $viewName;
}
/**
* Handles the view.
*
* @param sfFilterChain $filterChain The current filter chain
* @param sfAction $actionInstance An sfAction instance
* @param string $viewName The view name
*/
protected function handleView($filterChain, $actionInstance, $viewName)
{
switch ($viewName)
{
case sfView::HEADER_ONLY:
$this->context->getResponse()->setHeaderOnly(true);
return;
case sfView::NONE:
return;
}
$this->executeView($actionInstance->getModuleName(), $actionInstance->getActionName(), $viewName, $actionInstance->getVarHolder()->getAll());
}
/**
* Executes and renders the view.
*
* The behavior of this method depends on the controller render mode:
*
* - sfView::NONE: Nothing happens.
* - sfView::RENDER_CLIENT: View data populates the response content.
* - sfView::RENDER_VAR: View data populates the data presentation variable.
*
* @param string $moduleName The module name
* @param string $actionName The action name
* @param string $viewName The view name
* @param array $viewAttributes An array of view attributes
*
* @return string The view data
*/
protected function executeView($moduleName, $actionName, $viewName, $viewAttributes)
{
$controller = $this->context->getController();
// get the view instance
$view = $controller->getView($moduleName, $actionName, $viewName);
// execute the view
$view->execute();
// pass attributes to the view
$view->getAttributeHolder()->add($viewAttributes);
// render the view
switch ($controller->getRenderMode())
{
case sfView::RENDER_NONE:
break;
case sfView::RENDER_CLIENT:
$viewData = $view->render();
$this->context->getResponse()->setContent($viewData);
break;
case sfView::RENDER_VAR:
$viewData = $view->render();
$controller->getActionStack()->getLastEntry()->setPresentation($viewData);
break;
}
}
}