Implment DSN parsing

This commit is contained in:
Oleg Voronkovich 2023-02-20 17:22:49 +03:00
parent abeba566ef
commit 2fc807cf0c
2 changed files with 102 additions and 0 deletions

71
src/DSNConfigurator.php Normal file
View File

@ -0,0 +1,71 @@
<?php
/**
* PHPMailer - PHP email creation and transport class.
* PHP Version 5.5.
*
* @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project
*
* @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk>
* @author Jim Jagielski (jimjag) <jimjag@gmail.com>
* @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
* @author Brent R. Matzelle (original founder)
* @copyright 2012 - 2020 Marcus Bointon
* @copyright 2010 - 2012 Jim Jagielski
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
* @note This program is distributed in the hope that it will be useful - WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/
namespace PHPMailer\PHPMailer;
/**
* Configure PHPMailer via DSN.
*
* @author Oleg Voronkovich (voronkovich) <oleg-voronkovich@yandex.ru>
*/
class DSNConfigurator
{
/**
* Configure PHPMailer via DSN.
*
* @param PHPMailer $mailer PHPMailer instance
* @param string $dsn DSN
*
* @return PHPMailer
*/
public function configure(PHPMailer $mailer, $dsn)
{
$config = $this->parseDSN($dsn);
return $mailer;
}
/**
* Parse DSN.
*
* @param string $dsn DSN
*
* @throws Exception If DSN is mailformed
*
* @return array configruration
*/
private function parseDSN($dsn)
{
$config = parse_url($dsn);
if (false === $config || !isset($config['scheme']) || !isset($config['host'])) {
throw new Exception(
sprintf('Mailformed DSN: "%s".', $dsn)
);
}
if (isset($config['query'])) {
parse_str($config['query'], $config['query']);
}
return $config;
}
}

View File

@ -0,0 +1,31 @@
<?php
/**
* PHPMailer - PHP email transport unit tests.
* PHP version 5.5.
*
* @author Marcus Bointon <phpmailer@synchromedia.co.uk>
* @author Andy Prevost
* @copyright 2012 - 2020 Marcus Bointon
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
namespace PHPMailer\Test\PHPMailer;
use PHPMailer\PHPMailer\DSNConfigurator;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\Test\TestCase;
final class DSNConfiguratorTest extends TestCase
{
public function testInvalidDSN()
{
$configurator = new DSNConfigurator();
$this->expectException(Exception::class);
$this->expectExceptionMessage('Mailformed DSN: "localhost".');
$configurator->configure($this->Mail, 'localhost');
}
}