Commit 1ba82276 authored by Torsten Oppermann's avatar Torsten Oppermann
Browse files

[TASK] Using t3 caching framework, replöacing old register array functions

parent 95b0c2e1
<?php
namespace SGalinski\SgMail\Example;
/***************************************************************
* Copyright notice
*
* (c) sgalinski Internet Services (https://www.sgalinski.de)
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use SGalinski\SgMail\Service\RegisterInterface;
/**
* Example Registration for a Template
*
* @package SGalinski\SgMail\Example
*/
class Register implements RegisterInterface {
/**
* @var array
*/
private $markers = [];
/**
* @var array
*/
private $subject = [];
/**
* @var string
*/
private $description;
/**
* the extension key
*
* @var string
*/
private $extensionKey;
/**
* a unique name for this template
*
* @var string
*/
private $templateKey;
/**
* path where the template is located
*
* @var string
*/
private $templatePath;
/**
* initialize certain values
*/
public function init() {
$templateArray = [
'firstName' => 'Max',
'lastName' => 'Mustermann'
];
$this->markers = [
[
'marker' => 'firstname',
'type' => \SGalinski\SgMail\Service\MailTemplateService::MARKER_TYPE_STRING,
'value' => 'Max',
'description' => 'The first name of the customer'
],
[
'marker' => 'lastname',
'type' => \SGalinski\SgMail\Service\MailTemplateService::MARKER_TYPE_STRING,
'value' => 'Mustermann',
'description' => 'The last name of the customer'
],
[
'marker' => 'person',
'type' => \SGalinski\SgMail\Service\MailTemplateService::MARKER_TYPE_ARRAY,
'value' => $templateArray,
'description' => 'An array with the customer data',
'usage' => '{person.firstName}, {person.lastName}'
]
];
$this->description = 'Description about the Template';
$this->subject = [
'en' => 'The english subject text',
'de' => 'The german subject text'
];
$this->templateKey = 'notice_mail_admin';
$this->extensionKey = 'sg_sample';
}
/**
* Calls MailTemplateService registerTemplate with according values.
*/
public function registerTemplate() {
\SGalinski\SgMail\Service\MailTemplateService::registerTemplate(
$this->extensionKey, $this->templateKey, $this->templatePath, $this->description, $this->markers,
$this->subject
);
}
/**
* @return array
*/
public function getMarkers() {
return $this->markers;
}
/**
* @param array $markers
* @return RegisterInterface
*/
public function setMarkers(array $markers) {
$this->markers = $markers;
return $this;
}
/**
* @return array
*/
public function getSubject() {
return $this->subject;
}
/**
* @param array $subject
* @return RegisterInterface
*/
public function setSubject(array $subject) {
$this->subject = $subject;
return $this;
}
/**
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* @param string $description
* @return RegisterInterface
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* @return string
*/
public function getExtensionKey() {
return $this->extensionKey;
}
/**
* @param string $extensionKey
* @return RegisterInterface
*/
public function setExtensionKey($extensionKey) {
$this->extensionKey = $extensionKey;
return $this;
}
/**
* @return string
*/
public function getTemplateKey() {
return $this->templateKey;
}
/**
* @param string $templateKey
* @return RegisterInterface
*/
public function setTemplateKey($templateKey) {
$this->templateKey = $templateKey;
return $this;
}
/**
* @return string
*/
public function getTemplatePath() {
return $this->templatePath;
}
/**
* @param string $templatePath
* @return RegisterInterface
*/
public function setTemplatePath($templatePath) {
$this->templatePath = $templatePath;
return $this;
}
}
?>
\ No newline at end of file
......@@ -33,6 +33,8 @@ use SGalinski\SgMail\Domain\Repository\MailRepository;
use SGalinski\SgMail\Domain\Repository\TemplateRepository;
use Swift_Attachment;
use Swift_OutputByteStream;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -51,11 +53,8 @@ class MailTemplateService {
const MARKER_TYPE_OBJECT = 'Object';
const DEFAULT_LANGUAGE = 'default';
const DEFAULT_TEMPLATE_PATH = 'Resources/Private/Templates/SgMail/';
/**
* @var array $registerArray
*/
private static $registerArray = [];
const CACHE_NAME = 'sg_mail_registerArrayCache';
const CACHE_LIFETIME_IN_SECONDS = 86400;
/**
* @var array $toAddresses
......@@ -206,59 +205,6 @@ class MailTemplateService {
}
}
/**
* register a template with sg_mail
*
* description and subject can now be an array i.e. with elements such as 'en' => 'english description'
* or an translation string used in locallang.xml
*
* @deprecated public usage of this function is deprecated. use registerByFile instead
* @param string $extension
* @param string $templateName
* @param string $templatePath
* @param mixed $description
* @param array $markers
* @param mixed $subject
* @param string $usage
*/
public static function registerTemplate(
$extension, $templateName, $templatePath, $description, array $markers, $subject, $usage = ''
) {
self::$registerArray[$extension][$templateName] = [
'templatePath' => $templatePath,
'description' => $description,
'marker' => $markers,
'extension' => $extension,
'templateName' => $templateName,
'subject' => $subject,
'usage' => $usage
];
}
/**
* call in extlocalconf of an extension if you have a custom register class
*
* @param RegisterInterface $fileNameWithNamespace
* @param boolean $initObject Should the object initialize itself ?
*
* @return bool
* @throws \InvalidArgumentException
*/
public static function registerByFile($fileNameWithNamespace, $initObject = TRUE) {
$registerObject = GeneralUtility::makeInstance($fileNameWithNamespace);
// check instance of interface
if (!($registerObject instanceof RegisterInterface)) {
return FALSE;
}
// object calls registerTemplate, alternative way instead of localconf call
if ($initObject) {
$registerObject->init();
}
$registerObject->registerTemplate();
return TRUE;
}
/**
* Return default markers for sg_mail
*
......@@ -289,20 +235,14 @@ class MailTemplateService {
return $generatedMarker;
}
/**
* Get all registered templates
*
* @return array
*/
public static function getRegisterArray() {
return self::$registerArray;
}
/**
* Send the Email
*
* @param boolean $isPreview
* @return boolean email was sent or added to mail queue successfully?
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \InvalidArgumentException
* @throws \BadFunctionCallException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
*/
public function sendEmail($isPreview = FALSE) {
......@@ -339,7 +279,7 @@ class MailTemplateService {
// If there is no template for this language, use the default template
if ($template === NULL) {
$templatePath = self::$registerArray[$this->extensionKey][$this->templateName]['templatePath'];
$templatePath = self::getRegisterArray()[$this->extensionKey][$this->templateName]['templatePath'];
// only standard template file is considered since version 4.1
$defaultTemplateFile = $templatePath . 'template.html';
......@@ -355,7 +295,7 @@ class MailTemplateService {
if ($isPreview) {
$previewMarker = [];
/** @var array $markerArray */
$markerArray = self::$registerArray[$this->extensionKey][$this->templateName]['marker'];
$markerArray = self::getRegisterArray()[$this->extensionKey][$this->templateName]['marker'];
foreach ($markerArray as $marker) {
$markerPath = GeneralUtility::trimExplode('.', $marker['marker']);
$temporaryMarkerArray = [];
......@@ -387,12 +327,13 @@ class MailTemplateService {
} else {
$emailView->setTemplateSource($defaultTemplateContent);
$subject = self::$registerArray[$this->extensionKey][$this->templateName]['subject'];
$subject = self::getRegisterArray()[$this->extensionKey][$this->templateName]['subject'];
if (is_array($subject)) {
$subject = self::$registerArray[$this->extensionKey][$this->templateName]['subject'][$this->language];
$subject = self::getRegisterArray(
)[$this->extensionKey][$this->templateName]['subject'][$this->language];
} else {
$subject = LocalizationUtility::translate(
self::$registerArray[$this->extensionKey][$this->templateName]['subject'],
self::getRegisterArray()[$this->extensionKey][$this->templateName]['subject'],
$this->extensionKey
);
}
......@@ -510,14 +451,6 @@ class MailTemplateService {
}
}
/**
* @param array $registerArray
* @return void
*/
public static function setRegisterArray(array $registerArray) {
self::$registerArray = $registerArray;
}
/**
* @param string $toAddresses
* @return MailTemplateService
......@@ -723,15 +656,44 @@ class MailTemplateService {
return $this;
}
/**
* Get all registered templates
*
* @return array
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
* @throws \BadFunctionCallException
* @throws \InvalidArgumentException
*/
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 = [];
}
$cache->set($cacheId, $entry, [], self::CACHE_LIFETIME_IN_SECONDS);
}
return $entry;
}
/**
* Iterate over all installed extensions and look for sg_mail configuration files
* If found, register the template(s)
*
* @throws \BadFunctionCallException
* @return array
*/
public static function registerExtensions() {
// clear registerArray
self::$registerArray = [];
$registerArray = [];
$extensionList = ExtensionManagementUtility::getLoadedExtensionListArray();
......@@ -742,48 +704,39 @@ class MailTemplateService {
foreach ($configFiles as $configFile) {
$configArray = (include $extensionConfigDirectory . '/' . $configFile);
self::registerByConfigArray($configArray, $extensionName);
}
}
}
/**
* Register a template with a config array
*
* @param array $config
* @param string $extensionName
* @throws \BadFunctionCallException
*/
private static function registerByConfigArray(array $config = [], $extensionName) {
$extensionKey = $config['extension_key'];
$templateKey = $config['template_key'];
$extensionKey = $configArray['extension_key'];
$templateKey = $configArray['template_key'];
// transform template directory name: your_templates => YourTemplates/
$templateDirectoryParts = GeneralUtility::trimExplode('_', $configArray['template_key']);
$templateDirectory = '';
foreach ($templateDirectoryParts as $part) {
$templateDirectory .= ucfirst($part);
}
$templateDirectory .= '/';
$templatePath = ExtensionManagementUtility::extPath(
$extensionName
) . self::DEFAULT_TEMPLATE_PATH . $templateDirectory;
// transform template directory name: your_templates => YourTemplates/
$templateDirectoryParts = GeneralUtility::trimExplode('_', $config['template_key']);
$templateDirectory = '';
foreach ($templateDirectoryParts as $part) {
$templateDirectory .= ucfirst($part);
}
$templateDirectory .= '/';
$templatePath = ExtensionManagementUtility::extPath(
$extensionName
) . self::DEFAULT_TEMPLATE_PATH . $templateDirectory;
if ($configArray['template_path']) {
$templatePath = $configArray['template_key'];
}
if ($config['template_path']) {
$templatePath = $config['template_key'];
$description = $configArray['description'];
$subject = $configArray['subject'];
$marker = $configArray['markers'];
$registerArray[$extensionKey][$templateKey] = [
'templatePath' => $templatePath,
'description' => $description,
'marker' => $marker,
'extension' => $extensionKey,
'templateName' => $templateKey,
'subject' => $subject
];
}
}
$description = $config['description'];
$subject = $config['subject'];
$marker = $config['markers'];
self::$registerArray[$extensionKey][$templateKey] = [
'templatePath' => $templatePath,
'description' => $description,
'marker' => $marker,
'extension' => $extensionKey,
'templateName' => $templateKey,
'subject' => $subject
];
return $registerArray;
}
}
......@@ -47,4 +47,14 @@ if (TYPO3_MODE === 'BE') {
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTypoScriptSetup(file_get_contents($tsPath . 'setup.ts'));
}
\SGalinski\SgMail\Service\MailTemplateService::registerExtensions();
// Cache registration
$cacheName = \SGalinski\SgMail\Service\MailTemplateService::CACHE_NAME;
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheName])) {
$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheName] = [];
}
if (!isset($TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations'][$cacheName]['groups'])) {
$TYPO3_CONF_VARS['SYS']['caching']['cacheConfigurations'][$cacheName]['groups'] = [
'citypower'
];
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment