Newer
Older
<?php
namespace SGalinski\SgRest\Connector;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (http://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Core\Bootstrap;

Markus Guenther
committed
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Service\ExtensionService;
/**
* Utility class to simplify the execution of extbase actions from external sources (e.g. from Ext.Direct)
*/
class ExtBaseConnector implements SingletonInterface {
/**
* Extension Key
*
* @var string
*/
protected $extensionKey = '';
/**
* Module Key
*
* @var string
*/
protected $moduleOrPluginKey = '';

Markus Guenther
committed
/**
* Vendor Name
*
* @var string
*/
protected $vendorName = 'SGalinski';
/**
* ExtBase Bootstrap Instance
*
* @var \TYPO3\CMS\Extbase\Core\Bootstrap
*/

Markus Guenther
committed
protected $bootStrap;
/**
* Object Manager
*
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/

Markus Guenther
committed
protected $objectManager;
protected $format = 'json';
/**
* Initializes the instance
*/
public function __construct() {

Markus Guenther
committed
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);

Markus Guenther
committed
$bootStrap = $this->objectManager->get(Bootstrap::class);
$this->injectBootStrap($bootStrap);
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}
/**
* Initialize the bootstrap
*
* @param Bootstrap $bootStrap
* @return void
*/
public function injectBootStrap(Bootstrap $bootStrap) {
$this->bootStrap = $bootStrap;
}
/**
* Setter for the extension key
*
* @param string $extensionKey
* @return void
*/
public function setExtensionKey($extensionKey) {
$this->extensionKey = $extensionKey;
}
/**
* Getter for the extension key
*
* @return string
*/
public function getExtensionKey() {
return $this->extensionKey;
}
/**
* Setter for the module or plugin key
*
* @param string $moduleOrPluginKey
* @return void
*/
public function setModuleOrPluginKey($moduleOrPluginKey) {
$this->moduleOrPluginKey = $moduleOrPluginKey;
}
/**
* Getter for the module or plugin key
*
* @return string
*/
public function getModuleOrPluginKey() {
return $this->moduleOrPluginKey;
}
/**
* @return string
*/
public function getFormat() {
return $this->format;
}
/**
* @param string $format
* @return void
*/
public function setFormat($format) {
$this->format = $format;
}
/**
* Sets the parameters for the configured module/plugin
*
* @param array $parameters
* @return void
*/
public function setParameters(array $parameters) {
/** @var $extensionService ExtensionService */
$extensionService = $this->objectManager->get('TYPO3\CMS\Extbase\Service\ExtensionService');
$parameterNamespace = $extensionService->getPluginNamespace(
$this->extensionKey,
$this->moduleOrPluginKey
);
if (count($_POST[$parameterNamespace])) {
ArrayUtility::mergeRecursiveWithOverrule($_POST[$parameterNamespace], $parameters);
} else {
$_POST[$parameterNamespace] = $parameters;
}
}
/**
* Runs the given ExtBase configuration and returns the result. We need to passthru the apiKey. The key is need in
* the permission checks in the specific rest controller.
*
* @param string $controller
* @param string $action
* @param string $vendorName
* @param string $apiKey
* @throws \Exception
* @return array
*/
public function runControllerAction($controller, $action, $vendorName, $apiKey) {
if ($controller === '' || $action === '' || $vendorName === '') {
throw new \InvalidArgumentException('Invalid Controller/Action Combination!');
}
if ($controller === '' || $action === '' || $vendorName === '') {
throw new \Exception('You tried to access an object where you do not have the necessary permissions.', 403);
}

Markus Guenther
committed
// Pass thu the apiKey
$this->setParameters(['apiKey' => $apiKey]);
$response = $this->bootStrap->run(
'', [
'extensionName' => $this->extensionKey,
'pluginName' => $this->moduleOrPluginKey,
'vendorName' => $vendorName,
'switchableControllerActions' => [
$controller => [$action]
],
'format' => $this->format,
]
);
return $response;
}
}
?>