07ddb31def03cfdcc55874abc19f2a69dc47a056
[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 {
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 public function initialize($configuration) {
79 if (!empty($configuration['pluginName'])) {
80 $this->pluginName = $configuration['pluginName'];
81 }
82 if (!empty($configuration['extensionName'])) {
83 $this->extensionName = $configuration['extensionName'];
84 }
85 if (!empty($configuration['controller'])) {
86 $this->defaultControllerName = $configuration['controller'];
87 } elseif (is_array($configuration['switchableControllerActions'])) {
88 $firstControllerActions = current($configuration['switchableControllerActions']);
89 $this->defaultControllerName = $firstControllerActions['controller'];
90 }
91 if (!empty($configuration['action'])) {
92 $this->defaultActionName = $configuration['action'];
93 } elseif (is_array($configuration['switchableControllerActions'])) {
94 $firstControllerActions = current($configuration['switchableControllerActions']);
95 $this->defaultActionName = array_shift(t3lib_div::trimExplode(',', $firstControllerActions['actions'], TRUE));
96 }
97 $allowedControllerActions = array();
98 if (is_array($configuration['switchableControllerActions'])) {
99 foreach ($configuration['switchableControllerActions'] as $controller => $controllerConfiguration) {
100 $controllerActions = t3lib_div::trimExplode(',', $controllerConfiguration['actions'], TRUE);
101 foreach ($controllerActions as $actionName) {
102 $allowedControllerActions[$controller][] = $actionName;
103 }
104 }
105 }
106 $this->allowedControllerActions = $allowedControllerActions;
107 }
108
109 /**
110 * Injects the object manager
111 *
112 * @param Tx_Extbase_Object_ObjectManagerInterface $objectManager
113 * @return void
114 */
115 public function injectObjectManager(Tx_Extbase_Object_ObjectManagerInterface $objectManager) {
116 $this->objectManager = $objectManager;
117 }
118
119 /**
120 * Builds a web request object from the raw HTTP information and the configuration
121 *
122 * @return Tx_Extbase_MVC_Web_Request The web request as an object
123 */
124 public function build() {
125 $pluginSignature = strtolower($this->extensionName . '_' . $this->pluginName);
126 $pluginNamespace = Tx_Extbase_Utility_Extension::getPluginNamespaceByPluginSignature($pluginSignature);
127 $parameters = t3lib_div::_GPmerged($pluginNamespace);
128
129 if (is_string($parameters['controller']) && array_key_exists($parameters['controller'], $this->allowedControllerActions)) {
130 $controllerName = filter_var($parameters['controller'], FILTER_SANITIZE_STRING);
131 $allowedActions = $this->allowedControllerActions[$controllerName];
132 if (is_string($parameters['action']) && is_array($allowedActions) && in_array($parameters['action'], $allowedActions)) {
133 $actionName = filter_var($parameters['action'], FILTER_SANITIZE_STRING);
134 } else {
135 $actionName = $this->defaultActionName;
136 }
137 } else {
138 $controllerName = $this->defaultControllerName;
139 $actionName = $this->defaultActionName;
140 }
141
142 $request = $this->objectManager->create('Tx_Extbase_MVC_Web_Request');
143 $request->setPluginName($this->pluginName);
144 $request->setControllerExtensionName($this->extensionName);
145 $request->setControllerName($controllerName);
146 $request->setControllerActionName($actionName);
147 $request->setRequestURI(t3lib_div::getIndpEnv('TYPO3_REQUEST_URL'));
148 $request->setBaseURI(t3lib_div::getIndpEnv('TYPO3_SITE_URL'));
149 $request->setMethod((isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : NULL);
150
151 if (is_string($parameters['format']) && (strlen($parameters['format']))) {
152 $request->setFormat(filter_var($parameters['format'], FILTER_SANITIZE_STRING));
153 }
154
155 foreach ($parameters as $argumentName => $argumentValue) {
156 $request->setArgument($argumentName, $argumentValue);
157 }
158
159 return $request;
160 }
161
162
163 }
164 ?>