22
33namespace Enqueue \Symfony ;
44
5+ use Enqueue \AmqpExt \AmqpConnectionFactory ;
6+ use Enqueue \AmqpExt \Symfony \AmqpTransportFactory ;
7+ use Enqueue \Fs \FsConnectionFactory ;
8+ use Enqueue \Fs \Symfony \FsTransportFactory ;
9+ use Enqueue \Null \NullConnectionFactory ;
10+ use Enqueue \Null \Symfony \NullTransportFactory ;
511use Symfony \Component \Config \Definition \Builder \ArrayNodeDefinition ;
612use Symfony \Component \DependencyInjection \ContainerBuilder ;
13+ use function Enqueue \dsn_to_connection_factory ;
714
815class DefaultTransportFactory implements TransportFactoryInterface
916{
@@ -29,18 +36,30 @@ public function addConfiguration(ArrayNodeDefinition $builder)
2936 ->beforeNormalization ()
3037 ->ifString ()
3138 ->then (function ($ v ) {
32- return ['alias ' => $ v ];
39+ if (false === strpos ($ v , ':// ' )) {
40+ return ['alias ' => $ v ];
41+ }
42+
43+ return ['dsn ' => $ v ];
3344 })
3445 ->end ()
3546 ->children ()
36- ->scalarNode ('alias ' )->isRequired ()->cannotBeEmpty ()->end ()
37- ;
47+ ->scalarNode ('alias ' )->cannotBeEmpty ()->end ()
48+ ->scalarNode ('dsn ' )->cannotBeEmpty ()->end ()
49+ ;
3850 }
3951
4052 public function createConnectionFactory (ContainerBuilder $ container , array $ config )
4153 {
54+ if (isset ($ config ['alias ' ])) {
55+ $ aliasId = sprintf ('enqueue.transport.%s.connection_factory ' , $ config ['alias ' ]);
56+ } elseif (isset ($ config ['dsn ' ])) {
57+ $ aliasId = $ this ->findFactory ($ config ['dsn ' ])->createConnectionFactory ($ container , $ config );
58+ } else {
59+ throw new \LogicException ('Either dsn or alias option must be set. ' );
60+ }
61+
4262 $ factoryId = sprintf ('enqueue.transport.%s.connection_factory ' , $ this ->getName ());
43- $ aliasId = sprintf ('enqueue.transport.%s.connection_factory ' , $ config ['alias ' ]);
4463
4564 $ container ->setAlias ($ factoryId , $ aliasId );
4665 $ container ->setAlias ('enqueue.transport.connection_factory ' , $ factoryId );
@@ -53,8 +72,15 @@ public function createConnectionFactory(ContainerBuilder $container, array $conf
5372 */
5473 public function createContext (ContainerBuilder $ container , array $ config )
5574 {
75+ if (isset ($ config ['alias ' ])) {
76+ $ aliasId = sprintf ('enqueue.transport.%s.context ' , $ config ['alias ' ]);
77+ } elseif (isset ($ config ['dsn ' ])) {
78+ $ aliasId = $ this ->findFactory ($ config ['dsn ' ])->createContext ($ container , $ config );
79+ } else {
80+ throw new \LogicException ('Either dsn or alias option must be set. ' );
81+ }
82+
5683 $ contextId = sprintf ('enqueue.transport.%s.context ' , $ this ->getName ());
57- $ aliasId = sprintf ('enqueue.transport.%s.context ' , $ config ['alias ' ]);
5884
5985 $ container ->setAlias ($ contextId , $ aliasId );
6086 $ container ->setAlias ('enqueue.transport.context ' , $ contextId );
@@ -67,8 +93,15 @@ public function createContext(ContainerBuilder $container, array $config)
6793 */
6894 public function createDriver (ContainerBuilder $ container , array $ config )
6995 {
96+ if (isset ($ config ['alias ' ])) {
97+ $ aliasId = sprintf ('enqueue.client.%s.driver ' , $ config ['alias ' ]);
98+ } elseif (isset ($ config ['dsn ' ])) {
99+ $ aliasId = $ this ->findFactory ($ config ['dsn ' ])->createDriver ($ container , $ config );
100+ } else {
101+ throw new \LogicException ('Either dsn or alias option must be set. ' );
102+ }
103+
70104 $ driverId = sprintf ('enqueue.client.%s.driver ' , $ this ->getName ());
71- $ aliasId = sprintf ('enqueue.client.%s.driver ' , $ config ['alias ' ]);
72105
73106 $ container ->setAlias ($ driverId , $ aliasId );
74107 $ container ->setAlias ('enqueue.client.driver ' , $ driverId );
@@ -83,4 +116,33 @@ public function getName()
83116 {
84117 return $ this ->name ;
85118 }
119+
120+ /**
121+ * @param string
122+ * @param mixed $dsn
123+ *
124+ * @return TransportFactoryInterface
125+ */
126+ private function findFactory ($ dsn )
127+ {
128+ $ connectionFactory = dsn_to_connection_factory ($ dsn );
129+
130+ if ($ connectionFactory instanceof AmqpConnectionFactory) {
131+ return new AmqpTransportFactory ('default_amqp ' );
132+ }
133+
134+ if ($ connectionFactory instanceof FsConnectionFactory) {
135+ return new FsTransportFactory ('default_fs ' );
136+ }
137+
138+ if ($ connectionFactory instanceof NullConnectionFactory) {
139+ return new NullTransportFactory ('default_null ' );
140+ }
141+
142+ throw new \LogicException (sprintf (
143+ 'There is no supported transport factory for the connection factory "%s" created from DSN "%s" ' ,
144+ get_class ($ connectionFactory ),
145+ $ dsn
146+ ));
147+ }
86148}
0 commit comments