Skip to content

Commit 973ac0f

Browse files
authored
Merge pull request #646 from php-enqueue/driver-factory-rework
Rework DriverFactory, add separator option to Client Config.
2 parents fa06187 + dab2bc4 commit 973ac0f

File tree

19 files changed

+281
-128
lines changed

19 files changed

+281
-128
lines changed

docs/bundle/config_reference.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ enqueue:
3939
client:
4040
traceable_producer: true
4141
prefix: enqueue
42+
separator: .
4243
app_name: app
4344
router_topic: default
4445
router_queue: default
4546
router_processor: null
46-
default_processor_queue: default
4747
redelivered_delay_time: 0
48+
default_queue: default
49+
50+
# The array contains driver specific options
51+
driver_options: []
4852

4953
# The "monitoring" option could accept a string DSN, an array with DSN key, or null. It accept extra options. To find out what option you can set, look at stats storage constructor doc block.
5054
monitoring:
@@ -57,9 +61,12 @@ enqueue:
5761

5862
# The factory service should be a class that implements "Enqueue\Monitoring\StatsStorageFactory" interface
5963
storage_factory_class: ~
60-
job: false
6164
async_commands:
6265
enabled: false
66+
job:
67+
enabled: false
68+
async_events:
69+
enabled: false
6370
extensions:
6471
doctrine_ping_connection_extension: false
6572
doctrine_clear_identity_map_extension: false

docs/laravel/quick_tour.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ return [
6666
'client' => [
6767
'router_topic' => 'default',
6868
'router_queue' => 'default',
69-
'default_processor_queue' => 'default',
69+
'default_queue' => 'default',
7070
],
7171
],
7272
];

pkg/enqueue-bundle/Tests/Functional/App/CustomAppKernel.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CustomAppKernel extends Kernel
2222
'app_name' => '',
2323
'router_topic' => 'test',
2424
'router_queue' => 'test',
25-
'default_processor_queue' => 'test',
25+
'default_queue' => 'test',
2626
],
2727
],
2828
];

pkg/enqueue-bundle/Tests/Unit/DependencyInjection/ConfigurationTest.php

+56-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Symfony\Component\Config\Definition\ConfigurationInterface;
99
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
10+
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
1011
use Symfony\Component\Config\Definition\Processor;
1112

1213
class ConfigurationTest extends TestCase
@@ -96,14 +97,66 @@ public function testShouldSetDefaultConfigurationForClient()
9697
'router_processor' => null,
9798
'router_topic' => 'default',
9899
'router_queue' => 'default',
99-
'default_processor_queue' => 'default',
100+
'default_queue' => 'default',
100101
'traceable_producer' => true,
101102
'redelivered_delay_time' => 0,
102103
],
103104
],
104105
], $config);
105106
}
106107

