forked from ruflin/Elastica
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support DSN in Client constructor for config argument (ruflin#1640)
- Loading branch information
1 parent
5f71248
commit 638bbed
Showing
5 changed files
with
334 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
<?php | ||
|
||
namespace Elastica; | ||
|
||
use Elastica\Exception\InvalidException; | ||
|
||
/** | ||
* Dns parser and configuration exporter to Elastica Client. | ||
* | ||
* @author Antoine Lamirault <lamiraultantoine@gmail.com> | ||
*/ | ||
class Dsn | ||
{ | ||
/** | ||
* @var string|null | ||
*/ | ||
private $host; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $port; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $url; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $proxy; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $transport; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $persistent; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
private $timeout; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $connections; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $roundRobin; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $log; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $retryOnConflict; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $bigintConversion; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $username; | ||
|
||
/** | ||
* @var string|null | ||
*/ | ||
private $password; | ||
|
||
/** | ||
* Dsn constructor. | ||
* | ||
* @param string|null $host | ||
* @param string|null $port | ||
* @param string|null $url | ||
* @param string|null $path | ||
* @param string|null $proxy | ||
* @param string|null $transport | ||
* @param bool $persistent | ||
* @param int|null $timeout | ||
* @param array $connections | ||
* @param bool $roundRobin | ||
* @param bool $log | ||
* @param int $retryOnConflict | ||
* @param bool $bigintConversion | ||
* @param string|null $username | ||
* @param string|null $password | ||
*/ | ||
public function __construct(?string $host, ?string $port, ?string $url, ?string $path, ?string $proxy, | ||
?string $transport, bool $persistent, ?int $timeout, array $connections, | ||
bool $roundRobin, bool $log, int $retryOnConflict, bool $bigintConversion, | ||
?string $username, ?string $password) | ||
{ | ||
$this->host = $host; | ||
$this->port = $port; | ||
$this->url = $url; | ||
$this->path = $path; | ||
$this->proxy = $proxy; | ||
$this->transport = $transport; | ||
$this->persistent = $persistent; | ||
$this->timeout = $timeout; | ||
$this->connections = $connections; | ||
$this->roundRobin = $roundRobin; | ||
$this->log = $log; | ||
$this->retryOnConflict = $retryOnConflict; | ||
$this->bigintConversion = $bigintConversion; | ||
$this->username = $username; | ||
$this->password = $password; | ||
} | ||
|
||
public static function fromString(string $dsn): self | ||
{ | ||
if (false === $parsedDsn = \parse_url($dsn)) { | ||
throw new InvalidException(\sprintf("DSN '%s' is invalid.", $dsn)); | ||
} | ||
|
||
$transport = $parsedDsn['scheme'] ?? null; | ||
$host = $parsedDsn['host'] ?? null; | ||
$username = isset($parsedDsn['user']) ? \urldecode($parsedDsn['user']) : null; | ||
$password = isset($parsedDsn['pass']) ? \urldecode($parsedDsn['pass']) : null; | ||
$port = $parsedDsn['port'] ?? null; | ||
$path = $parsedDsn['path'] ?? null; | ||
|
||
$options = []; | ||
if (isset($parsedDsn['query'])) { | ||
\parse_str($parsedDsn['query'], $options); | ||
} | ||
|
||
$proxy = null; | ||
if (isset($options['proxy'])) { | ||
$proxy = $options['proxy']; | ||
} | ||
|
||
$persistent = true; | ||
if (isset($options['persistent']) && ('false' === $options['persistent'])) { | ||
$persistent = false; | ||
} | ||
|
||
$timeout = null; | ||
if (isset($options['timeout'])) { | ||
$timeout = (int) $options['timeout']; | ||
} | ||
|
||
$roundRobin = false; | ||
if (isset($options['roundRobin']) && ('true' === $options['roundRobin'])) { | ||
$roundRobin = true; | ||
} | ||
|
||
$log = false; | ||
if (isset($options['log']) && ('true' === $options['log'])) { | ||
$log = true; | ||
} | ||
|
||
$retryOnConflict = 0; | ||
if (isset($options['retryOnConflict'])) { | ||
$retryOnConflict = (int) $options['retryOnConflict']; | ||
} | ||
|
||
$bigintConversion = false; | ||
if (isset($options['bigintConversion']) && ('true' === $options['bigintConversion'])) { | ||
$bigintConversion = true; | ||
} | ||
|
||
return new self( | ||
$host, | ||
$port, | ||
null, | ||
$path, | ||
$proxy, | ||
$transport, | ||
$persistent, | ||
$timeout, | ||
[], | ||
$roundRobin, | ||
$log, | ||
$retryOnConflict, | ||
$bigintConversion, | ||
$username, | ||
$password | ||
); | ||
} | ||
|
||
public function getConfiguration(): array | ||
{ | ||
return [ | ||
'host' => $this->host, | ||
'port' => $this->port, | ||
'path' => $this->path, | ||
'url' => $this->url, | ||
'proxy' => $this->proxy, | ||
'transport' => $this->transport, | ||
'persistent' => $this->persistent, | ||
'timeout' => $this->timeout, | ||
'connections' => $this->connections, | ||
'roundRobin' => $this->roundRobin, | ||
'log' => $this->log, | ||
'retryOnConflict' => $this->retryOnConflict, | ||
'bigintConversion' => $this->bigintConversion, | ||
'username' => $this->username, | ||
'password' => $this->password, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Elastica\Test; | ||
|
||
use Elastica\Dsn; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @group unit | ||
*/ | ||
class DsnTest extends TestCase | ||
{ | ||
public function testInvalidDsn() | ||
{ | ||
$this->expectException(\Elastica\Exception\InvalidException::class); | ||
$this->expectExceptionMessage("DSN 'test:0' is invalid."); | ||
|
||
Dsn::fromString('test:0'); | ||
} | ||
|
||
public function testSimpleDsn() | ||
{ | ||
$dsn = Dsn::fromString('192.168.1.1:9201'); | ||
|
||
$expected = [ | ||
'host' => '192.168.1.1', | ||
'port' => '9201', | ||
'path' => null, | ||
'url' => null, | ||
'proxy' => null, | ||
'transport' => null, | ||
'persistent' => true, | ||
'timeout' => null, | ||
'connections' => [], | ||
'roundRobin' => false, | ||
'log' => false, | ||
'retryOnConflict' => 0, | ||
'bigintConversion' => false, | ||
'username' => null, | ||
'password' => null, | ||
]; | ||
|
||
$this->assertEquals($expected, $dsn->getConfiguration()); | ||
} | ||
|
||
public function testDsnWithParameters() | ||
{ | ||
$dsn = Dsn::fromString('https://user:p4ss@foo.com:9201/my-path?proxy=https://proxy.com&persistent=false&timeout=45&roundRobin=true&log=true&retryOnConflict=2&bigintConversion=true'); | ||
$expected = [ | ||
'host' => 'foo.com', | ||
'port' => '9201', | ||
'path' => '/my-path', | ||
'url' => null, | ||
'proxy' => 'https://proxy.com', | ||
'transport' => 'https', | ||
'persistent' => false, | ||
'timeout' => 45, | ||
'connections' => [], | ||
'roundRobin' => true, | ||
'log' => true, | ||
'retryOnConflict' => 2, | ||
'bigintConversion' => true, | ||
'username' => 'user', | ||
'password' => 'p4ss', | ||
]; | ||
|
||
$this->assertEquals($expected, $dsn->getConfiguration()); | ||
} | ||
} |