diff --git a/pkg/mongodb/MongodbConnectionFactory.php b/pkg/mongodb/MongodbConnectionFactory.php index a5148f1c0..3d7624904 100644 --- a/pkg/mongodb/MongodbConnectionFactory.php +++ b/pkg/mongodb/MongodbConnectionFactory.php @@ -27,7 +27,7 @@ class MongodbConnectionFactory implements ConnectionFactory * * or * - * mongodb://127.0.0.1:27017/dbname?polling_interval=1000&enqueue_collection=enqueue + * mongodb://127.0.0.1:27017/defaultauthdb?polling_interval=1000&enqueue_database=enqueue&enqueue_collection=enqueue * * @param array|string|null $config */ @@ -38,7 +38,10 @@ public function __construct($config = 'mongodb:') } elseif (is_string($config)) { $config = $this->parseDsn($config); } elseif (is_array($config)) { - $config = $this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn']); + $config = array_replace( + $config, + $this->parseDsn(empty($config['dsn']) ? 'mongodb:' : $config['dsn']) + ); } else { throw new \LogicException('The config must be either an array of options, a DSN string or null'); } @@ -74,11 +77,7 @@ public static function parseDsn(string $dsn): array 'mongodb' => true, ]; if (false == isset($parsedUrl['scheme'])) { - throw new \LogicException(sprintf( - 'The given DSN schema "%s" is not supported. There are supported schemes: "%s".', - $parsedUrl['scheme'], - implode('", "', array_keys($supported)) - )); + throw new \LogicException(sprintf('The given DSN schema "%s" is not supported. There are supported schemes: "%s".', $parsedUrl['scheme'], implode('", "', array_keys($supported)))); } if ('mongodb:' === $dsn) { return [ @@ -86,6 +85,8 @@ public static function parseDsn(string $dsn): array ]; } $config['dsn'] = $dsn; + // FIXME this is NOT a dbname but rather authdb. But removing this would be a BC break. + // see: https://github.com/php-enqueue/enqueue-dev/issues/1027 if (isset($parsedUrl['path']) && '/' !== $parsedUrl['path']) { $pathParts = explode('/', $parsedUrl['path']); //DB name @@ -103,6 +104,9 @@ public static function parseDsn(string $dsn): array if (!empty($queryParts['enqueue_collection'])) { $config['collection_name'] = $queryParts['enqueue_collection']; } + if (!empty($queryParts['enqueue_database'])) { + $config['dbname'] = $queryParts['enqueue_database']; + } } return $config; diff --git a/pkg/mongodb/MongodbProducer.php b/pkg/mongodb/MongodbProducer.php index d27f014eb..d26696cf8 100644 --- a/pkg/mongodb/MongodbProducer.php +++ b/pkg/mongodb/MongodbProducer.php @@ -77,10 +77,7 @@ public function send(Destination $destination, Message $message): void $delay = $message->getDeliveryDelay(); if ($delay) { if (!is_int($delay)) { - throw new \LogicException(sprintf( - 'Delay must be integer but got: "%s"', - is_object($delay) ? get_class($delay) : gettype($delay) - )); + throw new \LogicException(sprintf('Delay must be integer but got: "%s"', is_object($delay) ? get_class($delay) : gettype($delay))); } if ($delay <= 0) { @@ -93,10 +90,7 @@ public function send(Destination $destination, Message $message): void $timeToLive = $message->getTimeToLive(); if ($timeToLive) { if (!is_int($timeToLive)) { - throw new \LogicException(sprintf( - 'TimeToLive must be integer but got: "%s"', - is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive) - )); + throw new \LogicException(sprintf('TimeToLive must be integer but got: "%s"', is_object($timeToLive) ? get_class($timeToLive) : gettype($timeToLive))); } if ($timeToLive <= 0) { @@ -110,7 +104,7 @@ public function send(Destination $destination, Message $message): void $collection = $this->context->getCollection(); $collection->insertOne($mongoMessage); } catch (\Exception $e) { - throw new Exception('The transport has failed to send the message due to some internal error.', null, $e); + throw new Exception('The transport has failed to send the message due to some internal error.', $e->getCode(), $e); } }