From 9cb59d62bd5c6f00fda87ae6279aa50ef3056d8c Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 25 Sep 2017 15:41:34 +0300 Subject: [PATCH 1/5] [dsn] replace xxx:// to xxx: --- docs/transport/kafka.md | 2 +- pkg/amqp-bunny/AmqpConnectionFactory.php | 5 ++--- pkg/amqp-ext/AmqpConnectionFactory.php | 5 ++--- pkg/amqp-lib/AmqpConnectionFactory.php | 5 ++--- pkg/dbal/DbalConnectionFactory.php | 15 +++++++++------ .../Tests/DbalConnectionFactoryConfigTest.php | 6 +++--- .../DsnToConnectionFactoryFunctionTest.php | 16 ++++++++-------- pkg/fs/FsConnectionFactory.php | 9 +++++---- pkg/gearman/GearmanConnectionFactory.php | 4 ++-- pkg/pheanstalk/PheanstalkConnectionFactory.php | 5 +++-- pkg/rdkafka/RdKafkaConnectionFactory.php | 10 +++++----- .../Tests/RdKafkaConnectionFactoryTest.php | 10 +++++----- 12 files changed, 47 insertions(+), 45 deletions(-) diff --git a/docs/transport/kafka.md b/docs/transport/kafka.md index 915f1b08b..951fb2591 100644 --- a/docs/transport/kafka.md +++ b/docs/transport/kafka.md @@ -25,7 +25,7 @@ use Enqueue\RdKafka\RdKafkaConnectionFactory; $connectionFactory = new RdKafkaConnectionFactory(); // same as above -$connectionFactory = new RdKafkaConnectionFactory('rdkafka://'); +$connectionFactory = new RdKafkaConnectionFactory('rdkafka:'); // same as above $connectionFactory = new RdKafkaConnectionFactory([]); diff --git a/pkg/amqp-bunny/AmqpConnectionFactory.php b/pkg/amqp-bunny/AmqpConnectionFactory.php index 48f5fe742..8bf7c6028 100644 --- a/pkg/amqp-bunny/AmqpConnectionFactory.php +++ b/pkg/amqp-bunny/AmqpConnectionFactory.php @@ -43,14 +43,13 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate * * @param array|string $config */ - public function __construct($config = 'amqp://') + public function __construct($config = 'amqp:') { if (is_string($config) && 0 === strpos($config, 'amqp+bunny:')) { $config = str_replace('amqp+bunny:', 'amqp:', $config); } - // third argument is deprecated will be removed in 0.8 - if (empty($config) || 'amqp:' === $config || 'amqp://' === $config) { + if (empty($config) || 'amqp:' === $config) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); diff --git a/pkg/amqp-ext/AmqpConnectionFactory.php b/pkg/amqp-ext/AmqpConnectionFactory.php index 3002ab0db..824856df5 100644 --- a/pkg/amqp-ext/AmqpConnectionFactory.php +++ b/pkg/amqp-ext/AmqpConnectionFactory.php @@ -45,14 +45,13 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate * * @param array|string $config */ - public function __construct($config = 'amqp://') + public function __construct($config = 'amqp:') { if (is_string($config) && 0 === strpos($config, 'amqp+ext:')) { $config = str_replace('amqp+ext:', 'amqp:', $config); } - // third argument is deprecated will be removed in 0.8 - if (empty($config) || 'amqp:' === $config || 'amqp://' === $config) { + if (empty($config) || 'amqp:' === $config) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); diff --git a/pkg/amqp-lib/AmqpConnectionFactory.php b/pkg/amqp-lib/AmqpConnectionFactory.php index 0ecc1e52c..3c90dd473 100644 --- a/pkg/amqp-lib/AmqpConnectionFactory.php +++ b/pkg/amqp-lib/AmqpConnectionFactory.php @@ -48,14 +48,13 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate * * @param array|string $config */ - public function __construct($config = 'amqp://') + public function __construct($config = 'amqp:') { if (is_string($config) && 0 === strpos($config, 'amqp+lib:')) { $config = str_replace('amqp+lib:', 'amqp:', $config); } - // third argument is deprecated will be removed in 0.8 - if (empty($config) || 'amqp:' === $config || 'amqp://' === $config) { + if (empty($config) || 'amqp:' === $config) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); diff --git a/pkg/dbal/DbalConnectionFactory.php b/pkg/dbal/DbalConnectionFactory.php index f7d034849..55164213c 100644 --- a/pkg/dbal/DbalConnectionFactory.php +++ b/pkg/dbal/DbalConnectionFactory.php @@ -34,10 +34,10 @@ class DbalConnectionFactory implements PsrConnectionFactory * * @param array|string|null $config */ - public function __construct($config = 'mysql://') + public function __construct($config = 'mysql:') { if (empty($config)) { - $config = $this->parseDsn('mysql://'); + $config = $this->parseDsn('mysql:'); } elseif (is_string($config)) { $config = $this->parseDsn($config); } elseif (is_array($config)) { @@ -94,11 +94,14 @@ private function establishConnection() */ private function parseDsn($dsn) { - if (false === strpos($dsn, '://')) { - throw new \LogicException(sprintf('The given DSN "%s" is not valid. Must contain "://".', $dsn)); + if (false === parse_url($dsn)) { + throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn)); } - list($schema, $rest) = explode('://', $dsn, 2); + $schema = parse_url($dsn, PHP_URL_SCHEME); + if (empty($schema)) { + throw new \LogicException('Schema is empty'); + } $supported = [ 'db2' => true, @@ -128,7 +131,7 @@ private function parseDsn($dsn) return [ 'lazy' => true, 'connection' => [ - 'url' => empty($rest) ? $schema.'://root@localhost' : $dsn, + 'url' => $schema.':' === $dsn ? $schema.'://root@localhost' : $dsn, ], ]; } diff --git a/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php b/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php index 39a29563d..eeaf31c34 100644 --- a/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php +++ b/pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php @@ -32,7 +32,7 @@ public function testThrowIfSchemeIsNotSupported() public function testThrowIfDsnCouldNotBeParsed() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The given DSN "invalidDSN" is not valid. Must contain "://".'); + $this->expectExceptionMessage('Schema is empty'); new DbalConnectionFactory('invalidDSN'); } @@ -63,7 +63,7 @@ public static function provideConfigs() ]; yield [ - 'mysql://', + 'mysql:', [ 'lazy' => true, 'connection' => [ @@ -73,7 +73,7 @@ public static function provideConfigs() ]; yield [ - 'pgsql://', + 'pgsql:', [ 'lazy' => true, 'connection' => [ diff --git a/pkg/enqueue/Tests/Functions/DsnToConnectionFactoryFunctionTest.php b/pkg/enqueue/Tests/Functions/DsnToConnectionFactoryFunctionTest.php index 4b7df3852..59c5333b6 100644 --- a/pkg/enqueue/Tests/Functions/DsnToConnectionFactoryFunctionTest.php +++ b/pkg/enqueue/Tests/Functions/DsnToConnectionFactoryFunctionTest.php @@ -56,25 +56,25 @@ public function testReturnsExpectedFactoryInstance($dsn, $expectedFactoryClass) public static function provideDSNs() { - yield ['amqp://', AmqpConnectionFactory::class]; + yield ['amqp:', AmqpConnectionFactory::class]; yield ['amqp://user:pass@foo/vhost', AmqpConnectionFactory::class]; - yield ['file://', FsConnectionFactory::class]; + yield ['file:', FsConnectionFactory::class]; yield ['file:///foo/bar/baz', FsConnectionFactory::class]; - yield ['null://', NullConnectionFactory::class]; + yield ['null:', NullConnectionFactory::class]; - yield ['mysql://', DbalConnectionFactory::class]; + yield ['mysql:', DbalConnectionFactory::class]; - yield ['pgsql://', DbalConnectionFactory::class]; + yield ['pgsql:', DbalConnectionFactory::class]; - yield ['beanstalk://', PheanstalkConnectionFactory::class]; + yield ['beanstalk:', PheanstalkConnectionFactory::class]; - // yield ['gearman://', GearmanConnectionFactory::class]; + // yield ['gearman:', GearmanConnectionFactory::class]; - yield ['rdkafka://', RdKafkaConnectionFactory::class]; + yield ['kafka:', RdKafkaConnectionFactory::class]; yield ['redis:', RedisConnectionFactory::class]; diff --git a/pkg/fs/FsConnectionFactory.php b/pkg/fs/FsConnectionFactory.php index f155c9df9..8627edb95 100644 --- a/pkg/fs/FsConnectionFactory.php +++ b/pkg/fs/FsConnectionFactory.php @@ -23,14 +23,15 @@ class FsConnectionFactory implements PsrConnectionFactory * * or * + * file: - create queue files in tmp dir. * file://home/foo/enqueue * file://home/foo/enqueue?pre_fetch_count=20&chmod=0777 * * @param array|string|null $config */ - public function __construct($config = 'file://') + public function __construct($config = 'file:') { - if (empty($config) || 'file://' === $config) { + if (empty($config) || 'file:' === $config) { $config = ['path' => sys_get_temp_dir().'/enqueue']; } elseif (is_string($config)) { $config = $this->parseDsn($config); @@ -68,8 +69,8 @@ private function parseDsn($dsn) return ['path' => $dsn]; } - if (false === strpos($dsn, 'file://')) { - throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "file://".', $dsn)); + if (false === strpos($dsn, 'file:')) { + throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "file:".', $dsn)); } $dsn = substr($dsn, 7); diff --git a/pkg/gearman/GearmanConnectionFactory.php b/pkg/gearman/GearmanConnectionFactory.php index dd91dccaa..77f8dfd96 100644 --- a/pkg/gearman/GearmanConnectionFactory.php +++ b/pkg/gearman/GearmanConnectionFactory.php @@ -25,9 +25,9 @@ class GearmanConnectionFactory implements PsrConnectionFactory * * @param array|string $config */ - public function __construct($config = 'gearman://') + public function __construct($config = 'gearman:') { - if (empty($config) || 'gearman://' === $config) { + if (empty($config) || 'gearman:' === $config) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); diff --git a/pkg/pheanstalk/PheanstalkConnectionFactory.php b/pkg/pheanstalk/PheanstalkConnectionFactory.php index c2d629769..40999ccef 100644 --- a/pkg/pheanstalk/PheanstalkConnectionFactory.php +++ b/pkg/pheanstalk/PheanstalkConnectionFactory.php @@ -29,13 +29,14 @@ class PheanstalkConnectionFactory implements PsrConnectionFactory * * or * + * beanstalk: - connects to localhost:11300 * beanstalk://host:port * * @param array|string $config */ - public function __construct($config = 'beanstalk://') + public function __construct($config = 'beanstalk:') { - if (empty($config) || 'beanstalk://' === $config) { + if (empty($config) || 'beanstalk:' === $config) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); diff --git a/pkg/rdkafka/RdKafkaConnectionFactory.php b/pkg/rdkafka/RdKafkaConnectionFactory.php index 4db818686..a3fdbc13e 100644 --- a/pkg/rdkafka/RdKafkaConnectionFactory.php +++ b/pkg/rdkafka/RdKafkaConnectionFactory.php @@ -29,13 +29,13 @@ class RdKafkaConnectionFactory implements PsrConnectionFactory * * or * - * rdkafka://host:port + * kafka://host:port * * @param array|string $config */ - public function __construct($config = 'rdkafka://') + public function __construct($config = 'kafka:') { - if (empty($config) || 'rdkafka://' === $config) { + if (empty($config) || 'kafka:' === $config) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); @@ -79,8 +79,8 @@ private function parseDsn($dsn) 'query' => null, ], $dsnConfig); - if ('rdkafka' !== $dsnConfig['scheme']) { - throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "rdkafka" only.', $dsnConfig['scheme'])); + if ('kafka' !== $dsnConfig['scheme']) { + throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "kafka" only.', $dsnConfig['scheme'])); } $config = []; diff --git a/pkg/rdkafka/Tests/RdKafkaConnectionFactoryTest.php b/pkg/rdkafka/Tests/RdKafkaConnectionFactoryTest.php index 1b765679c..3422499b6 100644 --- a/pkg/rdkafka/Tests/RdKafkaConnectionFactoryTest.php +++ b/pkg/rdkafka/Tests/RdKafkaConnectionFactoryTest.php @@ -18,7 +18,7 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig() public function testThrowIfSchemeIsNotBeanstalkAmqp() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "rdkafka" only.'); + $this->expectExceptionMessage('The given DSN scheme "http" is not supported. Could be "kafka" only.'); new RdKafkaConnectionFactory('http://example.com'); } @@ -26,9 +26,9 @@ public function testThrowIfSchemeIsNotBeanstalkAmqp() public function testThrowIfDsnCouldNotBeParsed() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('Failed to parse DSN "rdkafka://:@/"'); + $this->expectExceptionMessage('Failed to parse DSN "kafka://:@/"'); - new RdKafkaConnectionFactory('rdkafka://:@/'); + new RdKafkaConnectionFactory('kafka://:@/'); } public function testShouldBeExpectedDefaultConfig() @@ -50,7 +50,7 @@ public function testShouldBeExpectedDefaultConfig() public function testShouldBeExpectedDefaultDsnConfig() { - $factory = new RdKafkaConnectionFactory('rdkafka://'); + $factory = new RdKafkaConnectionFactory('kafka:'); $config = $this->getObjectAttribute($factory, 'config'); @@ -81,7 +81,7 @@ public function testShouldParseConfigurationAsExpected($config, $expectedConfig) public static function provideConfigs() { yield [ - 'rdkafka://theHost:1234?global%5Bgroup.id%5D=group-id', + 'kafka://theHost:1234?global%5Bgroup.id%5D=group-id', [ 'global' => [ 'metadata.broker.list' => 'theHost:1234', From 26735040fb9eb8b6a90a7bcf41c851661ff55381 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 25 Sep 2017 15:46:17 +0300 Subject: [PATCH 2/5] upd docs. --- docs/transport/amqp.md | 11 ++++++----- docs/transport/amqp_bunny.md | 13 +++++++------ docs/transport/amqp_lib.md | 13 +++++++------ docs/transport/dbal.md | 3 +++ docs/transport/filesystem.md | 2 +- docs/transport/gearman.md | 2 +- docs/transport/gps.md | 3 +++ docs/transport/kafka.md | 2 +- docs/transport/pheanstalk.md | 2 +- 9 files changed, 30 insertions(+), 21 deletions(-) diff --git a/docs/transport/amqp.md b/docs/transport/amqp.md index 8fd374521..065f307af 100644 --- a/docs/transport/amqp.md +++ b/docs/transport/amqp.md @@ -32,13 +32,14 @@ use Enqueue\AmqpExt\AmqpConnectionFactory; $connectionFactory = new AmqpConnectionFactory(); // same as above -$connectionFactory = new AmqpConnectionFactory('amqp://'); +$factory = new AmqpConnectionFactory('amqp:'); +$factory = new AmqpConnectionFactory('amqp+ext:'); // same as above -$connectionFactory = new AmqpConnectionFactory([]); +$factory = new AmqpConnectionFactory([]); // connect to AMQP broker at example.com -$connectionFactory = new AmqpConnectionFactory([ +$factory = new AmqpConnectionFactory([ 'host' => 'example.com', 'port' => 1000, 'vhost' => '/', @@ -48,9 +49,9 @@ $connectionFactory = new AmqpConnectionFactory([ ]); // same as above but given as DSN string -$connectionFactory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); +$factory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); -$psrContext = $connectionFactory->createContext(); +$psrContext = $factory->createContext(); ``` ## Declare topic. diff --git a/docs/transport/amqp_bunny.md b/docs/transport/amqp_bunny.md index 28c78c8cc..37d7b26a9 100644 --- a/docs/transport/amqp_bunny.md +++ b/docs/transport/amqp_bunny.md @@ -29,16 +29,17 @@ $ composer require enqueue/amqp-bunny use Enqueue\AmqpBunny\AmqpConnectionFactory; // connects to localhost -$connectionFactory = new AmqpConnectionFactory(); +$factory = new AmqpConnectionFactory(); // same as above -$connectionFactory = new AmqpConnectionFactory('amqp://'); +$factory = new AmqpConnectionFactory('amqp:'); +$factory = new AmqpConnectionFactory('amqp+bunny:'); // same as above -$connectionFactory = new AmqpConnectionFactory([]); +$factory = new AmqpConnectionFactory([]); // connect to AMQP broker at example.com -$connectionFactory = new AmqpConnectionFactory([ +$factory = new AmqpConnectionFactory([ 'host' => 'example.com', 'port' => 1000, 'vhost' => '/', @@ -48,9 +49,9 @@ $connectionFactory = new AmqpConnectionFactory([ ]); // same as above but given as DSN string -$connectionFactory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); +$factory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); -$psrContext = $connectionFactory->createContext(); +$psrContext = $factory->createContext(); ``` ## Declare topic. diff --git a/docs/transport/amqp_lib.md b/docs/transport/amqp_lib.md index 59061352d..34e297222 100644 --- a/docs/transport/amqp_lib.md +++ b/docs/transport/amqp_lib.md @@ -29,16 +29,17 @@ $ composer require enqueue/amqp-lib use Enqueue\AmqpLib\AmqpConnectionFactory; // connects to localhost -$connectionFactory = new AmqpConnectionFactory(); +$factory = new AmqpConnectionFactory(); // same as above -$connectionFactory = new AmqpConnectionFactory('amqp://'); +$factory = new AmqpConnectionFactory('amqp:'); +$factory = new AmqpConnectionFactory('amqp+lib:'); // same as above -$connectionFactory = new AmqpConnectionFactory([]); +$factory = new AmqpConnectionFactory([]); // connect to AMQP broker at example.com -$connectionFactory = new AmqpConnectionFactory([ +$factory = new AmqpConnectionFactory([ 'host' => 'example.com', 'port' => 1000, 'vhost' => '/', @@ -48,9 +49,9 @@ $connectionFactory = new AmqpConnectionFactory([ ]); // same as above but given as DSN string -$connectionFactory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); +$factory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); -$psrContext = $connectionFactory->createContext(); +$psrContext = $factory->createContext(); ``` ## Declare topic. diff --git a/docs/transport/dbal.md b/docs/transport/dbal.md index 9f39bc729..d4ff3f219 100644 --- a/docs/transport/dbal.md +++ b/docs/transport/dbal.md @@ -28,6 +28,9 @@ use Enqueue\Dbal\DbalConnectionFactory; $factory = new DbalConnectionFactory('mysql://user:pass@localhost:3306/mqdev'); +// connects to localhost +$factory = new DbalConnectionFactory('mysql:'); + $psrContext = $factory->createContext(); ``` diff --git a/docs/transport/filesystem.md b/docs/transport/filesystem.md index 3b529eadc..1eddedceb 100644 --- a/docs/transport/filesystem.md +++ b/docs/transport/filesystem.md @@ -29,7 +29,7 @@ use Enqueue\Fs\FsConnectionFactory; $connectionFactory = new FsConnectionFactory(); // same as above -$connectionFactory = new FsConnectionFactory('file://'); +$connectionFactory = new FsConnectionFactory('file:'); // stores in custom folder $connectionFactory = new FsConnectionFactory('/path/to/queue/dir'); diff --git a/docs/transport/gearman.md b/docs/transport/gearman.md index a9dc0dc24..412e0beca 100644 --- a/docs/transport/gearman.md +++ b/docs/transport/gearman.md @@ -26,7 +26,7 @@ use Enqueue\Gearman\GearmanConnectionFactory; $factory = new GearmanConnectionFactory(); // same as above -$factory = new GearmanConnectionFactory('gearman://'); +$factory = new GearmanConnectionFactory('gearman:'); // connects to example host and port 5555 $factory = new GearmanConnectionFactory('gearman://example:5555'); diff --git a/docs/transport/gps.md b/docs/transport/gps.md index f124feb9b..2a7496ab0 100644 --- a/docs/transport/gps.md +++ b/docs/transport/gps.md @@ -27,6 +27,9 @@ putenv('PUBSUB_EMULATOR_HOST=http://localhost:8900'); $connectionFactory = new GpsConnectionFactory(); +// save as above +$connectionFactory = new GpsConnectionFactory('gps:'); + $psrContext = $connectionFactory->createContext(); ``` diff --git a/docs/transport/kafka.md b/docs/transport/kafka.md index 951fb2591..aac6bbca0 100644 --- a/docs/transport/kafka.md +++ b/docs/transport/kafka.md @@ -25,7 +25,7 @@ use Enqueue\RdKafka\RdKafkaConnectionFactory; $connectionFactory = new RdKafkaConnectionFactory(); // same as above -$connectionFactory = new RdKafkaConnectionFactory('rdkafka:'); +$connectionFactory = new RdKafkaConnectionFactory('kafka:'); // same as above $connectionFactory = new RdKafkaConnectionFactory([]); diff --git a/docs/transport/pheanstalk.md b/docs/transport/pheanstalk.md index 559517c00..d1c2b0400 100644 --- a/docs/transport/pheanstalk.md +++ b/docs/transport/pheanstalk.md @@ -26,7 +26,7 @@ use Enqueue\Pheanstalk\PheanstalkConnectionFactory; $factory = new PheanstalkConnectionFactory(); // same as above -$factory = new PheanstalkConnectionFactory('beanstalk://'); +$factory = new PheanstalkConnectionFactory('beanstalk:'); // connects to example host and port 5555 $factory = new PheanstalkConnectionFactory('beanstalk://example:5555'); From 387b1da1dd49d647549c49fdc87b7056055cb157 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 25 Sep 2017 16:01:51 +0300 Subject: [PATCH 3/5] [doc] add dsn_to_context usage example --- docs/transport/amqp.md | 5 ++++- docs/transport/amqp_bunny.md | 5 ++++- docs/transport/amqp_lib.md | 5 ++++- docs/transport/dbal.md | 3 +++ docs/transport/filesystem.md | 3 +++ docs/transport/gearman.md | 5 +++++ docs/transport/gps.md | 3 +++ docs/transport/kafka.md | 3 +++ docs/transport/pheanstalk.md | 5 +++++ docs/transport/redis.md | 3 +++ docs/transport/sqs.md | 3 +++ docs/transport/stomp.md | 3 +++ 12 files changed, 43 insertions(+), 3 deletions(-) diff --git a/docs/transport/amqp.md b/docs/transport/amqp.md index 065f307af..fcfdd1436 100644 --- a/docs/transport/amqp.md +++ b/docs/transport/amqp.md @@ -33,7 +33,6 @@ $connectionFactory = new AmqpConnectionFactory(); // same as above $factory = new AmqpConnectionFactory('amqp:'); -$factory = new AmqpConnectionFactory('amqp+ext:'); // same as above $factory = new AmqpConnectionFactory([]); @@ -52,6 +51,10 @@ $factory = new AmqpConnectionFactory([ $factory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); $psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('amqp:'); +$psrContext = \Enqueue\dsn_to_context('amqp+ext:'); ``` ## Declare topic. diff --git a/docs/transport/amqp_bunny.md b/docs/transport/amqp_bunny.md index 37d7b26a9..09b59336e 100644 --- a/docs/transport/amqp_bunny.md +++ b/docs/transport/amqp_bunny.md @@ -33,7 +33,6 @@ $factory = new AmqpConnectionFactory(); // same as above $factory = new AmqpConnectionFactory('amqp:'); -$factory = new AmqpConnectionFactory('amqp+bunny:'); // same as above $factory = new AmqpConnectionFactory([]); @@ -52,6 +51,10 @@ $factory = new AmqpConnectionFactory([ $factory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); $psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('amqp:'); +$psrContext = \Enqueue\dsn_to_context('amqp+bunny:'); ``` ## Declare topic. diff --git a/docs/transport/amqp_lib.md b/docs/transport/amqp_lib.md index 34e297222..8ce661433 100644 --- a/docs/transport/amqp_lib.md +++ b/docs/transport/amqp_lib.md @@ -33,7 +33,6 @@ $factory = new AmqpConnectionFactory(); // same as above $factory = new AmqpConnectionFactory('amqp:'); -$factory = new AmqpConnectionFactory('amqp+lib:'); // same as above $factory = new AmqpConnectionFactory([]); @@ -52,6 +51,10 @@ $factory = new AmqpConnectionFactory([ $factory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f'); $psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('amqp:'); +$psrContext = \Enqueue\dsn_to_context('amqp+lib:'); ``` ## Declare topic. diff --git a/docs/transport/dbal.md b/docs/transport/dbal.md index d4ff3f219..2d84782e1 100644 --- a/docs/transport/dbal.md +++ b/docs/transport/dbal.md @@ -48,6 +48,9 @@ $factory = new ManagerRegistryConnectionFactory($registry, [ ]); $psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('mysql:'); ``` ## Init database diff --git a/docs/transport/filesystem.md b/docs/transport/filesystem.md index 1eddedceb..825b7b5a2 100644 --- a/docs/transport/filesystem.md +++ b/docs/transport/filesystem.md @@ -47,6 +47,9 @@ $connectionFactory = new FsConnectionFactory([ ]); $psrContext = $connectionFactory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('file:'); ``` ## Send message to topic diff --git a/docs/transport/gearman.md b/docs/transport/gearman.md index 412e0beca..0161048d5 100644 --- a/docs/transport/gearman.md +++ b/docs/transport/gearman.md @@ -36,6 +36,11 @@ $factory = new GearmanConnectionFactory([ 'host' => 'example', 'port' => 5555 ]); + +$psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('gearman:'); ``` ## Send message to topic diff --git a/docs/transport/gps.md b/docs/transport/gps.md index 2a7496ab0..7d0197f23 100644 --- a/docs/transport/gps.md +++ b/docs/transport/gps.md @@ -31,6 +31,9 @@ $connectionFactory = new GpsConnectionFactory(); $connectionFactory = new GpsConnectionFactory('gps:'); $psrContext = $connectionFactory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('gps:'); ``` ## Send message to topic diff --git a/docs/transport/kafka.md b/docs/transport/kafka.md index aac6bbca0..efc564fa2 100644 --- a/docs/transport/kafka.md +++ b/docs/transport/kafka.md @@ -43,6 +43,9 @@ $connectionFactory = new RdKafkaConnectionFactory([ ]); $psrContext = $connectionFactory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('kafka:'); ``` ## Send message to topic diff --git a/docs/transport/pheanstalk.md b/docs/transport/pheanstalk.md index d1c2b0400..4371c2966 100644 --- a/docs/transport/pheanstalk.md +++ b/docs/transport/pheanstalk.md @@ -36,6 +36,11 @@ $factory = new PheanstalkConnectionFactory([ 'host' => 'example', 'port' => 5555 ]); + +$psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('beanstalk:'); ``` ## Send message to topic diff --git a/docs/transport/redis.md b/docs/transport/redis.md index 3ded64a36..0b362217a 100644 --- a/docs/transport/redis.md +++ b/docs/transport/redis.md @@ -58,6 +58,9 @@ $factory = new RedisConnectionFactory([ $factory = new RedisConnectionFactory('redis://example.com:1000?vendor=phpredis'); $psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('redis:'); ``` * With predis library: diff --git a/docs/transport/sqs.md b/docs/transport/sqs.md index 2c35c8bc3..03da0ebeb 100644 --- a/docs/transport/sqs.md +++ b/docs/transport/sqs.md @@ -33,6 +33,9 @@ $factory = new SqsConnectionFactory([ $factory = new SqsConnectionFactory('sqs:?key=aKey&secret=aSecret®ion=aRegion'); $psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('sqs:'); ``` ## Declare queue. diff --git a/docs/transport/stomp.md b/docs/transport/stomp.md index 1b40f96d1..fb9ea7694 100644 --- a/docs/transport/stomp.md +++ b/docs/transport/stomp.md @@ -38,6 +38,9 @@ $factory = new StompConnectionFactory([ $factory = new StompConnectionFactory('stomp://example.com:1000?login=theLogin'); $psrContext = $factory->createContext(); + +// if you have enqueue/enqueue library installed you can use a function from there to create the context +$psrContext = \Enqueue\dsn_to_context('stomp:'); ``` ## Send message to topic From 69674da46304534aeae3f7ab863229930b34358e Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 25 Sep 2017 16:26:40 +0300 Subject: [PATCH 4/5] [dsn] fix tests. clean up docs. --- docs/bundle/quick_tour.md | 4 +- docs/client/quick_tour.md | 2 +- docs/client/rpc_call.md | 101 +++++++++--------- docs/laravel/queues.md | 2 +- docs/quick_tour.md | 6 +- .../Tests/AmqpConnectionFactoryConfigTest.php | 2 +- .../Tests/AmqpConnectionFactoryConfigTest.php | 2 +- .../Tests/AmqpConnectionFactoryConfigTest.php | 2 +- .../Symfony/DefaultTransportFactory.php | 2 +- .../Functions/DsnToContextFunctionTest.php | 6 +- .../Symfony/DefaultTransportFactoryTest.php | 16 +-- pkg/simple-client/SimpleClient.php | 4 +- 12 files changed, 75 insertions(+), 74 deletions(-) diff --git a/docs/bundle/quick_tour.md b/docs/bundle/quick_tour.md index 9bd5e4b0d..cb9b0dcdf 100644 --- a/docs/bundle/quick_tour.md +++ b/docs/bundle/quick_tour.md @@ -6,7 +6,7 @@ It adds easy to use [configuration layer](config_reference.md), register service ## Install ```bash -$ composer require enqueue/enqueue-bundle enqueue/amqp-ext +$ composer require enqueue/enqueue-bundle enqueue/amqp-ext # or enqueue/amqp-bunny, enqueue/amqp-lib ``` _**Note**: You could use not only AMQP transport but other available: STOMP, Amazon SQS, Redis, Filesystem, Doctrine DBAL and others._ @@ -47,7 +47,7 @@ First, you have to configure a transport layer and set one to be default. enqueue: transport: - default: "amqp://" + default: "amqp:" client: ~ ``` diff --git a/docs/client/quick_tour.md b/docs/client/quick_tour.md index a5a4a3ed3..35e59537f 100644 --- a/docs/client/quick_tour.md +++ b/docs/client/quick_tour.md @@ -22,7 +22,7 @@ use Enqueue\SimpleClient\SimpleClient; include __DIR__.'/vendor/autoload.php'; -$client = new SimpleClient('amqp://'); +$client = new SimpleClient('amqp:'); ``` ## Produce message diff --git a/docs/client/rpc_call.md b/docs/client/rpc_call.md index 94a964d30..4afb1e722 100644 --- a/docs/client/rpc_call.md +++ b/docs/client/rpc_call.md @@ -1,55 +1,14 @@ # Client. RPC call The client's [quick tour](quick_tour.md) describes how to get the client object. -Here we'll use `Enqueue\SimpleClient\SimpleClient` though it is not required. -You can get all that stuff from manually built client or get objects from a container (Symfony). +Here we'll show you how to use Enqueue Client to perform a [RPC call](https://en.wikipedia.org/wiki/Remote_procedure_call). +You can do it by defining a command which returns something. -The simple client could be created like this: +## The consumer side -## The client side +On the consumer side we have to register a command processor which computes the result and send it back to the sender. +Pay attention that you have to add reply extension. It wont work without it. -There is a handy class RpcClient shipped with the client component. -It allows you to easily perform [RPC calls](https://en.wikipedia.org/wiki/Remote_procedure_call). -It send a message and wait for a reply. - -```php -getProducer(), $context); - -$replyMessage = $rpcClient->call('greeting_topic', 'Hi Thomas!', 5); -``` - -You can perform several requests asynchronously with `callAsync` and request replays later. - -```php -getProducer(), $context); - -$promises = []; -$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5); -$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5); -$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5); -$promises[] = $rpcClient->callAsync('greeting_topic', 'Hi Thomas!', 5); - -$replyMessages = []; -foreach ($promises as $promise) { - $replyMessages[] = $promise->receive(); -} -``` - -## The server side - -On the server side you may register a processor which returns a result object with a reply message set. Of course it is possible to implement rpc server side based on transport classes only. That would require a bit more work to do. ```php @@ -60,19 +19,61 @@ use Interop\Queue\PsrContext; use Enqueue\Consumption\Result; use Enqueue\Consumption\ChainExtension; use Enqueue\Consumption\Extension\ReplyExtension; +use Enqueue\Client\Config; use Enqueue\SimpleClient\SimpleClient; /** @var \Interop\Queue\PsrContext $context */ -$client = new SimpleClient('amqp://'); +// composer require enqueue/amqp-ext # or enqueue/amqp-bunny, enqueue/amqp-lib +$client = new SimpleClient('amqp:'); -$client->bind('greeting_topic', 'greeting_processor', function (PsrMessage $message, PsrContext $context) use (&$requestMessage) { - echo $message->getBody(); +$client->bind(Config::COMMAND_TOPIC, 'square', function (PsrMessage $message, PsrContext $context) use (&$requestMessage) { + $number = (int) $message->getBody(); - return Result::reply($context->createMessage('Hi there! I am John.')); + return Result::reply($context->createMessage($number ^ 2)); }); $client->consume(new ChainExtension([new ReplyExtension()])); ``` [back to index](../index.md) + +## The sender side + +On the sender's side we need a client which send a command and wait for reply messages. + +```php +sendCommand('square', 5, true)->receive(5000 /* 5 sec */)->getBody(); +``` + +You can perform several requests asynchronously with `sendCommand` and ask for replays later. + +```php +sendCommand('square', 5, true); +$promises[] = $client->sendCommand('square', 10, true); +$promises[] = $client->sendCommand('square', 7, true); +$promises[] = $client->sendCommand('square', 12, true); + +$replyMessages = []; +while ($promises) { + foreach ($promises as $index => $promise) { + if ($replyMessage = $promise->receiveNoWait()) { + $replyMessages[$index] = $replyMessage; + + unset($promises[$index]); + } + } +} +``` \ No newline at end of file diff --git a/docs/laravel/queues.md b/docs/laravel/queues.md index a49b7832c..ed9dd9574 100644 --- a/docs/laravel/queues.md +++ b/docs/laravel/queues.md @@ -70,7 +70,7 @@ return [ 'connection_factory_class' => \Enqueue\AmqpBunny\AmqpConnectionFactory::class, // connects to localhost - 'dsn' => 'amqp://', + 'dsn' => 'amqp:', // could be "rabbitmq_dlx", "rabbitmq_delay_plugin", instance of DelayStrategy interface or null // 'delay_strategy' => 'rabbitmq_dlx' diff --git a/docs/quick_tour.md b/docs/quick_tour.md index 0be831500..9a5eb4fa9 100644 --- a/docs/quick_tour.md +++ b/docs/quick_tour.md @@ -171,7 +171,7 @@ use Enqueue\SimpleClient\SimpleClient; use Interop\Queue\PsrMessage; // composer require enqueue/amqp-ext -$client = new SimpleClient('amqp://'); +$client = new SimpleClient('amqp:'); // composer require enqueue/fs $client = new SimpleClient('file://foo/bar'); @@ -197,8 +197,8 @@ use Enqueue\Client\Config; use Enqueue\Consumption\Extension\ReplyExtension; use Enqueue\Consumption\Result; -// composer require enqueue/amqp-ext -$client = new SimpleClient('amqp://'); +// composer require enqueue/amqp-ext # or enqueue/amqp-bunny or enqueue/amqp-lib +$client = new SimpleClient('amqp:'); // composer require enqueue/fs $client = new SimpleClient('file://foo/bar'); diff --git a/pkg/amqp-bunny/Tests/AmqpConnectionFactoryConfigTest.php b/pkg/amqp-bunny/Tests/AmqpConnectionFactoryConfigTest.php index 4f761fd57..376ca33c4 100644 --- a/pkg/amqp-bunny/Tests/AmqpConnectionFactoryConfigTest.php +++ b/pkg/amqp-bunny/Tests/AmqpConnectionFactoryConfigTest.php @@ -148,7 +148,7 @@ public static function provideConfigs() ]; yield [ - 'amqp://', + 'amqp:', [ 'host' => 'localhost', 'port' => 5672, diff --git a/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php b/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php index 99950be4a..2cbbc2022 100644 --- a/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php +++ b/pkg/amqp-ext/Tests/AmqpConnectionFactoryConfigTest.php @@ -158,7 +158,7 @@ public static function provideConfigs() ]; yield [ - 'amqp://', + 'amqp:', [ 'host' => 'localhost', 'port' => 5672, diff --git a/pkg/amqp-lib/Tests/AmqpConnectionFactoryConfigTest.php b/pkg/amqp-lib/Tests/AmqpConnectionFactoryConfigTest.php index 50dcc12cc..48eb0d424 100644 --- a/pkg/amqp-lib/Tests/AmqpConnectionFactoryConfigTest.php +++ b/pkg/amqp-lib/Tests/AmqpConnectionFactoryConfigTest.php @@ -198,7 +198,7 @@ public static function provideConfigs() ]; yield [ - 'amqp://', + 'amqp:', [ 'host' => 'localhost', 'port' => 5672, diff --git a/pkg/enqueue/Symfony/DefaultTransportFactory.php b/pkg/enqueue/Symfony/DefaultTransportFactory.php index b79499c9c..2e3344488 100644 --- a/pkg/enqueue/Symfony/DefaultTransportFactory.php +++ b/pkg/enqueue/Symfony/DefaultTransportFactory.php @@ -58,7 +58,7 @@ public function addConfiguration(ArrayNodeDefinition $builder) } if (empty($v)) { - return ['dsn' => 'null://']; + return ['dsn' => 'null:']; } if (is_string($v)) { diff --git a/pkg/enqueue/Tests/Functions/DsnToContextFunctionTest.php b/pkg/enqueue/Tests/Functions/DsnToContextFunctionTest.php index 4e8b4c601..2a8bc83bc 100644 --- a/pkg/enqueue/Tests/Functions/DsnToContextFunctionTest.php +++ b/pkg/enqueue/Tests/Functions/DsnToContextFunctionTest.php @@ -52,15 +52,15 @@ public function testReturnsExpectedFactoryInstance($dsn, $expectedFactoryClass) public static function provideDSNs() { - yield ['amqp://', AmqpContext::class]; + yield ['amqp:', AmqpContext::class]; yield ['amqp://user:pass@foo/vhost', AmqpContext::class]; - yield ['file://', FsContext::class]; + yield ['file:', FsContext::class]; yield ['file://'.sys_get_temp_dir(), FsContext::class]; - yield ['null://', NullContext::class]; + yield ['null:', NullContext::class]; yield ['redis:', RedisContext::class]; diff --git a/pkg/enqueue/Tests/Symfony/DefaultTransportFactoryTest.php b/pkg/enqueue/Tests/Symfony/DefaultTransportFactoryTest.php index 5ee18c211..1c7b0bd5c 100644 --- a/pkg/enqueue/Tests/Symfony/DefaultTransportFactoryTest.php +++ b/pkg/enqueue/Tests/Symfony/DefaultTransportFactoryTest.php @@ -82,10 +82,10 @@ public function testShouldSetNullTransportByDefault() $processor = new Processor(); $config = $processor->process($tb->buildTree(), [null]); - $this->assertEquals(['dsn' => 'null://'], $config); + $this->assertEquals(['dsn' => 'null:'], $config); $config = $processor->process($tb->buildTree(), ['']); - $this->assertEquals(['dsn' => 'null://'], $config); + $this->assertEquals(['dsn' => 'null:'], $config); } public function testThrowIfNeitherDsnNorAliasConfigured() @@ -250,19 +250,19 @@ public function testShouldCreateDriverFromDsn($dsn, $expectedName) public static function provideDSNs() { - yield ['amqp+ext://', 'default_amqp_ext']; + yield ['amqp+ext:', 'default_amqp_ext']; yield ['amqp+lib:', 'default_amqp_lib']; - yield ['amqp+bunny://', 'default_amqp_bunny']; + yield ['amqp+bunny:', 'default_amqp_bunny']; - yield ['null://', 'default_null']; + yield ['null:', 'default_null']; - yield ['file://', 'default_fs']; + yield ['file:', 'default_fs']; - yield ['mysql://', 'default_dbal']; + yield ['mysql:', 'default_dbal']; - yield ['pgsql://', 'default_dbal']; + yield ['pgsql:', 'default_dbal']; yield ['gps:', 'default_gps']; diff --git a/pkg/simple-client/SimpleClient.php b/pkg/simple-client/SimpleClient.php index cd7dc0793..14afa8e54 100644 --- a/pkg/simple-client/SimpleClient.php +++ b/pkg/simple-client/SimpleClient.php @@ -37,10 +37,10 @@ final class SimpleClient /** * The config could be a transport DSN (string) or an array, here's an example of a few DSNs:. * - * amqp:// + * amqp: * amqp://guest:guest@localhost:5672/%2f?lazy=1&persisted=1 * file://foo/bar/ - * null:// + * null: * * or an array, the most simple: * From 240f932acaa815f050b2a5d2bda0ec5452e6fb9b Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 25 Sep 2017 16:33:02 +0300 Subject: [PATCH 5/5] fix tests. --- pkg/fs/Tests/FsConnectionFactoryConfigTest.php | 4 ++-- pkg/gearman/Tests/GearmanConnectionFactoryConfigTest.php | 2 +- .../Tests/PheanstalkConnectionFactoryConfigTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/fs/Tests/FsConnectionFactoryConfigTest.php b/pkg/fs/Tests/FsConnectionFactoryConfigTest.php index a42ede8c6..c79581c51 100644 --- a/pkg/fs/Tests/FsConnectionFactoryConfigTest.php +++ b/pkg/fs/Tests/FsConnectionFactoryConfigTest.php @@ -24,7 +24,7 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig() public function testThrowIfSchemeIsNotAmqp() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "file://'); + $this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "file:'); new FsConnectionFactory('http://example.com'); } @@ -83,7 +83,7 @@ public static function provideConfigs() ]; yield [ - 'file://', + 'file:', [ 'path' => sys_get_temp_dir().'/enqueue', 'pre_fetch_count' => 1, diff --git a/pkg/gearman/Tests/GearmanConnectionFactoryConfigTest.php b/pkg/gearman/Tests/GearmanConnectionFactoryConfigTest.php index 75c61591e..bb8ab7ad6 100644 --- a/pkg/gearman/Tests/GearmanConnectionFactoryConfigTest.php +++ b/pkg/gearman/Tests/GearmanConnectionFactoryConfigTest.php @@ -62,7 +62,7 @@ public static function provideConfigs() ]; yield [ - 'gearman://', + 'gearman:', [ 'host' => 'localhost', 'port' => 4730, diff --git a/pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php b/pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php index 239dae68f..231ec0d77 100644 --- a/pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php +++ b/pkg/pheanstalk/Tests/PheanstalkConnectionFactoryConfigTest.php @@ -63,7 +63,7 @@ public static function provideConfigs() ]; yield [ - 'beanstalk://', + 'beanstalk:', [ 'host' => 'localhost', 'port' => 11300,