예제 #1
0
 /**
  * @param string $name
  *
  * @return \Swift_Mailer
  */
 private static function mailer(string $name = null) : \Swift_Mailer
 {
     if ($return = DI::get(__METHOD__)) {
         return $return;
     }
     $config = DI::config()->getIfExists('email/' . ($name ?: 'default'));
     if (!$config) {
         $transport = \Swift_MailTransport::newInstance();
         $return = \Swift_Mailer::newInstance($transport);
     } elseif (is_callable($config)) {
         $return = $config();
     } else {
         $uri = new Uri($config);
         switch ($uri->getScheme()) {
             case 'smtp':
                 $transport = \Swift_SmtpTransport::newInstance($uri->getHost(), $uri->getPort());
                 if ($uri->getUser()) {
                     $transport->setUsername($uri->getUser());
                 }
                 if ($uri->getPassword()) {
                     $transport->setPassword($uri->getPassword());
                 }
                 if ($uri->getQuery('auth')) {
                     $transport->setAuthMode($uri->getQuery('auth'));
                 }
                 if ($uri->getQuery('encryption')) {
                     $transport->setEncryption($uri->getQuery('encryption'));
                 }
                 break;
             case 'echo':
                 $transport = EchoTransport::newInstance();
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf("Undefined email mailer type '%s'", $uri->getScheme()));
                 break;
         }
         $return = \Swift_Mailer::newInstance($transport);
         if ($uri->getQuery('plugins')) {
             foreach ($uri->getQuery('plugins') as $plugin) {
                 $return->registerPlugin(new $plugin());
             }
         }
     }
     return DI::set(__METHOD__, $name, $return);
 }
예제 #2
0
파일: UriTest.php 프로젝트: cawaphp/cawa
 /**
  * Test the port extract
  *
  * @param string $uriString
  * @param array $parts
  * @dataProvider validUriStringProviderWithPart
  */
 public function testPort(string $uriString, array $parts)
 {
     $uri = new Uri($uriString);
     if (isset($parts['port'])) {
         $this->assertEquals($parts['port'], $uri->getPort());
     } else {
         $this->assertNull($uri->getPort());
     }
 }