TransportFactory.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Messenger\Transport;
  11. use Symfony\Component\Messenger\Exception\InvalidArgumentException;
  12. use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
  13. /**
  14. * @author Samuel Roze <samuel.roze@gmail.com>
  15. */
  16. class TransportFactory implements TransportFactoryInterface
  17. {
  18. private $factories;
  19. /**
  20. * @param iterable|TransportFactoryInterface[] $factories
  21. */
  22. public function __construct(iterable $factories)
  23. {
  24. $this->factories = $factories;
  25. }
  26. public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface
  27. {
  28. foreach ($this->factories as $factory) {
  29. if ($factory->supports($dsn, $options)) {
  30. return $factory->createTransport($dsn, $options, $serializer);
  31. }
  32. }
  33. // Help the user to select Symfony packages based on protocol.
  34. $packageSuggestion = '';
  35. if (0 === strpos($dsn, 'amqp://')) {
  36. $packageSuggestion = ' Run "composer require symfony/amqp-messenger" to install AMQP transport.';
  37. } elseif (0 === strpos($dsn, 'doctrine://')) {
  38. $packageSuggestion = ' Run "composer require symfony/doctrine-messenger" to install Doctrine transport.';
  39. } elseif (0 === strpos($dsn, 'redis://')) {
  40. $packageSuggestion = ' Run "composer require symfony/redis-messenger" to install Redis transport.';
  41. } elseif (0 === strpos($dsn, 'sqs://') || preg_match('#^https://sqs\.[\w\-]+\.amazonaws\.com/.+#', $dsn)) {
  42. $packageSuggestion = ' Run "composer require symfony/amazon-sqs-messenger" to install Amazon SQS transport.';
  43. } elseif (0 === strpos($dsn, 'beanstalkd://')) {
  44. $packageSuggestion = ' Run "composer require symfony/beanstalkd-messenger" to install Beanstalkd transport.';
  45. }
  46. throw new InvalidArgumentException(sprintf('No transport supports the given Messenger DSN "%s".%s.', $dsn, $packageSuggestion));
  47. }
  48. public function supports(string $dsn, array $options): bool
  49. {
  50. foreach ($this->factories as $factory) {
  51. if ($factory->supports($dsn, $options)) {
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. }