From a6931df7c191e335eb752398601d878cffeb6b38 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 11 Mar 2019 23:57:05 +0100 Subject: [PATCH] MailExtension: added getConfigSchema() --- composer.json | 3 +++ src/Bridges/MailDI/MailExtension.php | 35 +++++++++++++++------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/composer.json b/composer.json index b2f8ade..520eaff 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,9 @@ "nette/tester": "^2.0", "tracy/tracy": "^2.4" }, + "conflict": { + "nette/di": "<3.0" + }, "suggest": { "ext-fileinfo": "to detect type of attached files" }, diff --git a/src/Bridges/MailDI/MailExtension.php b/src/Bridges/MailDI/MailExtension.php index 887fd35..05ee5c0 100644 --- a/src/Bridges/MailDI/MailExtension.php +++ b/src/Bridges/MailDI/MailExtension.php @@ -10,6 +10,7 @@ namespace Nette\Bridges\MailDI; use Nette; +use Nette\Schema\Expect; /** @@ -17,32 +18,34 @@ */ class MailExtension extends Nette\DI\CompilerExtension { - public $defaults = [ - 'smtp' => false, - 'host' => null, - 'port' => null, - 'username' => null, - 'password' => null, - 'secure' => null, - 'timeout' => null, - 'context' => null, - 'clientHost' => null, - 'persistent' => false, - ]; + public function getConfigSchema(): Nette\Schema\Schema + { + return Expect::structure([ + 'smtp' => Expect::bool(false), + 'host' => Expect::string()->dynamic(), + 'port' => Expect::int()->dynamic(), + 'username' => Expect::string()->dynamic(), + 'password' => Expect::string()->dynamic(), + 'secure' => Expect::anyOf(null, 'ssl', 'tls')->dynamic(), + 'timeout' => Expect::int()->dynamic(), + 'context' => Expect::arrayOf('array')->dynamic(), + 'clientHost' => Expect::string()->dynamic(), + 'persistent' => Expect::bool(false)->dynamic(), + ])->castTo('array'); + } public function loadConfiguration() { $builder = $this->getContainerBuilder(); - $config = $this->validateConfig($this->defaults); $mailer = $builder->addDefinition($this->prefix('mailer')) ->setType(Nette\Mail\IMailer::class); - if (empty($config['smtp'])) { - $mailer->setFactory(Nette\Mail\SendmailMailer::class); + if ($this->config['smtp']) { + $mailer->setFactory(Nette\Mail\SmtpMailer::class, [$this->config]); } else { - $mailer->setFactory(Nette\Mail\SmtpMailer::class, [$config]); + $mailer->setFactory(Nette\Mail\SendmailMailer::class); } if ($this->name === 'mail') {