Transport.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mailer;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Mailer\Bridge\Amazon\Transport\SesTransportFactory;
  13. use Symfony\Component\Mailer\Bridge\Google\Transport\GmailTransportFactory;
  14. use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
  15. use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
  16. use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
  17. use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
  18. use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
  19. use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
  20. use Symfony\Component\Mailer\Exception\InvalidArgumentException;
  21. use Symfony\Component\Mailer\Exception\UnsupportedSchemeException;
  22. use Symfony\Component\Mailer\Transport\Dsn;
  23. use Symfony\Component\Mailer\Transport\FailoverTransport;
  24. use Symfony\Component\Mailer\Transport\NativeTransportFactory;
  25. use Symfony\Component\Mailer\Transport\NullTransportFactory;
  26. use Symfony\Component\Mailer\Transport\RoundRobinTransport;
  27. use Symfony\Component\Mailer\Transport\SendmailTransportFactory;
  28. use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransportFactory;
  29. use Symfony\Component\Mailer\Transport\TransportFactoryInterface;
  30. use Symfony\Component\Mailer\Transport\TransportInterface;
  31. use Symfony\Component\Mailer\Transport\Transports;
  32. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  33. use Symfony\Contracts\HttpClient\HttpClientInterface;
  34. /**
  35. * @author Fabien Potencier <fabien@symfony.com>
  36. * @author Konstantin Myakshin <molodchick@gmail.com>
  37. */
  38. class Transport
  39. {
  40. private const FACTORY_CLASSES = [
  41. SesTransportFactory::class,
  42. GmailTransportFactory::class,
  43. MandrillTransportFactory::class,
  44. MailgunTransportFactory::class,
  45. PostmarkTransportFactory::class,
  46. SendgridTransportFactory::class,
  47. MailjetTransportFactory::class,
  48. SendinblueTransportFactory::class,
  49. ];
  50. private $factories;
  51. public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
  52. {
  53. $factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher, $client, $logger)));
  54. return $factory->fromString($dsn);
  55. }
  56. public static function fromDsns(array $dsns, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): TransportInterface
  57. {
  58. $factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher, $client, $logger)));
  59. return $factory->fromStrings($dsns);
  60. }
  61. /**
  62. * @param TransportFactoryInterface[] $factories
  63. */
  64. public function __construct(iterable $factories)
  65. {
  66. $this->factories = $factories;
  67. }
  68. public function fromStrings(array $dsns): Transports
  69. {
  70. $transports = [];
  71. foreach ($dsns as $name => $dsn) {
  72. $transports[$name] = $this->fromString($dsn);
  73. }
  74. return new Transports($transports);
  75. }
  76. public function fromString(string $dsn): TransportInterface
  77. {
  78. [$transport, $offset] = $this->parseDsn($dsn);
  79. if ($offset !== \strlen($dsn)) {
  80. throw new InvalidArgumentException(sprintf('The DSN has some garbage at the end: "%s".', substr($dsn, $offset)));
  81. }
  82. return $transport;
  83. }
  84. private function parseDsn(string $dsn, int $offset = 0): array
  85. {
  86. static $keywords = [
  87. 'failover' => FailoverTransport::class,
  88. 'roundrobin' => RoundRobinTransport::class,
  89. ];
  90. while (true) {
  91. foreach ($keywords as $name => $class) {
  92. $name .= '(';
  93. if ($name === substr($dsn, $offset, \strlen($name))) {
  94. $offset += \strlen($name) - 1;
  95. preg_match('{\(([^()]|(?R))*\)}A', $dsn, $matches, 0, $offset);
  96. if (!isset($matches[0])) {
  97. continue;
  98. }
  99. ++$offset;
  100. $args = [];
  101. while (true) {
  102. [$arg, $offset] = $this->parseDsn($dsn, $offset);
  103. $args[] = $arg;
  104. if (\strlen($dsn) === $offset) {
  105. break;
  106. }
  107. ++$offset;
  108. if (')' === $dsn[$offset - 1]) {
  109. break;
  110. }
  111. }
  112. return [new $class($args), $offset];
  113. }
  114. }
  115. if (preg_match('{(\w+)\(}A', $dsn, $matches, 0, $offset)) {
  116. throw new InvalidArgumentException(sprintf('The "%s" keyword is not valid (valid ones are "%s"), ', $matches[1], implode('", "', array_keys($keywords))));
  117. }
  118. if ($pos = strcspn($dsn, ' )', $offset)) {
  119. return [$this->fromDsnObject(Dsn::fromString(substr($dsn, $offset, $pos))), $offset + $pos];
  120. }
  121. return [$this->fromDsnObject(Dsn::fromString(substr($dsn, $offset))), \strlen($dsn)];
  122. }
  123. }
  124. public function fromDsnObject(Dsn $dsn): TransportInterface
  125. {
  126. foreach ($this->factories as $factory) {
  127. if ($factory->supports($dsn)) {
  128. return $factory->create($dsn);
  129. }
  130. }
  131. throw new UnsupportedSchemeException($dsn);
  132. }
  133. public static function getDefaultFactories(EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, LoggerInterface $logger = null): iterable
  134. {
  135. foreach (self::FACTORY_CLASSES as $factoryClass) {
  136. if (class_exists($factoryClass)) {
  137. yield new $factoryClass($dispatcher, $client, $logger);
  138. }
  139. }
  140. yield new NullTransportFactory($dispatcher, $client, $logger);
  141. yield new SendmailTransportFactory($dispatcher, $client, $logger);
  142. yield new EsmtpTransportFactory($dispatcher, $client, $logger);
  143. yield new NativeTransportFactory($dispatcher, $client, $logger);
  144. }
  145. }