Skip to content
Snippets Groups Projects
Commit eac26e8e authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] Refactored the registration process

parent 9a80818c
No related branches found
No related tags found
1 merge request!11Feature security update
...@@ -776,25 +776,17 @@ class MailTemplateService { ...@@ -776,25 +776,17 @@ class MailTemplateService {
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \BadFunctionCallException * @throws \BadFunctionCallException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @deprecated since 4.7.0 will be removed in 5.0.0. Use the RegistrationService class instead
*/ */
public static function getRegisterArray() { public static function getRegisterArray(): array {
/** @var CacheManager $cacheManager */ // @TODO remove this deprecated function in version 5.0.0
$cacheManager = GeneralUtility::makeInstance(CacheManager::class); self::logDeprecation('registerExtensions');
/** @var FrontendInterface $cache */
$cache = $cacheManager->getCache(self::CACHE_NAME);
$cacheId = md5('sg_mail');
/** @var array entry */
if (($entry = $cache->get($cacheId)) === FALSE) {
$entry = self::registerExtensions();
if ($entry === NULL) {
$entry = [];
}
$cache->set($cacheId, $entry, [], self::CACHE_LIFETIME_IN_SECONDS); $registrationService = GeneralUtility::makeInstance(RegisterService::class);
} // write entries for old extensions intto the file list
self::registerExtensions();
return $entry; return $registrationService->getRegisterArray();
} }
/** /**
...@@ -802,16 +794,13 @@ class MailTemplateService { ...@@ -802,16 +794,13 @@ class MailTemplateService {
* If found, register the template(s) * If found, register the template(s)
* *
* @throws \BadFunctionCallException * @throws \BadFunctionCallException
* @deprecated since 4.7.0 will be removed in 5.0.0 * @deprecated since 4.7.0 will be removed in 5.0.0. Use the RegistrationService class instead
* @return array
*/ */
public static function registerExtensions(): array { public static function registerExtensions() {
// @TODO remove this deprecated function in version 5.0.0 // @TODO remove this deprecated function in version 5.0.0
self::logDeprecation('registerExtensions'); self::logDeprecation('registerExtensions');
// clear registerArray $registerService = GeneralUtility::makeInstance(RegisterService::class);
$registerArray = [];
$extensionList = ExtensionManagementUtility::getLoadedExtensionListArray(); $extensionList = ExtensionManagementUtility::getLoadedExtensionListArray();
foreach ($extensionList as $extensionName) { foreach ($extensionList as $extensionName) {
...@@ -820,90 +809,16 @@ class MailTemplateService { ...@@ -820,90 +809,16 @@ class MailTemplateService {
if (\is_dir($extensionConfigDirectory)) { if (\is_dir($extensionConfigDirectory)) {
$configFiles = GeneralUtility::getFilesInDir($extensionConfigDirectory); $configFiles = GeneralUtility::getFilesInDir($extensionConfigDirectory);
foreach ($configFiles as $configFile) { foreach ($configFiles as $configFile) {
if (!\file_exists($extensionConfigDirectory . '/' . $configFile)) { $pathToRegistrationFile = $extensionConfigDirectory . '/' . $configFile;
continue;
}
$configArray = (include $extensionConfigDirectory . '/' . $configFile); if (!\file_exists($pathToRegistrationFile)) {
$extensionKey = $configArray['extension_key'];
$templateKey = $configArray['template_key'];
if ($extensionKey === NULL || $templateKey === NULL) {
continue; continue;
} }
$registerService->register($pathToRegistrationFile);
$registerArray = self::writeRegisterArrayEntry(
$registerArray, $extensionKey, $templateKey, $configArray
);
} }
} }
} }
return $registerArray;
}
/**
* writes a single entry into the register array
*
* @param array $registerArray
* @param string $extensionKey
* @param string $templateKey
* @param array $configArray
* @param bool $transformTemplateFolder
* @param string $storeTemplateExtension
* @deprecated since 4.7.0 will be removed in 5.0.0
* @return array
*/
private static function writeRegisterArrayEntry(
array $registerArray, $extensionKey, $templateKey, array $configArray,
$transformTemplateFolder = TRUE, $storeTemplateExtension = ''
) {
// @TODO remove this deprecated function in version 5.0.0
self::logDeprecation('writeRegisterArrayEntry');
// If it is not explicitly set in which extension the html should be located, use the extension set in the template settings
if ($storeTemplateExtension === '') {
$storeTemplateExtension = $extensionKey;
}
// give the option to use the template key as folder name. this is used mainly with auto registering
$templateDirectory = $templateKey;
// by default folders with underscore will be transformed to upper camelcase
if ($transformTemplateFolder) {
// transform template directory name: your_templates => YourTemplates/
$templateDirectoryParts = GeneralUtility::trimExplode('_', $templateKey);
$templateDirectory = '';
foreach ($templateDirectoryParts as $part) {
$templateDirectory .= ucfirst($part);
}
}
$templateDirectory .= '/';
$templatePath = ExtensionManagementUtility::extPath($storeTemplateExtension) . self::DEFAULT_TEMPLATE_PATH
. $templateDirectory;
if ($configArray['template_path']) {
$templatePath = $configArray[$templateKey];
}
$description = $configArray['description'];
$subject = $configArray['subject'];
$marker = $configArray['markers'];
$registerArray[$extensionKey][$templateKey] = [
'templatePath' => $templatePath,
'description' => $description,
'marker' => $marker,
'extension' => $extensionKey,
'templateName' => $templateKey,
'subject' => $subject
];
return $registerArray;
} }
/** /**
......
...@@ -32,20 +32,25 @@ use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; ...@@ -32,20 +32,25 @@ use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
/** /**
* Class RegisterService * Provides an api for registering your mail templates
* *
* @package SGalinski\SgMail\Service * @package SGalinski\SgMail\Service
*/ */
class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
const CACHE_NAME = 'sg_mail_registerArrayCache'; const CACHE_NAME = 'sg_mail_registerArrayCache';
const CACHE_LIFETIME_IN_SECONDS = 86400; const CACHE_LIFETIME_IN_SECONDS = 86400;
const DEFAULT_TEMPLATE_PATH = 'Resources/Private/Templates/SgMail/';
/** /**
* contains the actual registration data
*
* @var array * @var array
*/ */
public $registerArray = []; private $registerArray = [];
/** /**
* contains the paths to the registration files
*
* @var array * @var array
*/ */
private $registrationFiles = []; private $registrationFiles = [];
...@@ -54,6 +59,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -54,6 +59,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
* Enables extensions to add their registration files * Enables extensions to add their registration files
* *
* @param string $pathToRegisterFile * @param string $pathToRegisterFile
* @api
*/ */
public function register($pathToRegisterFile) { public function register($pathToRegisterFile) {
if (file_exists($pathToRegisterFile)) { if (file_exists($pathToRegisterFile)) {
...@@ -93,13 +99,14 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -93,13 +99,14 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
/** /**
* Read every registered file and create a registration entry in the registerArray if possible * Read every registered file and create a registration entry in the registerArray if possible
*
* @internal
*/ */
private function registerExtensions() { public function registerExtensions() {
foreach ($this->registrationFiles as $registerFile) { // clear registerArray
$this->registerArray = [];
// clear registerArray
$registerArray = [];
foreach ($this->registrationFiles as $registerFile) {
if (\is_file($registerFile)) { if (\is_file($registerFile)) {
$configArray = (include $registerFile); $configArray = (include $registerFile);
...@@ -110,31 +117,26 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -110,31 +117,26 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
continue; continue;
} }
$registerArray = $this->writeRegisterArrayEntry( $this->writeRegisterArrayEntry(
$registerArray, $extensionKey, $templateKey, $configArray $extensionKey, $templateKey, $configArray
); );
} }
// set the register array after every registration has been parsed
$this->registerArray = $registerArray;
} }
} }
/** /**
* write a single entry into the register array * write a single entry into the register array
* *
* @param array $registerArray
* @param $extensionKey * @param $extensionKey
* @param $templateKey * @param $templateKey
* @param array $configArray * @param array $configArray
* @param bool $transformTemplateFolder * @param bool $transformTemplateFolder
* @param string $storeTemplateExtension * @param string $storeTemplateExtension
* @return array
*/ */
private function writeRegisterArrayEntry( private function writeRegisterArrayEntry(
array $registerArray, $extensionKey, $templateKey, array $configArray, $extensionKey, $templateKey, array $configArray,
$transformTemplateFolder = TRUE, $storeTemplateExtension = '' $transformTemplateFolder = TRUE, $storeTemplateExtension = ''
): array { ) {
// If it is not explicitly set in which extension the html should be located, use the extension set in the template settings // If it is not explicitly set in which extension the html should be located, use the extension set in the template settings
if ($storeTemplateExtension === '') { if ($storeTemplateExtension === '') {
$storeTemplateExtension = $extensionKey; $storeTemplateExtension = $extensionKey;
...@@ -165,7 +167,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -165,7 +167,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
$subject = $configArray['subject']; $subject = $configArray['subject'];
$marker = $configArray['markers']; $marker = $configArray['markers'];
$registerArray[$extensionKey][$templateKey] = [ $this->registerArray[$extensionKey][$templateKey] = [
'templatePath' => $templatePath, 'templatePath' => $templatePath,
'description' => $description, 'description' => $description,
'marker' => $marker, 'marker' => $marker,
...@@ -173,8 +175,16 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface { ...@@ -173,8 +175,16 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
'templateName' => $templateKey, 'templateName' => $templateKey,
'subject' => $subject 'subject' => $subject
]; ];
}
return $registerArray; /**
* Get an array with a list of filepaths of all registration files
*
* @api
* @return array
*/
public function getRegistrationFiles(): array {
return $this->registrationFiles;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment