[!!!][~TASK] Extbase (Configuration): Major rework of the ConfigurationManager
[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 /**
40 * @var Tx_Extbase_Object_ObjectManagerInterface
41 */
42 protected $objectManager;
43
44 /**
45 * This is a unique key for a plugin (not the extension key!)
46 *
47 * @var string
48 */
49 protected $pluginName = 'plugin';
50
51 /**
52 * The name of the extension (in UpperCamelCase)
53 *
54 * @var string
55 */
56 protected $extensionName;
57
58 /**
59 * The default controller name
60 *
61 * @var string
62 */
63 protected $defaultControllerName;
64
65 /**
66 * The default action of the default controller
67 *
68 * @var string
69 */
70 protected $defaultActionName;
71
72 /**
73 * The allowed actions of the controller. This actions can be called via $_GET and $_POST.
74 *
75 * @var array
76 */
77 protected $allowedControllerActions = array();
78
79 /**
80 * @var Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
81 */
82 protected $configurationManager;
83
84 /**
85 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
86 */
87 public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
88 $this->configurationManager = $configurationManager;
89 }
90
91 /**
92 * Injects the object manager
93 *
94 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
95 * @return void
96 */
97 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
98 $this->objectManager = $objectManager;
99 }
100
101 /**
102 * @return void
103 */
104 protected function loadDefaultValues() {
105 $configuration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
106 if (empty($configuration['extensionName'])) {
107 throw new Tx_Extbase_MVC_Exception('"extensionName" is not properly configured. Request can\'t be dispatched!', 1289843275);
108 }
109 if (empty($configuration['pluginName'])) {
110 throw new Tx_Extbase_MVC_Exception('"pluginName" is not properly configured. Request can\'t be dispatched!', 1289843277);
111 }
112 $this->extensionName = $configuration['extensionName'];
113 $this->pluginName = $configuration['pluginName'];
114
115 $frameworkConfiguration = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
116 $controllerConfiguration = $frameworkConfiguration['controllerConfiguration'];
117 $this->defaultControllerName = current(array_keys($controllerConfiguration));
118 $this->defaultActionName = current($controllerConfiguration[$this->defaultControllerName]['actions']);
119
120 $allowedControllerActions = array();
121 foreach ($controllerConfiguration as $controllerName => $controllerActions) {
122 $allowedControllerActions[$controllerName] = $controllerActions['actions'];
123 }
124 $this->allowedControllerActions = $allowedControllerActions;
125 }
126
127 /**
128 * Builds a web request object from the raw HTTP information and the configuration
129 *
130 * @return Tx_Extbase_MVC_Web_Request The web request as an object
131 */
132 public function build() {
133 $this->loadDefaultValues();
134 $pluginNamespace = Tx_Extbase_Utility_Extension::getPluginNamespace($this->extensionName, $this->pluginName);
135 $parameters = t3lib_div::_GPmerged($pluginNamespace);
136
137 if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
138 $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
139 } else {
140 $controllerName = $this->defaultControllerName;
141 }
142 $allowedActions = $this->allowedControllerActions[$controllerName];
143 if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
144 $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
145 } else {
146 $actionName = $this->defaultActionName;
147 }
148
149 $request = $this->objectManager->create('Tx_Extbase_MVC_Web_Request');
150 $request->setPluginName($this->pluginName);
151 $request->setControllerExtensionName($this->extensionName);
152 $request->setControllerName($controllerName);
153 $request->setControllerActionName($actionName);
154 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
155 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
156 $request->setMethod((isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : NULL);
157
158 if (is_string($parameters['format']) && (strlen($parameters['format']))) {
159 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
160 }
161
162 foreach ($parameters as $argumentName => $argumentValue) {
163 $request->setArgument($argumentName, $argumentValue);
164 }
165
166 return $request;
167 }
168
169
170 }
171 ?>