Skip to content

Commit 242cd7e

Browse files
committed
Allow passing Client as configuration option.
Prevent calling database creation if Client driver does not support QueryDriverInterface
1 parent 9ed823c commit 242cd7e

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

InfluxDbStorage.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,17 @@ public function __construct($config = 'influxdb:')
5959
if (empty($config)) {
6060
$config = [];
6161
} elseif (is_string($config)) {
62-
$config = $this->parseDsn($config);
62+
$config = self::parseDsn($config);
6363
} elseif (is_array($config)) {
64-
$config = empty($config['dsn']) ? $config : $this->parseDsn($config['dsn']);
64+
$config = empty($config['dsn']) ? $config : self::parseDsn($config['dsn']);
6565
} elseif ($config instanceof Client) {
66+
// Passing Client instead of array config is deprecated because it prevents setting any configuration values
67+
// and causes library to use defaults.
68+
@trigger_error(
69+
sprintf('Passing %s as %s argument is deprecated. Pass it as "client" array property or use createWithClient instead',
70+
Client::class,
71+
__METHOD__
72+
), E_USER_DEPRECATED);
6673
$this->client = $config;
6774
$config = [];
6875
} else {
@@ -97,6 +104,22 @@ public function __construct($config = 'influxdb:')
97104
$this->config = $config;
98105
}
99106

107+
/**
108+
* @param Client $client
109+
* @param string $config
110+
*
111+
* @return InfluxDbStorage
112+
*/
113+
public static function createWithClient(Client $client, $config = 'influxdb:'): self
114+
{
115+
if (is_string($config)) {
116+
$config = self::parseDsn($config);
117+
}
118+
$config['client'] = $client;
119+
120+
return new static($config);
121+
}
122+
100123
public function pushConsumerStats(ConsumerStats $stats): void
101124
{
102125
$points = [];
@@ -221,7 +244,7 @@ private function getDb(): Database
221244
return $this->database;
222245
}
223246

224-
private function parseDsn(string $dsn): array
247+
private static function parseDsn(string $dsn): array
225248
{
226249
$dsn = Dsn::parseFirst($dsn);
227250

0 commit comments

Comments
 (0)