123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Notifier;
- use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
- use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
- use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
- use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
- use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
- use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
- use Symfony\Component\Notifier\Bridge\Sinch\SinchTransportFactory;
- use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
- use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
- use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
- use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
- use Symfony\Component\Notifier\Transport\Dsn;
- use Symfony\Component\Notifier\Transport\FailoverTransport;
- use Symfony\Component\Notifier\Transport\NullTransportFactory;
- use Symfony\Component\Notifier\Transport\RoundRobinTransport;
- use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
- use Symfony\Component\Notifier\Transport\TransportInterface;
- use Symfony\Component\Notifier\Transport\Transports;
- use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
- use Symfony\Contracts\HttpClient\HttpClientInterface;
- /**
- * @author Fabien Potencier <fabien@symfony.com>
- *
- * @experimental in 5.1
- */
- class Transport
- {
- private const FACTORY_CLASSES = [
- SlackTransportFactory::class,
- TelegramTransportFactory::class,
- MattermostTransportFactory::class,
- NexmoTransportFactory::class,
- RocketChatTransportFactory::class,
- TwilioTransportFactory::class,
- OvhCloudTransportFactory::class,
- FirebaseTransportFactory::class,
- SinchTransportFactory::class,
- FreeMobileTransportFactory::class,
- ];
- private $factories;
- public static function fromDsn(string $dsn, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null): TransportInterface
- {
- $factory = new self(self::getDefaultFactories($dispatcher, $client));
- return $factory->fromString($dsn);
- }
- public static function fromDsns(array $dsns, EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null): TransportInterface
- {
- $factory = new self(iterator_to_array(self::getDefaultFactories($dispatcher, $client)));
- return $factory->fromStrings($dsns);
- }
- /**
- * @param TransportFactoryInterface[] $factories
- */
- public function __construct(iterable $factories)
- {
- $this->factories = $factories;
- }
- public function fromStrings(array $dsns): Transports
- {
- $transports = [];
- foreach ($dsns as $name => $dsn) {
- $transports[$name] = $this->fromString($dsn);
- }
- return new Transports($transports);
- }
- public function fromString(string $dsn): TransportInterface
- {
- $dsns = preg_split('/\s++\|\|\s++/', $dsn);
- if (\count($dsns) > 1) {
- return new FailoverTransport($this->createFromDsns($dsns));
- }
- $dsns = preg_split('/\s++&&\s++/', $dsn);
- if (\count($dsns) > 1) {
- return new RoundRobinTransport($this->createFromDsns($dsns));
- }
- return $this->fromDsnObject(Dsn::fromString($dsn));
- }
- public function fromDsnObject(Dsn $dsn): TransportInterface
- {
- foreach ($this->factories as $factory) {
- if ($factory->supports($dsn)) {
- return $factory->create($dsn);
- }
- }
- throw new UnsupportedSchemeException($dsn);
- }
- /**
- * @return TransportInterface[]
- */
- private function createFromDsns(array $dsns): array
- {
- $transports = [];
- foreach ($dsns as $dsn) {
- $transports[] = $this->fromDsnObject(Dsn::fromString($dsn));
- }
- return $transports;
- }
- /**
- * @return TransportFactoryInterface[]
- */
- private static function getDefaultFactories(EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null): iterable
- {
- foreach (self::FACTORY_CLASSES as $factoryClass) {
- if (class_exists($factoryClass)) {
- yield new $factoryClass($dispatcher, $client);
- }
- }
- yield new NullTransportFactory($dispatcher, $client);
- }
- }
|