|
2 | 2 |
|
3 | 3 | namespace Enqueue\Client;
|
4 | 4 |
|
5 |
| -use Enqueue\Client\Driver\RabbitMqDriver; |
6 | 5 | use Enqueue\Client\Driver\RabbitMqStompDriver;
|
7 | 6 | use Enqueue\Client\Driver\StompManagementClient;
|
8 | 7 | use Enqueue\Dsn\Dsn;
|
9 |
| -use Enqueue\Stomp\StompConnectionFactory; |
10 |
| -use Interop\Amqp\AmqpConnectionFactory; |
11 | 8 | use Interop\Queue\ConnectionFactory;
|
12 | 9 |
|
13 | 10 | final class DriverFactory implements DriverFactoryInterface
|
14 | 11 | {
|
15 |
| - /** |
16 |
| - * @var Config |
17 |
| - */ |
18 |
| - private $config; |
19 |
| - |
20 |
| - /** |
21 |
| - * @var RouteCollection |
22 |
| - */ |
23 |
| - private $routeCollection; |
24 |
| - |
25 |
| - public function __construct(Config $config, RouteCollection $routeCollection) |
| 12 | + public function create(ConnectionFactory $factory, Config $config, RouteCollection $collection): DriverInterface |
26 | 13 | {
|
27 |
| - $this->config = $config; |
28 |
| - $this->routeCollection = $routeCollection; |
29 |
| - } |
| 14 | + $dsn = $config->getTransportOption('dsn'); |
| 15 | + |
| 16 | + if (empty($dsn)) { |
| 17 | + throw new \LogicException('This driver factory relies on dsn option from transport config. The option is empty or not set.'); |
| 18 | + } |
30 | 19 |
|
31 |
| - public function create(ConnectionFactory $factory, string $dsn, array $config): DriverInterface |
32 |
| - { |
33 | 20 | $dsn = Dsn::parseFirst($dsn);
|
34 | 21 |
|
35 | 22 | if ($driverInfo = $this->findDriverInfo($dsn, Resources::getAvailableDrivers())) {
|
36 | 23 | $driverClass = $driverInfo['driverClass'];
|
37 | 24 |
|
38 |
| - if (RabbitMqDriver::class === $driverClass) { |
39 |
| - if (false == $factory instanceof AmqpConnectionFactory) { |
40 |
| - throw new \LogicException(sprintf( |
41 |
| - 'The factory must be instance of "%s", got "%s"', |
42 |
| - AmqpConnectionFactory::class, |
43 |
| - get_class($factory) |
44 |
| - )); |
45 |
| - } |
46 |
| - |
47 |
| - return new RabbitMqDriver($factory->createContext(), $this->config, $this->routeCollection); |
48 |
| - } |
49 |
| - |
50 | 25 | if (RabbitMqStompDriver::class === $driverClass) {
|
51 |
| - if (false == $factory instanceof StompConnectionFactory) { |
52 |
| - throw new \LogicException(sprintf( |
53 |
| - 'The factory must be instance of "%s", got "%s"', |
54 |
| - StompConnectionFactory::class, |
55 |
| - get_class($factory) |
56 |
| - )); |
57 |
| - } |
58 |
| - |
59 |
| - $managementClient = StompManagementClient::create( |
60 |
| - ltrim($dsn->getPath(), '/'), |
61 |
| - $dsn->getHost() ?: 'localhost', |
62 |
| - $config['management_plugin_port'] ?? 15672, |
63 |
| - (string) $dsn->getUser(), |
64 |
| - (string) $dsn->getPassword() |
65 |
| - ); |
66 |
| - |
67 |
| - return new RabbitMqStompDriver($factory->createContext(), $this->config, $this->routeCollection, $managementClient); |
| 26 | + return $this->createRabbitMqStompDriver($factory, $dsn, $config, $collection); |
68 | 27 | }
|
69 | 28 |
|
70 |
| - return new $driverClass($factory->createContext(), $this->config, $this->routeCollection); |
| 29 | + return new $driverClass($factory->createContext(), $config, $collection); |
71 | 30 | }
|
72 | 31 |
|
73 | 32 | $knownDrivers = Resources::getKnownDrivers();
|
@@ -121,4 +80,20 @@ private function findDriverInfo(Dsn $dsn, array $factories): ?array
|
121 | 80 |
|
122 | 81 | return null;
|
123 | 82 | }
|
| 83 | + |
| 84 | + private function createRabbitMqStompDriver(ConnectionFactory $factory, Dsn $dsn, Config $config, RouteCollection $collection): RabbitMqStompDriver |
| 85 | + { |
| 86 | + $defaultManagementHost = $dsn->getHost() ?: $config->getTransportOption('host', 'localhost'); |
| 87 | + $managementVast = ltrim($dsn->getPath(), '/') ?: $config->getTransportOption('vhost', '/'); |
| 88 | + |
| 89 | + $managementClient = StompManagementClient::create( |
| 90 | + urldecode($managementVast), |
| 91 | + $config->getDriverOption('rabbitmq_management_host', $defaultManagementHost), |
| 92 | + $config->getDriverOption('rabbitmq_management_port', 15672), |
| 93 | + (string) $dsn->getUser() ?: $config->getTransportOption('user', 'guest'), |
| 94 | + (string) $dsn->getPassword() ?: $config->getTransportOption('pass', 'guest') |
| 95 | + ); |
| 96 | + |
| 97 | + return new RabbitMqStompDriver($factory->createContext(), $config, $collection, $managementClient); |
| 98 | + } |
124 | 99 | }
|
0 commit comments