108+
public function testThrowIfClientDriverOptionsIsNotArray()
109+
{
110+
$configuration = new Configuration(true);
111+
112+
$processor = new Processor();
113+
114+
$this->expectException(InvalidTypeException::class);
115+
$this->expectExceptionMessage('Invalid type for path "enqueue.default.client.driver_options". Expected array, but got string');
116+
$processor->processConfiguration($configuration, [[
117+
'default' => [
118+
'transport' => 'null:',
119+
'client' => [
120+
'driver_options' => 'invalidOption',
121+
],
122+
],
123+
]]);
124+
}
125+
126+
public function testShouldConfigureClientDriverOptions()
127+
{
128+
$configuration = new Configuration(true);
129+
130+
$processor = new Processor();
131+
$config = $processor->processConfiguration($configuration, [[
132+
'default' => [
133+
'transport' => 'null:',
134+
'client' => [
135+
'driver_options' => [
136+
'foo' => 'fooVal',
137+
],
138+
],
139+
],
140+
]]);
141+
142+
$this->assertConfigEquals([
143+
'default' => [
144+
'client' => [
145+
'prefix' => 'enqueue',
146+
'app_name' => 'app',
147+
'router_processor' => null,
148+
'router_topic' => 'default',
149+
'router_queue' => 'default',
150+
'default_queue' => 'default',
151+
'traceable_producer' => true,
152+
'driver_options' => [
153+
'foo' => 'fooVal',
154+
],
155+
],
156+
],
157+
], $config);
158+
}
159+
107160
public function testThrowExceptionIfRouterTopicIsEmpty()
108161
{
109162
$this->expectException(InvalidConfigurationException::class);
@@ -147,12 +200,12 @@ public function testShouldThrowExceptionIfDefaultProcessorQueueIsEmpty()
147200
$processor = new Processor();
148201

149202
$this->expectException(InvalidConfigurationException::class);
150-
$this->expectExceptionMessage('The path "enqueue.default.client.default_processor_queue" cannot contain an empty value, but got "".');
203+
$this->expectExceptionMessage('The path "enqueue.default.client.default_queue" cannot contain an empty value, but got "".');
151204
$processor->processConfiguration($configuration, [[
152205
'default' => [
153206
'transport' => ['dsn' => 'null:'],
154207
'client' => [
155-
'default_processor_queue' => '',
208+
'default_queue' => '',
156209
],
157210
],
158211
]]);

pkg/enqueue/Client/Config.php

+39-10
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,22 @@ class Config
5252
*/
5353
private $transportConfig;
5454

55-
public function __construct(string $prefix, string $app, string $routerTopic, string $routerQueue, string $defaultQueue, string $routerProcessor, array $transportConfig = [])
56-
{
55+
/**
56+
* @var array
57+
*/
58+
private $driverConfig;
59+
60+
public function __construct(
61+
string $prefix,
62+
string $separator,
63+
string $app,
64+
string $routerTopic,
65+
string $routerQueue,
66+
string $defaultQueue,
67+
string $routerProcessor,
68+
array $transportConfig,
69+
array $driverConfig
70+
) {
5771
$this->prefix = trim($prefix);
5872
$this->app = trim($app);
5973

@@ -78,8 +92,9 @@ public function __construct(string $prefix, string $app, string $routerTopic, st
7892
}
7993

8094
$this->transportConfig = $transportConfig;
95+
$this->driverConfig = $driverConfig;
8196

82-
$this->separator = '.';
97+
$this->separator = $separator;
8398
}
8499

85100
public function getPrefix(): string
@@ -117,33 +132,47 @@ public function getRouterProcessor(): string
117132
return $this->routerProcessor;
118133
}
119134

120-
/**
121-
* @deprecated
122-
*
123-
* @param null|mixed $default
124-
*/
125135
public function getTransportOption(string $name, $default = null)
126136
{
127137
return array_key_exists($name, $this->transportConfig) ? $this->transportConfig[$name] : $default;
128138
}
129139

140+
public function getTransportOptions(): array
141+
{
142+
return $this->transportConfig;
143+
}
144+
145+
public function getDriverOption(string $name, $default = null)
146+
{
147+
return array_key_exists($name, $this->driverConfig) ? $this->driverConfig[$name] : $default;
148+
}
149+
150+
public function getDriverOptions(): array
151+
{
152+
return $this->driverConfig;
153+
}
154+
130155
public static function create(
131156
string $prefix = null,
157+
string $separator = null,
132158
string $app = null,
133159
string $routerTopic = null,
134160
string $routerQueue = null,
135161
string $defaultQueue = null,
136162
string $routerProcessor = null,
137-
array $transportConfig = []
163+
array $transportConfig = [],
164+
array $driverConfig = []
138165
): self {
139166
return new static(
140167
$prefix ?: '',
168+
$separator ?: '.',
141169
$app ?: '',
142170
$routerTopic ?: 'router',
143171
$routerQueue ?: 'default',
144172
$defaultQueue ?: 'default',
145173
$routerProcessor ?: 'router',
146-
$transportConfig
174+
$transportConfig,
175+
$driverConfig
147176
);
148177
}
149178
}

pkg/enqueue/Client/DriverFactory.php

+24-49
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,31 @@
22

33
namespace Enqueue\Client;
44

5-
use Enqueue\Client\Driver\RabbitMqDriver;
65
use Enqueue\Client\Driver\RabbitMqStompDriver;
76
use Enqueue\Client\Driver\StompManagementClient;
87
use Enqueue\Dsn\Dsn;
9-
use Enqueue\Stomp\StompConnectionFactory;
10-
use Interop\Amqp\AmqpConnectionFactory;
118
use Interop\Queue\ConnectionFactory;
129

1310
final class DriverFactory implements DriverFactoryInterface
1411
{
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
2613
{
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+
}
3019

31-
public function create(ConnectionFactory $factory, string $dsn, array $config): DriverInterface
32-
{
3320
$dsn = Dsn::parseFirst($dsn);
3421

3522
if ($driverInfo = $this->findDriverInfo($dsn, Resources::getAvailableDrivers())) {
3623
$driverClass = $driverInfo['driverClass'];
3724

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-
5025
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);
6827
}
6928

70-
return new $driverClass($factory->createContext(), $this->config, $this->routeCollection);
29+
return new $driverClass($factory->createContext(), $config, $collection);
7130
}
7231

7332
$knownDrivers = Resources::getKnownDrivers();
@@ -121,4 +80,20 @@ private function findDriverInfo(Dsn $dsn, array $factories): ?array
12180

12281
return null;
12382
}
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+
}
12499
}

pkg/enqueue/Client/DriverFactoryInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
interface DriverFactoryInterface
88
{
9-
public function create(ConnectionFactory $factory, string $dsn, array $config): DriverInterface;
9+
public function create(ConnectionFactory $factory, Config $config, RouteCollection $collection): DriverInterface;
1010
}

0 commit comments

Comments
 (0)