diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php index e808d4178481c3de63835bc4267d51b08b81c72f..5554779f3c53d4ca0ad9840109fd39bc6fdc47b1 100644 --- a/Classes/Service/MailTemplateService.php +++ b/Classes/Service/MailTemplateService.php @@ -798,7 +798,6 @@ class MailTemplateService { // @TODO remove this deprecated function in version 5.0.0 self::logDeprecation('registerExtensions'); - $registerService = GeneralUtility::makeInstance(RegisterService::class); $extensionList = ExtensionManagementUtility::getLoadedExtensionListArray(); foreach ($extensionList as $extensionName) { @@ -813,7 +812,7 @@ class MailTemplateService { if (!\file_exists($pathToRegistrationFile)) { continue; } - $registerService->register($pathToRegistrationFile); + $GLOBALS['sgmail']['sg_mail'][$configFile] = $pathToRegistrationFile; } } } diff --git a/Classes/Service/RegisterService.php b/Classes/Service/RegisterService.php index aa80fdb091968968806916116608819d22efc596..e4b8831cc6176858fa74040002a2ce7e5f48d4f1 100644 --- a/Classes/Service/RegisterService.php +++ b/Classes/Service/RegisterService.php @@ -27,19 +27,17 @@ namespace SGalinski\SgMail\Service; ***************************************************************/ use TYPO3\CMS\Core\Cache\CacheManager; -use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; /** * Provides an api for registering your mail templates - * - * @package SGalinski\SgMail\Service */ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { const CACHE_NAME = 'sg_mail_registerArrayCache'; const CACHE_LIFETIME_IN_SECONDS = 86400; const DEFAULT_TEMPLATE_PATH = 'Resources/Private/Templates/SgMail/'; + const CONFIG_PATH = 'Configuration/MailTemplates/Forms'; /** * contains the actual registration data @@ -62,7 +60,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { * @api */ public function register($pathToRegisterFile) { - if (file_exists($pathToRegisterFile)) { + if (\file_exists($pathToRegisterFile)) { $this->registrationFiles[] = [ 'path' => $pathToRegisterFile ]; @@ -78,13 +76,12 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { * @throws \InvalidArgumentException */ public function getRegisterArray(): array { - /** @var CacheManager $cacheManager */ $cacheManager = GeneralUtility::makeInstance(CacheManager::class); - /** @var FrontendInterface $cache */ $cache = $cacheManager->getCache(self::CACHE_NAME); - $cacheId = md5('sg_mail'); - /** @var array entry */ - if (($entry = $cache->get($cacheId)) === FALSE) { + $cacheId = \md5('sg_mail'); + + $entry = $cache->get($cacheId); + if (!$entry) { $entry = $this->registerExtensions(); if ($entry === NULL) { @@ -99,15 +96,51 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { /** * Read every registered file and create a registration entry in the registerArray if possible + * * @return array */ - public function registerExtensions(): array { - // clear registerArray + private function registerExtensions(): array { $this->registerArray = []; - foreach ($this->registrationFiles as $registerFile) { - if (\is_file($registerFile['path'])) { - $configArray = (include $registerFile['path']); + // @TODO remove in version 5.0.0 + MailTemplateService::registerExtensions(); + + foreach ($GLOBALS['sgmail'] as $extension) { + foreach ($extension as $key => $registerFile) { + if (\is_file($registerFile)) { + $configArray = (include $registerFile); + + $extensionKey = $configArray['extension_key']; + $templateKey = $configArray['template_key']; + + if ($extensionKey === NULL || $templateKey === NULL) { + continue; + } + + $this->writeRegisterArrayEntry( + $extensionKey, $templateKey, $configArray + ); + } + } + } + + // @TODO read automatically created extension registrations and write the entries + // get the location where automatic registrations should be stored + $configurationLocation = $this->getRegistrationPath(); + $registerFolder = GeneralUtility::getFileAbsFileName( + $configurationLocation + ); + if (\is_dir($registerFolder)) { + $configFiles = GeneralUtility::getFilesInDir($registerFolder); + foreach ($configFiles as $configFile) { + $pathToRegistrationFile = $registerFolder . '/' . $configFile; + + if (!\file_exists($pathToRegistrationFile)) { + continue; + } + + // @TODO SECURITY CHECK WITH HASH + $configArray = (include $pathToRegistrationFile); $extensionKey = $configArray['extension_key']; $templateKey = $configArray['template_key']; @@ -128,8 +161,8 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { /** * write a single entry into the register array * - * @param $extensionKey - * @param $templateKey + * @param string $extensionKey + * @param string $templateKey * @param array $configArray * @param bool $transformTemplateFolder * @param string $storeTemplateExtension @@ -187,5 +220,20 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { public function getRegistrationFiles(): array { return $this->registrationFiles; } + + /** + * Returns the path to the configured location where automatic mail template registrations should be + * + * @return string + */ + private function getRegistrationPath(): string { + // get typoscript settings from sg mail + /** @var TypoScriptSettingsService $typoScriptSettingsService */ + $typoScriptSettingsService = GeneralUtility::makeInstance(TypoScriptSettingsService::class); + $tsSettings = $typoScriptSettingsService->getSettings(0, 'tx_sgmail'); + + // get the location where automatic registrations should be stored + return 'EXT:' . $tsSettings['mail']['configurationLocation'] . '/' . RegisterService::CONFIG_PATH; + } } diff --git a/Classes/XClass/Form/FormEditorController.php b/Classes/XClass/Form/FormEditorController.php index 5331d901d8fe7a3e291a0be63c5e9c667175c682..e3a7791dfaf8eac819e950ffe928f16ed6d7697e 100644 --- a/Classes/XClass/Form/FormEditorController.php +++ b/Classes/XClass/Form/FormEditorController.php @@ -136,13 +136,12 @@ class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorControll } // call register function in register service class - $registrationService = GeneralUtility::makeInstance(RegisterService::class); + $registrationService = $this->objectManager->get(RegisterService::class); + // write the new Register.php file and add it to the registrationFiles array in the RegisterService - $registerFilePath = $this->writeRegisterFile($renderables, $extensionKey, $templateKey); - $registrationService->register($registerFilePath); - $registrationService->registerExtensions(); + $this->writeRegisterFile($renderables, $extensionKey, $templateKey); + $registrationService->getRegisterArray(); - // clear caches $this->clearCaches(); } @@ -161,7 +160,7 @@ class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorControll $registerFolder = GeneralUtility::getFileAbsFileName( $configurationLocation ); - // create folder + GeneralUtility::mkdir($registerFolder); $hashPrefix = md5($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] . '|' . $templateKey . '.php'); @@ -226,6 +225,6 @@ class FormEditorController extends \TYPO3\CMS\Form\Controller\FormEditorControll $tsSettings = $typoScriptSettingsService->getSettings(0, 'tx_sgmail'); // get the location where automatic registrations should be stored - return 'EXT:' . $tsSettings['mail']['configurationLocation'] . '/' . MailTemplateService::CONFIG_PATH; + return 'EXT:' . $tsSettings['mail']['configurationLocation'] . '/' . RegisterService::CONFIG_PATH; } }