Skip to content

Commit fed2c39

Browse files
committed
[stomp] Use enqueue/dsn to parse stomp dsn.
1 parent b704695 commit fed2c39

7 files changed

Lines changed: 69 additions & 34 deletions

‎bin/pre-commit‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,22 @@ function runPhpCsFixer()
107107
$returnCode = null;
108108

109109
exec(sprintf(
110-
'%s %s fix --config=.php_cs.php --no-interaction -v --path-mode=intersection -- %s',
110+
'%s %s fix --config=.php_cs.php --dry-run --no-interaction --path-mode=intersection -- %s',
111111
$phpBin,
112112
$phpCsFixerBin,
113113
implode(' ', getFilesToFix())
114114
), $output, $returnCode);
115115

116-
return $returnCode ? $output : false;
116+
if ($returnCode) {
117+
exec(sprintf(
118+
'%s %s fix --config=.php_cs.php --no-interaction -v --path-mode=intersection -- %s',
119+
$phpBin,
120+
$phpCsFixerBin,
121+
implode(' ', getFilesToFix())
122+
), $output);
123+
}
124+
125+
return $returnCode;
117126
}
118127

119128
function runPhpstan()
@@ -143,16 +152,10 @@ if ($phpSyntaxErrors) {
143152
exit(1);
144153
}
145154

