Skip to content

Commit

Permalink
MailExtension: added getConfigSchema()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 1, 2019
1 parent aab9e4f commit a6931df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
35 changes: 19 additions & 16 deletions src/Bridges/MailDI/MailExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,42 @@
namespace Nette\Bridges\MailDI;

use Nette;
use Nette\Schema\Expect;


/**
* Mail extension for Nette DI.
*/
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') {
Expand Down

0 comments on commit a6931df

Please sign in to comment.