From 7c1dae9b5dc4ce18bf9837eba472c9118705a200 Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Mon, 30 Aug 2021 21:54:28 +0200 Subject: [PATCH] [FEATURE] Add option to set custom http return status --- .../Controller/AbstractCommandController.php | 164 ++++++++++++++++++ Classes/Controller/AbstractEIDController.php | 19 +- Classes/Traits/DependencyInjectionTrait.php | 154 ++++++++++++++++ 3 files changed, 334 insertions(+), 3 deletions(-) create mode 100644 Classes/Controller/AbstractCommandController.php create mode 100644 Classes/Traits/DependencyInjectionTrait.php diff --git a/Classes/Controller/AbstractCommandController.php b/Classes/Controller/AbstractCommandController.php new file mode 100644 index 0000000..4508a9e --- /dev/null +++ b/Classes/Controller/AbstractCommandController.php @@ -0,0 +1,164 @@ + + * + ***/ + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use TYPO3\CMS\Core\Cache\CacheManager; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer; +use TYPO3\CMS\Core\Log\LogManager; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; +use TYPO3\CMS\Extbase\Object\ObjectManager; +use TYPO3\CMS\Frontend\Plugin\AbstractPlugin; +use TYPO3\CMS\Frontend\Utility\EidUtility; +use TYPO3\CMS\Extbase\Reflection\ClassSchema; +use TYPO3\CMS\Extbase\Reflection\ReflectionService; +use Symfony\Component\Console\Command\Command; + +/** + * AbstractEIDController + */ +class AbstractCommandController extends Commands +{ + + /** + * @var BackendConfigurationManager + */ + protected $configurationManager; + + /** + * ApiUtility + */ + protected $apiUtility = null; + + /* + * extension Key + */ + protected $extensionKey = null; + + /* + * objectManager + */ + protected $objectManager = null; + + /* + * storagePids + */ + protected $settings = null; + + /* + * storagePids + */ + protected $storagePids = []; + + /* + * logManager + */ + protected $logManager = null; + + /* + * logger + */ + protected $importLogger = null; + + + /* + * returns the extensionkey set in the exended calss + */ + public function getExtensionKey() { + return $this->extensionKey; + } + + /** + * Construct + * + * @param ObjectManager $objectManager + * @param array $configuration + */ + public function __construct( + ObjectManager $objectManager = null, + array $configuration = [] + ) { + $this->objectManager = GeneralUtility::makeInstance( + ObjectManager::class + ); + $this->initFrontendController(); + $this->configurationManager = $this->objectManager->get( + ConfigurationManagerInterface::class + ); + $this->apiUtility = $this->objectManager->get( + \Cjel\TemplatesAide\Utility\ApiUtility::class + ); + $this->configurationManager->setConfiguration(array()); + $frameworkConfiguration = $this->configurationManager->getConfiguration( + ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, + $this->getExtensionKey() + ); + $this->settings = $frameworkConfiguration; + $this->storagePids = explode( + ',', + str_replace( + ' ', + '', + $frameworkConfiguration['persistence']['storagePid'] + ) + ); + $this->logManager = $this->objectManager->get( + LogManager::Class + ); + $this->importLogger = $this->logManager->getLogger( + 'importLogger' + ); + $this->reflectionService = GeneralUtility::makeInstance( + ReflectionService::class, GeneralUtility::makeInstance( + CacheManager::class + ) + ); + $classInfo = $this->reflectionService->getClassSchema( + get_class($this) + ); + foreach ($classInfo->getInjectMethods() as $method => $className) { + $class = $this->objectManager->get( + $className + ); + $this->{$method}($class); + } + parent::__construct($name); + } + + /** + * Initialize frontentController + */ + private function initFrontendController() + { + $currentDomain = strtok(GeneralUtility::getIndpEnv('HTTP_HOST'), ':'); + $frontendController = GeneralUtility::makeInstance( + \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, + $GLOBALS['TYPO3_CONF_VARS'], + null, + 0, + true + ); + $GLOBALS['TSFE'] = $frontendController; + $frontendController->connectToDB(); + $frontendController->fe_user = EidUtility::initFeUser(); + $frontendController->id = $result[0]['pid']; + $frontendController->determineId(); + $frontendController->initTemplate(); + $frontendController->getConfigArray(); + EidUtility::initTCA(); + } + +} diff --git a/Classes/Controller/AbstractEIDController.php b/Classes/Controller/AbstractEIDController.php index 5ae0ffc..e874211 100644 --- a/Classes/Controller/AbstractEIDController.php +++ b/Classes/Controller/AbstractEIDController.php @@ -191,9 +191,10 @@ class AbstractEIDController * @param ResponseInterface $response * @return void */ - public function processRequest(ServerRequestInterface $request, - ResponseInterface $response = null) - { + public function processRequest( + ServerRequestInterface $request, + ResponseInterface $response = null + ) { $apiObject = explode('/', $request->getUri()->getPath())[3]; $apiObjectId = explode('/', $request->getUri()->getPath())[4]; if (!$apiObject) { @@ -221,6 +222,18 @@ class AbstractEIDController ) { $response = $response->withStatus(400); } + if (is_array($responseData) + && array_key_exists('status', $responseData) + ) { + if (is_array($responseData['status'])) { + $response = $response->withStatus( + $responseData['status'][0], + $responseData['status'][1] + ); + } else { + $response = $response->withStatus($responseData['status']); + } + } $response->getBody()->write(\json_encode($responseData)); return $response; } else { diff --git a/Classes/Traits/DependencyInjectionTrait.php b/Classes/Traits/DependencyInjectionTrait.php new file mode 100644 index 0000000..a9ad822 --- /dev/null +++ b/Classes/Traits/DependencyInjectionTrait.php @@ -0,0 +1,154 @@ + + * + ***/ + +use Cjel\TemplatesAide\Utility\ApiUtility; +use TYPO3\CMS\Core\Cache\CacheManager; +use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; +use TYPO3\CMS\Extbase\Object\ObjectManager; +use TYPO3\CMS\Extbase\Reflection\ClassSchema; +use TYPO3\CMS\Extbase\Reflection\ReflectionService; +use TYPO3\CMS\Frontend\Utility\EidUtility; + +/** + * ValidationTrait + */ +trait DependencyInjectionTrait +{ + /* + * extension Key + */ + protected $extensionKey = null; + + /* + * storagePids + */ + protected $storagePids = []; + + /* + * objectManager + */ + protected $objectManager = null; + + /** + * @var BackendConfigurationManager + */ + protected $configurationManager; + + /** + * ApiUtility + */ + protected $apiUtility = null; + + /* + * storagePids + */ + protected $settings = null; + + /* + * logManager + */ + protected $logManager = null; + + /* + * logger + */ + protected $importLogger = null; + + + /* + * returns the extensionkey set in the exended calss + * + * @return string + */ + public function getExtensionKey() { + return $this->extensionKey; + } + + + /** + * Loads config and sets up extbase like dependecny injection + * + * @return void + */ + public function setupDependencyInjection() { + $this->objectManager = GeneralUtility::makeInstance( + ObjectManager::class + ); + $this->initFrontendController(); + $this->configurationManager = $this->objectManager->get( + ConfigurationManagerInterface::class + ); + $this->configurationManager->setConfiguration( + array() + ); + $this->apiUtility = $this->objectManager->get( + ApiUtility::class + ); + $frameworkConfiguration = $this->configurationManager->getConfiguration( + ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK, + $this->getExtensionKey() + ); + $this->configurationManager->setConfiguration($frameworkConfiguration); + $this->settings = $frameworkConfiguration; + $this->storagePids = explode( + ',', + str_replace( + ' ', + '', + $frameworkConfiguration['persistence']['storagePid'] + ) + ); + $this->reflectionService = GeneralUtility::makeInstance( + ReflectionService::class, GeneralUtility::makeInstance( + CacheManager::class + ) + ); + $classInfo = $this->reflectionService->getClassSchema( + get_class($this) + ); + foreach ($classInfo->getInjectMethods() as $method => $className) { + $class = $this->objectManager->get( + $className + ); + $this->{$method}($class); + } + } + + /** + * Initialize frontentController + * + * @return void + */ + private function initFrontendController() + { + $currentDomain = strtok(GeneralUtility::getIndpEnv('HTTP_HOST'), ':'); + $frontendController = GeneralUtility::makeInstance( + \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, + $GLOBALS['TYPO3_CONF_VARS'], + null, + 0, + true + ); + $GLOBALS['TSFE'] = $frontendController; + $frontendController->connectToDB(); + $frontendController->fe_user = EidUtility::initFeUser(); + $frontendController->id = $result[0]['pid']; + $frontendController->determineId(); + $frontendController->initTemplate(); + $frontendController->getConfigArray(); + EidUtility::initTCA(); + } + +}