146-
$phpCSFixerErrors = runPhpCsFixer();
147-
if ($phpCSFixerErrors) {
155+
$phpCSFixed = runPhpCsFixer();
156+
if ($phpCSFixed) {
148157
echo "Incorrect coding standards were detected and fixed." . PHP_EOL;
149158
echo "Please stash changes and run commit again." . PHP_EOL;
150-
echo "Output:" . PHP_EOL . PHP_EOL;
151-
152-
echo array_walk_recursive($phpStanErrors, function($item) {
153-
echo $item.PHP_EOL;
154-
}) . PHP_EOL;
155-
echo PHP_EOL;
156159

157160
exit(1);
158161
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Enqueue\Dsn;
3+
4+
final class InvalidQueryParameterTypeException extends \LogicException
5+
{
6+
public static function create(string $name, string $expectedType): self
7+
{
8+
return new static(sprintf('The query parameter "%s" has invalid type. It must be "%s"', $name, $expectedType));
9+
}
10+
}

‎pkg/stomp/StompConnectionFactory.php‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Enqueue\Stomp;
66

7+
use Enqueue\Dsn\Dsn;
78
use Interop\Queue\PsrConnectionFactory;
89
use Interop\Queue\PsrContext;
910
use Stomp\Network\Connection;
@@ -48,6 +49,11 @@ public function __construct($config = 'stomp:')
4849
} elseif (is_string($config)) {
4950
$config = $this->parseDsn($config);
5051
} elseif (is_array($config)) {
52+
if (array_key_exists('dsn', $config)) {
53+
$config = array_replace($config, $this->parseDsn($config['dsn']));
54+
55+
unset($config['dsn']);
56+
}
5157
} else {
5258
throw new \LogicException('The config must be either an array of options, a DSN string or null');
5359
}
@@ -91,27 +97,24 @@ private function establishConnection(): BufferedStompClient
9197

9298
private function parseDsn(string $dsn): array
9399
{
94-
if (false === strpos($dsn, 'stomp:')) {
95-
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "stomp:".', $dsn));
96-
}
97-
98-
if (false === $config = parse_url($dsn)) {
99-
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
100-
}
100+
$dsn = new Dsn($dsn);
101101

102-
if ($query = parse_url($dsn, PHP_URL_QUERY)) {
103-
$queryConfig = [];
104-
parse_str($query, $queryConfig);
105-
106-
$config = array_replace($queryConfig, $config);
102+
if ('stomp' !== $dsn->getSchemeProtocol()) {
103+
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "stomp:".', $dsn));
107104
}
108105

109-
unset($config['query'], $config['scheme']);
110-
111-
$config['sync'] = empty($config['sync']) ? false : true;
112-
$config['lazy'] = empty($config['lazy']) ? false : true;
113-
114-
return $config;
106+
return array_filter(array_replace($dsn->getQuery(), [
107+
'host' => $dsn->getHost(),
108+
'port' => $dsn->getPort(),
109+
'login' => $dsn->getUser(),
110+
'password' => $dsn->getPassword(),
111+
'vhost' => null !== $dsn->getPath() ? ltrim($dsn->getPath(), '/') : null,
112+
'buffer_size' => $dsn->getInt('buffer_size'),
113+
'connection_timeout' => $dsn->getInt('connection_timeout'),
114+
'sync' => $dsn->getBool('sync'),
115+
'lazy' => $dsn->getBool('lazy'),
116+
'ssl_on' => $dsn->getBool('ssl_on'),
117+
]), function ($value) { return null !== $value; });
115118
}
116119

117120
private function defaultConfig(): array

‎pkg/stomp/StompConsumer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public function receive(int $timeout = 0): ?PsrMessage
9898

9999
if (0 === $timeout) {
100100
while (true) {
101-
if ($message = $this->stomp->readMessageFrame($this->subscriptionId, 0.1)) {
101+
if ($message = $this->stomp->readMessageFrame($this->subscriptionId, 100)) {
102102
return $this->convertMessage($message);
103103
}
104104
}

‎pkg/stomp/Tests/Functional/StompCommonUseCasesTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testWaitsForTwoSecondsAndReturnNullOnReceive()
4141
$startAt = microtime(true);
4242

4343
$consumer = $this->stompContext->createConsumer($queue);
44-
$message = $consumer->receive(2);
44+
$message = $consumer->receive(2000);
4545

4646
$endAt = microtime(true);
4747

@@ -85,7 +85,7 @@ public function testProduceAndReceiveOneMessage()
8585
$producer->send($queue, $message);
8686

8787
$consumer = $this->stompContext->createConsumer($queue);
88-
$message = $consumer->receive(1);
88+
$message = $consumer->receive(1000);
8989

9090
$this->assertInstanceOf(StompMessage::class, $message);
9191
$consumer->acknowledge($message);

‎pkg/stomp/Tests/StompConnectionFactoryConfigTest.php‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig()
2121
new StompConnectionFactory(new \stdClass());
2222
}
2323

24-
public function testThrowIfSchemeIsNotAmqp()
24+
public function testThrowIfSchemeIsNotStomp()
2525
{
2626
$this->expectException(\LogicException::class);
2727
$this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "stomp:".');
@@ -32,9 +32,9 @@ public function testThrowIfSchemeIsNotAmqp()
3232
public function testThrowIfDsnCouldNotBeParsed()
3333
{
3434
$this->expectException(\LogicException::class);
35-
$this->expectExceptionMessage('Failed to parse DSN "stomp://:@/"');
35+
$this->expectExceptionMessage('The DSN is invalid.');
3636

37-
new StompConnectionFactory('stomp://:@/');
37+
new StompConnectionFactory('foo');
3838
}
3939

4040
/**
@@ -117,6 +117,24 @@ public static function provideConfigs()
117117
],
118118
];
119119

120+
yield [
121+
['dsn' => 'stomp://localhost:1234/theVhost?foo=bar&lazy=0&sync=true', 'baz' => 'bazVal', 'foo' => 'fooVal'],
122+
[
123+
'host' => 'localhost',
124+
'port' => 1234,
125+
'login' => 'guest',
126+
'password' => 'guest',
127+
'vhost' => 'theVhost',
128+
'buffer_size' => 1000,
129+
'connection_timeout' => 1,
130+
'sync' => true,
131+
'lazy' => false,
132+
'foo' => 'bar',
133+
'ssl_on' => false,
134+
'baz' => 'bazVal',
135+
],
136+
];
137+
120138
yield [
121139
['host' => 'localhost', 'port' => 1234, 'foo' => 'bar'],
122140
[

‎pkg/stomp/composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"license": "MIT",
88
"require": {
99
"php": "^7.1.3",
10+
"enqueue/dsn": "0.9.x-dev",
1011
"stomp-php/stomp-php": "^4",
1112
"queue-interop/queue-interop": "0.7.x-dev",
1213
"php-http/guzzle6-adapter": "^1.1",

0 commit comments

Comments
 (0)