Skip to content
Permalink
Browse files

MailExtension: flexible mailer definition, require contributte/di

  • Loading branch information...
mabar authored and f3l1x committed Jul 20, 2019
1 parent c0b8dab commit 21a1e3439a25837ed4057ca8ce7ee75f13cd30a0
Showing with 12 additions and 11 deletions.
  1. +2 −5 composer.json
  2. +10 −6 src/DI/MailExtension.php
@@ -21,7 +21,7 @@
"nette/utils": "~3.0.0"
},
"require-dev": {
"nette/di": "~3.0.0",
"contributte/di": "^0.4.0",
"ninjify/nunjuck": "^0.2.0",
"ninjify/qa": "^0.9.0",
"phpstan/extension-installer": "^1.0",
@@ -31,11 +31,8 @@
"phpstan/phpstan-strict-rules": "^0.11",
"tracy/tracy": "~2.6.2"
},
"conflict": {
"nette/di": "<=3.0.0-RC"
},
"suggest": {
"nette/di": "to use MailerExtension[CompilerExtension]"
"contributte/di": "to use MailerExtension[CompilerExtension]"
},
"autoload": {
"psr-4": {
@@ -2,10 +2,12 @@
namespace Contributte\Mail\DI;
use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Contributte\Mail\Mailer\TraceableMailer;
use Contributte\Mail\Message\IMessageFactory;
use Contributte\Mail\Tracy\MailPanel;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\ClassType;
use Nette\Schema\Expect;
@@ -31,7 +33,7 @@ public function getConfigSchema(): Schema
{
return Expect::structure([
'mode' => Expect::anyOf(...self::MODES)->default(self::MODE_STANDALONE),
'mailer' => Expect::type('string|' . Statement::class)->dynamic()->required(),
'mailer' => Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))->required(),
'debug' => Expect::bool(false),
]);
}
@@ -43,28 +45,30 @@ public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->config;
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);
$builder->addFactoryDefinition($this->prefix('messageFactory'))
->setImplement(IMessageFactory::class);
// Wrap original mailer by TraceableMailer
if ($config->debug) {
$mailer = $builder->addDefinition($this->prefix('mailer.original'))
->setAutowired(false);
$originalMailer = $definitionsHelper->getDefinitionFromConfig($config->mailer, $this->prefix('mailer.original'));
$this->loadDefinitionsFromConfig(['mailer.original' => $config->mailer]);
if ($originalMailer instanceof Definition) {
$originalMailer->setAutowired(false);
}
$traceableMailer = $builder->addDefinition($this->prefix('mailer'))
->setType(TraceableMailer::class)
->setArguments([$mailer]);
->setArguments([$originalMailer]);
// Mail panel for tracy
$builder->addDefinition($this->prefix('panel'))
->setFactory(MailPanel::class)
->addSetup('setTraceableMailer', [$traceableMailer]);
} else {
// Load mailer
$this->loadDefinitionsFromConfig(['mailer' => $config->mailer]);
$definitionsHelper->getDefinitionFromConfig($config->mailer, $this->prefix('mailer'));
}
}

0 comments on commit 21a1e34

Please sign in to comment.
You can’t perform that action at this time.