<?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; 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 = ''; /** * Vendor Name * * @var string */ protected $vendorName = 'SGalinski'; /** * ExtBase Bootstrap Instance * * @var \TYPO3\CMS\Extbase\Core\Bootstrap */ protected $bootStrap; /** * Object Manager * * @var \TYPO3\CMS\Extbase\Object\ObjectManager */ protected $objectManager; /** * @var string */ protected $format = 'json'; /** * Initializes the instance */ public function __construct() { $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class); /** @var $bootStrap Bootstrap */ $bootStrap = $this->objectManager->get(Bootstrap::class); $this->injectBootStrap($bootStrap); } /** * 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); } // 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; } } ?>