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 {
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \BadFunctionCallException
* @throws \InvalidArgumentException
* @deprecated since 4.7.0 will be removed in 5.0.0. Use the RegistrationService class instead
*/
public static function getRegisterArray() {
/** @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) {
$entry = self::registerExtensions();
if ($entry === NULL) {
$entry = [];
}
public static function getRegisterArray(): array {
// @TODO remove this deprecated function in version 5.0.0
self::logDeprecation('registerExtensions');
$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 {
* If found, register the template(s)
*
* @throws \BadFunctionCallException
* @deprecated since 4.7.0 will be removed in 5.0.0
* @return array
* @deprecated since 4.7.0 will be removed in 5.0.0. Use the RegistrationService class instead
*/
public static function registerExtensions(): array {
public static function registerExtensions() {
// @TODO remove this deprecated function in version 5.0.0
self::logDeprecation('registerExtensions');
// clear registerArray
$registerArray = [];
$registerService = GeneralUtility::makeInstance(RegisterService::class);
$extensionList = ExtensionManagementUtility::getLoadedExtensionListArray();
foreach ($extensionList as $extensionName) {
......@@ -820,90 +809,16 @@ class MailTemplateService {
if (\is_dir($extensionConfigDirectory)) {
$configFiles = GeneralUtility::getFilesInDir($extensionConfigDirectory);
foreach ($configFiles as $configFile) {
if (!\file_exists($extensionConfigDirectory . '/' . $configFile)) {
continue;
}
$pathToRegistrationFile = $extensionConfigDirectory . '/' . $configFile;
$configArray = (include $extensionConfigDirectory . '/' . $configFile);
$extensionKey = $configArray['extension_key'];
$templateKey = $configArray['template_key'];
if ($extensionKey === NULL || $templateKey === NULL) {
if (!\file_exists($pathToRegistrationFile)) {
continue;
}
$registerArray = self::writeRegisterArrayEntry(
$registerArray, $extensionKey, $templateKey, $configArray
);
$registerService->register($pathToRegistrationFile);
}
}
}
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;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Class RegisterService
* 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/';
/**
* contains the actual registration data
*
* @var array
*/
public $registerArray = [];
private $registerArray = [];
/**
* contains the paths to the registration files
*
* @var array
*/
private $registrationFiles = [];
......@@ -54,6 +59,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
* Enables extensions to add their registration files
*
* @param string $pathToRegisterFile
* @api
*/
public function register($pathToRegisterFile) {
if (file_exists($pathToRegisterFile)) {
......@@ -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
*
* @internal
*/
private function registerExtensions() {
foreach ($this->registrationFiles as $registerFile) {
// clear registerArray
$registerArray = [];
public function registerExtensions() {
// clear registerArray
$this->registerArray = [];
foreach ($this->registrationFiles as $registerFile) {
if (\is_file($registerFile)) {
$configArray = (include $registerFile);
......@@ -110,31 +117,26 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
continue;
}
$registerArray = $this->writeRegisterArrayEntry(
$registerArray, $extensionKey, $templateKey, $configArray
$this->writeRegisterArrayEntry(
$extensionKey, $templateKey, $configArray
);
}
// set the register array after every registration has been parsed
$this->registerArray = $registerArray;
}
}
/**
* write a single entry into the register array
*
* @param array $registerArray
* @param $extensionKey
* @param $templateKey
* @param array $configArray
* @param bool $transformTemplateFolder
* @param string $storeTemplateExtension
* @return array
*/
private function writeRegisterArrayEntry(
array $registerArray, $extensionKey, $templateKey, array $configArray,
$extensionKey, $templateKey, array $configArray,
$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 ($storeTemplateExtension === '') {
$storeTemplateExtension = $extensionKey;
......@@ -165,7 +167,7 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
$subject = $configArray['subject'];
$marker = $configArray['markers'];
$registerArray[$extensionKey][$templateKey] = [
$this->registerArray[$extensionKey][$templateKey] = [
'templatePath' => $templatePath,
'description' => $description,
'marker' => $marker,
......@@ -173,8 +175,16 @@ class RegisterService implements \TYPO3\CMS\Core\SingletonInterface {
'templateName' => $templateKey,
'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