-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathDefaultTransportFactory.php
194 lines (160 loc) · 5.77 KB
/
DefaultTransportFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Enqueue\Symfony;
use Enqueue\AmqpExt\AmqpConnectionFactory;
use Enqueue\AmqpExt\Symfony\AmqpTransportFactory;
use Enqueue\Dbal\DbalConnectionFactory;
use Enqueue\Dbal\Symfony\DbalTransportFactory;
use Enqueue\Fs\FsConnectionFactory;
use Enqueue\Fs\Symfony\FsTransportFactory;
use Enqueue\Null\NullConnectionFactory;
use Enqueue\Null\Symfony\NullTransportFactory;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use function Enqueue\dsn_to_connection_factory;
class DefaultTransportFactory implements TransportFactoryInterface, DriverFactoryInterface
{
/**
* @var string
*/
private $name;
/**
* @param string $name
*/
public function __construct($name = 'default')
{
$this->name = $name;
}
/**
* {@inheritdoc}
*/
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->beforeNormalization()
->always(function ($v) {
if (is_array($v)) {
if (empty($v['dsn']) && empty($v['alias'])) {
throw new \LogicException('Either dsn or alias option must be set');
}
return $v;
}
if (empty($v)) {
return ['dsn' => 'null://'];
}
if (is_string($v)) {
return false !== strpos($v, '://') || false !== strpos($v, 'env_') ?
['dsn' => $v] :
['alias' => $v];
}
})
->end()
->children()
->scalarNode('alias')->cannotBeEmpty()->end()
->scalarNode('dsn')->cannotBeEmpty()->end()
->end()
->end()
;
}
public function createConnectionFactory(ContainerBuilder $container, array $config)
{
if (isset($config['alias'])) {
$aliasId = sprintf('enqueue.transport.%s.connection_factory', $config['alias']);
} else {
$dsn = $this->resolveDSN($container, $config['dsn']);
$aliasId = $this->findFactory($dsn)->createConnectionFactory($container, $config);
}
$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
$container->setAlias($factoryId, $aliasId);
$container->setAlias('enqueue.transport.connection_factory', $factoryId);
return $factoryId;
}
/**
* {@inheritdoc}
*/
public function createContext(ContainerBuilder $container, array $config)
{
if (isset($config['alias'])) {
$aliasId = sprintf('enqueue.transport.%s.context', $config['alias']);
} else {
$dsn = $this->resolveDSN($container, $config['dsn']);
$aliasId = $this->findFactory($dsn)->createContext($container, $config);
}
$contextId = sprintf('enqueue.transport.%s.context', $this->getName());
$container->setAlias($contextId, $aliasId);
$container->setAlias('enqueue.transport.context', $contextId);
return $contextId;
}
/**
* {@inheritdoc}
*/
public function createDriver(ContainerBuilder $container, array $config)
{
if (isset($config['alias'])) {
$aliasId = sprintf('enqueue.client.%s.driver', $config['alias']);
} else {
$dsn = $this->resolveDSN($container, $config['dsn']);
$aliasId = $this->findFactory($dsn)->createDriver($container, $config);
}
$driverId = sprintf('enqueue.client.%s.driver', $this->getName());
$container->setAlias($driverId, $aliasId);
$container->setAlias('enqueue.client.driver', $driverId);
return $driverId;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* This is a quick fix to the exception "Incompatible use of dynamic environment variables "ENQUEUE_DSN" found in parameters."
* TODO: We'll have to come up with a better solution.
*
* @param ContainerBuilder $container
* @param $dsn
*
* @return array|false|string
*/
private function resolveDSN(ContainerBuilder $container, $dsn)
{
if (method_exists($container, 'resolveEnvPlaceholders')) {
$dsn = $container->resolveEnvPlaceholders($dsn);
$matches = [];
if (preg_match('/%env\((.*?)\)/', $dsn, $matches)) {
if (false === $realDsn = getenv($matches[1])) {
throw new \LogicException(sprintf('The env "%s" var is not defined', $matches[1]));
}
return $realDsn;
}
}
return $dsn;
}
/**
* @param string
* @param mixed $dsn
*
* @return TransportFactoryInterface
*/
private function findFactory($dsn)
{
$connectionFactory = dsn_to_connection_factory($dsn);
if ($connectionFactory instanceof AmqpConnectionFactory) {
return new AmqpTransportFactory('default_amqp');
}
if ($connectionFactory instanceof FsConnectionFactory) {
return new FsTransportFactory('default_fs');
}
if ($connectionFactory instanceof DbalConnectionFactory) {
return new DbalTransportFactory('default_dbal');
}
if ($connectionFactory instanceof NullConnectionFactory) {
return new NullTransportFactory('default_null');
}
throw new \LogicException(sprintf(
'There is no supported transport factory for the connection factory "%s" created from DSN "%s"',
get_class($connectionFactory),
$dsn
));
}
}