[~TAKS] Extbase (MVC): FrontendRequestHandler now retrieves the current cObject throu...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Web / RequestBuilder.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Builds a web request.
30 *
31 * @package Extbase
32 * @subpackage MVC\Web
33 * @version $ID:$
34 *
35 * @scope prototype
36 */
37 class Tx_Extbase_MVC_Web_RequestBuilder implements t3lib_Singleton {
38 /**
39 * @var Tx_Extbase_Object_ObjectManagerInterface
40 */
41 protected $objectManager;
42
43 /**
44 * This is a unique key for a plugin (not the extension key!)
45 *
46 * @var string
47 */
48 protected $pluginName = 'plugin';
49
50 /**
51 * The name of the extension (in UpperCamelCase)
52 *
53 * @var string
54 */
55 protected $extensionName = 'Extbase';
56
57 /**
58 * The default controller name
59 *
60 * @var string
61 */
62 protected $defaultControllerName = 'Standard';
63
64 /**
65 * The default action of the default controller
66 *
67 * @var string
68 */
69 protected $defaultActionName = 'index';
70
71 /**
72 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
73 *
74 * @var array
75 */
76 protected $allowedControllerActions;
77
78 /**
79 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
80 */
81 protected $configurationManager;
82
83 /**
84 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
85 */
86 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
87 $this->configurationManager = $configurationManager;
88 }
89
90 /**
91 * Injects the object manager
92 *
93 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
94 * @return void
95 */
96 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
97 $this->objectManager = $objectManager;
98 }
99
100 /**
101 * @return void
102 */
103 protected function loadDefaultValues() {
104 $configuration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
105 if (!empty($configuration['pluginName'])) {
106 $this->pluginName = $configuration['pluginName'];
107 }
108 if (!empty($configuration['extensionName'])) {
109 $this->extensionName = $configuration['extensionName'];
110 }
111 if (!empty($configuration['controller'])) {
112 $this->defaultControllerName = $configuration['controller'];
113 } elseif (is_array($configuration['switchableControllerActions'])) {
114 $firstControllerActions = current($configuration['switchableControllerActions']);
115 $this->defaultControllerName = $firstControllerActions['controller'];
116 }
117 if (!empty($configuration['action'])) {
118 $this->defaultActionName = $configuration['action'];
119 } elseif (is_array($configuration['switchableControllerActions'])) {
120 $firstControllerActions = current($configuration['switchableControllerActions']);
121 $this->defaultActionName = array_shift(t3lib_div::trimExplode(',', $firstControllerActions['actions'], TRUE));
122 }
123 $allowedControllerActions = array();
124 if (is_array($configuration['switchableControllerActions'])) {
125 foreach ($configuration['switchableControllerActions'] as $controller => $controllerConfiguration) {
126 $controllerActions = t3lib_div::trimExplode(',', $controllerConfiguration['actions'], TRUE);
127 foreach ($controllerActions as $actionName) {
128 $allowedControllerActions[$controller][] = $actionName;
129 }
130 }
131 }
132 $this->allowedControllerActions = $allowedControllerActions;
133 }
134
135 /**
136 * Builds a web request object from the raw HTTP information and the configuration
137 *
138 * @return Tx_Extbase_MVC_Web_Request The web request as an object
139 */
140 public function build() {
141 $this->loadDefaultValues();
142 $pluginNamespace = Tx_Extbase_Utility_Extension::getPluginNamespace($this->extensionName, $this->pluginName);
143 $parameters = t3lib_div::_GPmerged($pluginNamespace);
144
145 if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
146 $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
147 } else {
148 $controllerName = $this->defaultControllerName;
149 }
150 $allowedActions = $this->allowedControllerActions[$controllerName];
151 if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
152 $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
153 } else {
154 $actionName = $this->defaultActionName;
155 }
156
157 $request = $this->objectManager->create('Tx_Extbase_MVC_Web_Request');
158 $request->setPluginName($this->pluginName);
159 $request->setControllerExtensionName($this->extensionName);
160 $request->setControllerName($controllerName);
161 $request->setControllerActionName($actionName);
162 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
163 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
164 $request->setMethod((isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : NULL);
165
166 if (is_string($parameters['format']) && (strlen($parameters['format']))) {
167 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
168 }
169
170 foreach ($parameters as $argumentName => $argumentValue) {
171 $request->setArgument($argumentName, $argumentValue);
172 }
173
174 return $request;
175 }
176
177
178 }
179 ?>