-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathDefaultTransportFactory.php
148 lines (124 loc) · 4.39 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
<?php
namespace Enqueue\Symfony;
use Enqueue\AmqpExt\AmqpConnectionFactory;
use Enqueue\AmqpExt\Symfony\AmqpTransportFactory;
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
{
/**
* @var string
*/
private $name;
/**
* @param string $name
*/
public function __construct($name = 'default')
{
$this->name = $name;
}
/**
* {@inheritdoc}
*/
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->beforeNormalization()
->ifString()
->then(function ($v) {
if (false === strpos($v, '://')) {
return ['alias' => $v];
}
return ['dsn' => $v];
})
->end()
->children()
->scalarNode('alias')->cannotBeEmpty()->end()
->scalarNode('dsn')->cannotBeEmpty()->end()
;
}
public function createConnectionFactory(ContainerBuilder $container, array $config)
{
if (isset($config['alias'])) {
$aliasId = sprintf('enqueue.transport.%s.connection_factory', $config['alias']);
} elseif (isset($config['dsn'])) {
$aliasId = $this->findFactory($config['dsn'])->createConnectionFactory($container, $config);
} else {
throw new \LogicException('Either dsn or alias option must be set.');
}
$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']);
} elseif (isset($config['dsn'])) {
$aliasId = $this->findFactory($config['dsn'])->createContext($container, $config);
} else {
throw new \LogicException('Either dsn or alias option must be set.');
}
$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']);
} elseif (isset($config['dsn'])) {
$aliasId = $this->findFactory($config['dsn'])->createDriver($container, $config);
} else {
throw new \LogicException('Either dsn or alias option must be set.');
}
$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;
}
/**
* @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 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
));
}
}