From eac26e8e95fa37335657a7d5c0595df2a3783c99 Mon Sep 17 00:00:00 2001
From: Torsten Oppermann <torsten@sgalinski.de>
Date: Mon, 17 Sep 2018 10:06:03 +0200
Subject: [PATCH] [TASK] Refactored the registration process

---
 Classes/Service/MailTemplateService.php | 113 +++---------------------
 Classes/Service/RegisterService.php     |  46 ++++++----
 2 files changed, 42 insertions(+), 117 deletions(-)

diff --git a/Classes/Service/MailTemplateService.php b/Classes/Service/MailTemplateService.php
index 8bbc37d2..92d99617 100644
--- a/Classes/Service/MailTemplateService.php
+++ b/Classes/Service/MailTemplateService.php
@@ -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;
 	}
 
 	/**
diff --git a/Classes/Service/RegisterService.php b/Classes/Service/RegisterService.php
index 2454d9a1..ba8dda3c 100644
--- a/Classes/Service/RegisterService.php
+++ b/Classes/Service/RegisterService.php
@@ -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;
 	}
 }
 
-- 
GitLab