Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/ILIAS/DI/src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public function cron(): \ILIAS\Cron\CronServices

public function mail(): \ILIAS\Mail\Service\MailService
{
return new \ILIAS\Mail\Service\MailService($this);
return $this[\ILIAS\Mail\Service\MailService::class];
}

public function certificate(): \ILIAS\Certificate\Service\CertificateService
Expand Down
7 changes: 7 additions & 0 deletions components/ILIAS/Init/classes/class.ilInitialisation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use ILIAS\LegalDocuments\Conductor;
use ILIAS\ILIASObject\Properties\AdditionalProperties\Icon\Factory as CustomIconFactory;
use ILIAS\User\PublicInterface as UserPublicInterface;
use ILIAS\Mail\Service\MailService;

// needed for slow queries, etc.
if (!isset($GLOBALS['ilGlobalStartTime']) || !$GLOBALS['ilGlobalStartTime']) {
Expand Down Expand Up @@ -764,6 +765,11 @@ protected static function initLegalDocuments(Container $c): void
$c['legalDocuments'] = static fn(Container $c) => new Conductor($c);
}

protected static function initMail(Container $c): void
{
MailService::init($c);
}

protected static function initAccessibilityControlConcept(\ILIAS\DI\Container $c): void
{
$c['acc.criteria.type.factory'] = function (\ILIAS\DI\Container $c) {
Expand Down Expand Up @@ -1312,6 +1318,7 @@ protected static function initClient(): void
self::initAvatar($GLOBALS['DIC']);
self::initCustomObjectIcons($GLOBALS['DIC']);
self::initLegalDocuments($GLOBALS['DIC']);
self::initMail($GLOBALS['DIC']);
self::initAccessibilityControlConcept($GLOBALS['DIC']);
self::initLearningObjectMetadata($GLOBALS['DIC']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

declare(strict_types=1);

use ILIAS\Mail\TemplateEngine\TemplateEngineFactoryInterface;

class ilMailMimeSenderFactory
{
/** @var array<int, ilMailMimeSender> */
Expand All @@ -26,7 +28,7 @@ class ilMailMimeSenderFactory

public function __construct(
protected ilSetting $settings,
protected ilMustacheFactory $mustache_factory,
protected TemplateEngineFactoryInterface $template_engine_factory,
?int $anonymous_usr_id = null
) {
if ($anonymous_usr_id === null && defined('ANONYMOUS_USER_ID')) {
Expand Down Expand Up @@ -68,11 +70,11 @@ public function system(): ilMailMimeSenderSystem

public function user(int $usr_id): ilMailMimeSenderUser
{
return new ilMailMimeSenderUserById($this->settings, $usr_id, $this->mustache_factory);
return new ilMailMimeSenderUserById($this->settings, $usr_id, $this->template_engine_factory);
}

public function userByEmailAddress(string $email_address): ilMailMimeSenderUser
{
return new ilMailMimeSenderUserByEmailAddress($this->settings, $email_address, $this->mustache_factory);
return new ilMailMimeSenderUserByEmailAddress($this->settings, $email_address, $this->template_engine_factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

declare(strict_types=1);

use ILIAS\Mail\TemplateEngine\TemplateEngineFactoryInterface;

abstract class ilMailMimeSenderUser implements ilMailMimeSender
{
public function __construct(
protected ilSetting $settings,
protected ilObjUser $user,
protected ilMustacheFactory $mustache_factory
protected TemplateEngineFactoryInterface $template_engine_factory
) {
}

Expand Down Expand Up @@ -82,7 +84,7 @@ public function getFromName(): string
];

$template = $from;
$interpolated = $this->mustache_factory->getBasicEngine()->render($template, $placeholders);
$interpolated = $this->template_engine_factory->getBasicEngine()->render($template, $placeholders);

if ($template !== $interpolated) {
return $interpolated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

declare(strict_types=1);

use ILIAS\Mail\TemplateEngine\TemplateEngineFactoryInterface;

class ilMailMimeSenderUserByEmailAddress extends ilMailMimeSenderUser
{
public function __construct(ilSetting $settings, string $email_address, ilMustacheFactory $mustache_factory)
public function __construct(ilSetting $settings, string $email_address, TemplateEngineFactoryInterface $template_engine_factory)
{
$user = new ilObjUser();
$user->setEmail($email_address);

parent::__construct($settings, $user, $mustache_factory);
parent::__construct($settings, $user, $template_engine_factory);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@

declare(strict_types=1);

use ILIAS\Mail\TemplateEngine\TemplateEngineFactoryInterface;

class ilMailMimeSenderUserById extends ilMailMimeSenderUser
{
/** @var array<int, ilObjUser> */
protected static array $user_instances = [];

public function __construct(ilSetting $settings, int $usr_id, ilMustacheFactory $mustache_factory)
public function __construct(ilSetting $settings, int $usr_id, TemplateEngineFactoryInterface $template_engine_factory)
{
if (!array_key_exists($usr_id, self::$user_instances)) {
self::$user_instances[$usr_id] = new ilObjUser($usr_id);
}

parent::__construct($settings, self::$user_instances[$usr_id], $mustache_factory);
parent::__construct($settings, self::$user_instances[$usr_id], $template_engine_factory);
}

public static function addUserToCache(int $usr_id, ilObjUser $user): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@

declare(strict_types=1);

use ILIAS\Mail\TemplateEngine\TemplateEngineInterface;

/**
* This class forms an interface between the existing ILIAS mail contexts and the requirements of Mustache.
* This class forms an interface between the existing ILIAS mail contexts and the requirements of the template engine.
* In the old system, it was possible to gradually replace individual placeholders via the contexts.
* This is now done by Mustache and requires a single source. If a placeholder does not exist in this source,
* This is now done by the template engine and requires a single source. If a placeholder does not exist in this source,
* then it will be replaced with NULL. Mustache takes two steps to find the placeholder.
* On the one hand the check whether it would be theoretically possible and the actual query for the value
* if the check is successful. This is done in this class using the two magic methods.
* With the introduction of this interface, the ILIAS mail contexts do not have to be changed for Mustache.
* With the introduction of this interface, the ILIAS mail contexts do not have to be changed for the template engine.
*/
class ilMailTemplateContextAdapter
{
public function __construct(
/** @var ilMailTemplateContext[] $contexts */
protected array $contexts,
protected array $context_parameter,
protected readonly Mustache_Engine $mustache_engine,
protected readonly TemplateEngineInterface $template_engine,
protected ?ilObjUser $recipient = null
) {
}
Expand Down Expand Up @@ -67,7 +69,7 @@ public function __get(string $name): string
foreach ($this->contexts as $context) {
$ret = $context->resolvePlaceholder($name, $this->context_parameter, $this->recipient);
if (in_array($name, $context->getNestedPlaceholders(), true)) {
$ret = $this->mustache_engine->render($ret, $this);
$ret = $this->template_engine->render($ret, $this);
}
if ($ret !== '') {
return $ret;
Expand Down
136 changes: 100 additions & 36 deletions components/ILIAS/Mail/classes/Service/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,139 @@
namespace ILIAS\Mail\Service;

use ILIAS\DI\Container;
use ILIAS\Mail\Autoresponder\AutoresponderServiceImpl;
use ILIAS\Mail\Autoresponder\AutoresponderService;
use ilMailTemplateService;
use ILIAS\Data\Factory as DataFactory;
use ILIAS\Mail\Autoresponder\AutoresponderDatabaseRepository;
use ilMailMimeSenderFactory;
use ilMailTemplateRepository;
use ilMailMimeTransportFactory;
use ilMailTemplateServiceInterface;
use ILIAS\Data\Factory as DataFactory;
use ilMailTemplatePlaceholderResolver;
use ilMailTemplatePlaceholderToEmptyResolver;
use ILIAS\Mail\Autoresponder\AutoresponderService;
use ILIAS\Mail\Autoresponder\AutoresponderServiceImpl;
use ILIAS\Mail\Autoresponder\AutoresponderDatabaseRepository;
use ILIAS\Mail\TemplateEngine\TemplateEngineFactoryInterface;
use ILIAS\Mail\TemplateEngine\Mustache\MustacheTemplateEngineFactory;

class MailService
{
public function __construct(protected Container $dic)
{
if (!isset($this->dic[ilMailTemplateServiceInterface::class])) {
$this->dic[ilMailTemplateServiceInterface::class] = static function (Container $c): ilMailTemplateServiceInterface {
return new ilMailTemplateService(
new ilMailTemplateRepository($c->database()),
$c->mail()->mustacheFactory()
);
};
}
}

public static function init(Container $container): void
{
$container[self::class] = static function (Container $c): self {
return new self($c);
};

$container[ilMailTemplateServiceInterface::class] = static function (Container $c): ilMailTemplateServiceInterface {
return new ilMailTemplateService(
new ilMailTemplateRepository($c->database()),
$c->mail()->templateEngineFactory()
);
};

$container[TemplateEngineFactoryInterface::class] = static function (Container $c): TemplateEngineFactoryInterface {
return $c->mail()->templateEngineFactory();
};

$container[MimeMailService::class] = static function (Container $c): MimeMailService {
return new MimeMailService($c);
};

$container['mail.mime.transport.factory'] = static function (Container $c): ilMailMimeTransportFactory {
return new ilMailMimeTransportFactory($c->settings(), $c->event());
};

$container['mail.mime.sender.factory'] = static function (Container $c): ilMailMimeSenderFactory {
return new ilMailMimeSenderFactory(
$c->settings(),
$c->mail()->templateEngineFactory()
);
};

$container['mail.texttemplates.service'] = static function (Container $c): ilMailTemplateService {
return new ilMailTemplateService(
new ilMailTemplateRepository($c->database()),
$c->mail()->templateEngineFactory()
);
};

$container['mail.template.placeholder.resolver'] = static function (Container $c): ilMailTemplatePlaceholderResolver {
return new ilMailTemplatePlaceholderResolver(
$c->mail()->templateEngineFactory()->getBasicEngine()
);
};

$container[AutoresponderService::class] = static function (Container $c): AutoresponderService {
return new AutoresponderServiceImpl(
(int) $c->settings()->get(
'mail_auto_responder_idle_time',
(string) AutoresponderService::AUTO_RESPONDER_DEFAULT_IDLE_TIME
),
false,
new AutoresponderDatabaseRepository($c->database()),
(new DataFactory())->clock()->utc()
);
};

$container[ilMailTemplatePlaceholderResolver::class] = static function (Container $c): ilMailTemplatePlaceholderResolver {
return new ilMailTemplatePlaceholderResolver(
$c->mail()->templateEngineFactory()->getBasicEngine()
);
};

$container[ilMailTemplatePlaceholderToEmptyResolver::class] = static function (Container $c): ilMailTemplatePlaceholderToEmptyResolver {
return new ilMailTemplatePlaceholderToEmptyResolver();
};

$container['mail.template_engine.factory'] = static function (Container $c): MustacheTemplateEngineFactory {
return new MustacheTemplateEngineFactory();
};

$container['mail.signature.service'] = static function (Container $c): MailSignatureService {
return new MailSignatureService(
$c->mail()->templateEngineFactory(),
$c->clientIni(),
$c->language(),
$c->settings()
);
};
}

public function mime(): MimeMailService
{
return new MimeMailService($this->dic);
return $this->dic[MimeMailService::class];
}

public function autoresponder(): AutoresponderService
{
return new AutoresponderServiceImpl(
(int) $this->dic->settings()->get(
'mail_auto_responder_idle_time',
(string) AutoresponderService::AUTO_RESPONDER_DEFAULT_IDLE_TIME
),
false,
new AutoresponderDatabaseRepository($this->dic->database()),
(new DataFactory())->clock()->utc()
);
return $this->dic[AutoresponderService::class];
}

public function textTemplates(): ilMailTemplateServiceInterface
{
return $this->dic[ilMailTemplateServiceInterface::class];
}

public function placeholderResolver(): \ilMailTemplatePlaceholderResolver
public function placeholderResolver(): ilMailTemplatePlaceholderResolver
{
return new \ilMailTemplatePlaceholderResolver(
$this->mustacheFactory()->getBasicEngine()
);
return $this->dic[ilMailTemplatePlaceholderResolver::class];
}

public function placeholderToEmptyResolver(): \ilMailTemplatePlaceholderToEmptyResolver
public function placeholderToEmptyResolver(): ilMailTemplatePlaceholderToEmptyResolver
{
return new \ilMailTemplatePlaceholderToEmptyResolver();
return $this->dic[ilMailTemplatePlaceholderToEmptyResolver::class];
}

public function mustacheFactory(): \ilMustacheFactory
public function templateEngineFactory(): TemplateEngineFactoryInterface
{
return new \ilMustacheFactory();
return $this->dic['mail.template_engine.factory'];
}

public function signature(): MailSignatureService
{
return new MailSignatureService(
$this->mustacheFactory(),
$this->dic->clientIni(),
$this->dic->language(),
$this->dic->settings()
);
return $this->dic['mail.signature.service'];
}
}
37 changes: 0 additions & 37 deletions components/ILIAS/Mail/classes/Service/MimeMailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,6 @@ class MimeMailService
{
public function __construct(protected Container $dic)
{
if (!isset($this->dic['mail.mime.transport.factory'])) {
$this->dic['mail.mime.transport.factory'] = static function (Container $c): ilMailMimeTransportFactory {
return new ilMailMimeTransportFactory($c->settings(), $c->event());
};
}

if (!isset($this->dic['mail.mime.sender.factory'])) {
$this->dic['mail.mime.sender.factory'] = static function (Container $c): ilMailMimeSenderFactory {
return new ilMailMimeSenderFactory(
$c->settings(),
$c->mail()->mustacheFactory()
);
};
}

if (!isset($this->dic['mail.texttemplates.service'])) {
$this->dic['mail.texttemplates.service'] = static function (Container $c): \ilMailTemplateService {
return new \ilMailTemplateService(
new \ilMailTemplateRepository($c->database()),
$this->dic['mail.mustache.factory']
);
};
}

if (!isset($this->dic['mail.mustache.factory'])) {
$this->dic["mail.mustache.factory"] = static function (Container $c): \ilMustacheFactory {
return new \ilMustacheFactory();
};
}

if (!isset($this->dic['mail.template.placeholder.resolver'])) {
$this->dic["mail.template.placeholder.resolver"] = static function (Container $c): \ilMailTemplatePlaceholderResolver {
return new \ilMailTemplatePlaceholderResolver(
$c["mail.mustache.factory"]->getBasicEngine()
);
};
}
}

public function transportFactory(): ilMailMimeTransportFactory
Expand Down
Loading