Skip to content

Commit

Permalink
Merge 4d15d4e into 6b9d97f
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Nov 11, 2018
2 parents 6b9d97f + 4d15d4e commit 5f15db8
Show file tree
Hide file tree
Showing 22 changed files with 666 additions and 359 deletions.
1 change: 0 additions & 1 deletion examples/demo-async-cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
]);

$connection = EventStoreAsyncConnectionFactory::createFromSettings(
null,
$builder->build(),
'cluster-connection'
);
Expand Down
1 change: 0 additions & 1 deletion examples/demo-async-dns-cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
$builder->setClusterGossipPort(2113);

$connection = EventStoreAsyncConnectionFactory::createFromSettings(
null,
$builder->build(),
'dns-cluster-connection'
);
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-async-with-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$builder->enableVerboseLogging();
$builder->useConsoleLogger();

$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113),
$builder->build()
);
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-async.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
require __DIR__ . '/../vendor/autoload.php';

Loop::run(function () {
$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);

Expand Down
2 changes: 1 addition & 1 deletion examples/demo-subscription-with-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$builder->enableVerboseLogging();
$builder->useConsoleLogger();

$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113),
$builder->build()
);
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-subscription1.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
require __DIR__ . '/../vendor/autoload.php';

Loop::run(function () {
$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);

Expand Down
2 changes: 1 addition & 1 deletion examples/demo-subscription2.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
require __DIR__ . '/../vendor/autoload.php';

Loop::run(function () {
$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);

Expand Down
2 changes: 1 addition & 1 deletion examples/demo-subscription3.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
require __DIR__ . '/../vendor/autoload.php';

Loop::run(function () {
$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);

Expand Down
2 changes: 1 addition & 1 deletion examples/demo-subscription4.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
require __DIR__ . '/../vendor/autoload.php';

Loop::run(function () {
$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);

Expand Down
2 changes: 1 addition & 1 deletion examples/demo-subscription5.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
require __DIR__ . '/../vendor/autoload.php';

Loop::run(function () {
$connection = EventStoreAsyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreAsyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);

Expand Down
2 changes: 1 addition & 1 deletion examples/demo-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

require __DIR__ . '/../vendor/autoload.php';

$connection = EventStoreSyncConnectionFactory::createFromSettingsWithEndPoint(
$connection = EventStoreSyncConnectionFactory::createFromEndPoint(
new EndPoint('eventstore', 1113)
);

Expand Down
56 changes: 43 additions & 13 deletions src/ClusterSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,71 @@

namespace Prooph\EventStoreClient;

use Prooph\EventStoreClient\Exception\InvalidArgumentException;

/**
* All times are milliseconds
*/
class ClusterSettings
{
/** @var string */
/** @var string|null */
private $clusterDns;
/** @var int */
private $maxDiscoverAttempts;
/** @var int */
private $externalGossipPort;
/** @var GossipSeed[] */
/** @var GossipSeed[]|null */
private $gossipSeeds;
/** @var int */
private $gossipTimeout;
/** @var bool */
private $preferRandomNode;

public function __construct(
public static function fromGossipSeeds(
array $gossipSeeds,
int $maxDiscoverAttempts,
int $gossipTimeout,
bool $preferRandomNode
): self {
$clusterSettings = new self();

foreach ($gossipSeeds as $gossipSeed) {
if (! $gossipSeed instanceof GossipSeed) {
throw new InvalidArgumentException(\sprintf(
'Expected an array of %s',
GossipSeed::class
));
}

$clusterSettings->gossipSeeds[] = $gossipSeed;
}

$clusterSettings->maxDiscoverAttempts = $maxDiscoverAttempts;
$clusterSettings->gossipTimeout = $gossipTimeout;
$clusterSettings->preferRandomNode = $preferRandomNode;

return $clusterSettings;
}

public static function fromClusterDns(
string $clusterDns,
int $maxDiscoverAttempts,
int $externalGossipPort,
array $gossipSeeds,
int $gossipTimeout,
bool $preferRandomNode
) {
$this->clusterDns = $clusterDns;
$this->maxDiscoverAttempts = $maxDiscoverAttempts;
$this->externalGossipPort = $externalGossipPort;
$this->gossipSeeds = $gossipSeeds;
$this->gossipTimeout = $gossipTimeout;
$this->preferRandomNode = $preferRandomNode;
): self {
$clusterSettings = new self();

$clusterSettings->clusterDns = $clusterDns;
$clusterSettings->maxDiscoverAttempts = $maxDiscoverAttempts;
$clusterSettings->externalGossipPort = $externalGossipPort;
$clusterSettings->gossipTimeout = $gossipTimeout;
$clusterSettings->preferRandomNode = $preferRandomNode;

return $clusterSettings;
}

public function clusterDns(): string
public function clusterDns(): ?string
{
return $this->clusterDns;
}
Expand All @@ -62,7 +92,7 @@ public function externalGossipPort(): int
return $this->externalGossipPort;
}

public function gossipSeeds(): array
public function gossipSeeds(): ?array
{
return $this->gossipSeeds;
}
Expand Down
10 changes: 9 additions & 1 deletion src/ConnectionSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ConnectionSettings
/** @var int */
private $externalGossipPort;
/** @var GossipSeed[] */
private $gossipSeeds;
private $gossipSeeds = [];
/** @var int */
private $gossipTimeout;
/** @var bool */
Expand Down Expand Up @@ -256,4 +256,12 @@ public function clientConnectionTimeout(): int
{
return $this->clientConnectionTimeout;
}

public function withDefaultCredentials(UserCredentials $userCredentials): self
{
$clone = clone $this;
$clone->defaultUserCredentials = $userCredentials;

return $clone;
}
}
167 changes: 167 additions & 0 deletions src/ConnectionString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

/**
* This file is part of `prooph/event-store-client`.
* (c) 2018-2018 prooph software GmbH <contact@prooph.de>
* (c) 2018-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Prooph\EventStoreClient;

use Prooph\EventStoreClient\Exception\InvalidArgumentException;
use ReflectionObject;

class ConnectionString
{
private static $allowedValues = [
'verboselogging' => 'bool',
'maxqueuesize' => 'int',
'maxconcurrentitems' => 'int',
'maxretries' => 'int',
'maxreconnections' => 'int',
'requiremaster' => 'bool',
'reconnectiondelay' => 'int',
'operationtimeout' => 'int',
'operationtimeoutcheckperiod' => 'int',
'defaultusercredentials' => UserCredentials::class,
'usesslconnection' => 'bool',
'targethost' => 'string',
'validateserver' => 'bool',
'failonnoserverresponse' => 'bool',
'heartbeatinterval' => 'int',
'heartbeattimeout' => 'int',
'clusterdns' => 'string',
'maxdiscoverattempts' => 'int',
'externalgossipport' => 'int',
'gossipseeds' => GossipSeed::class,
'gossiptimeout' => 'int',
'preferrandomNode' => 'bool',
'clientconnectiontimeout' => 'int',
];

public static function getConnectionSettings(
string $connectionString,
?ConnectionSettings $settings = null
): ConnectionSettings {
$settings = $settings ?? ConnectionSettings::default();
$reflection = new ReflectionObject($settings);
$properties = $reflection->getProperties();
$values = \explode(';', $connectionString);

foreach ($values as $value) {
list($key, $value) = \explode('=', $value);
$key = \strtolower($key);

if (! \array_key_exists($key, self::$allowedValues)) {
throw new InvalidArgumentException(\sprintf(
'Key %s is not an allowed key in %s',
$key,
__CLASS__
));
}

$type = self::$allowedValues[$key];

switch ($type) {
case 'bool':
$filteredValue = \filter_var($value, \FILTER_VALIDATE_BOOLEAN);
break;
case 'int':
$filteredValue = \filter_var($value, \FILTER_VALIDATE_INT);

if (false === $filteredValue) {
throw new InvalidArgumentException(\sprintf(
'Expected type for key %s is %s, but %s given',
$key,
$type,
$value
));
}
break;
case 'string':
$filteredValue = $value;
break;
case UserCredentials::class:
$exploded = \explode(':', $value);

if (\count($exploded) !== 2) {
throw new InvalidArgumentException(\sprintf(
'Expected user credentials in format user:pass, %s given',
$value
));
}

$filteredValue = new UserCredentials($exploded[0], $exploded[1]);
break;
case GossipSeed::class:
$gossipSeeds = [];

foreach (\explode(',', $value) as $v) {
$exploded = \explode(':', $v);

if (\count($exploded) !== 2) {
throw new InvalidArgumentException(\sprintf(
'Expected user credentials in format user:pass, %s given',
$value
));
}

$host = $exploded[0];
$port = \filter_var($exploded[1], \FILTER_VALIDATE_INT);

if (false === $port) {
throw new InvalidArgumentException(\sprintf(
'Expected type for port of gossip seed is int, but %s given',
$exploded[1]
));
}

$gossipSeeds[] = new GossipSeed(new EndPoint($host, $port));
}

if (empty($gossipSeeds)) {
throw new InvalidArgumentException(\sprintf(
'No gossip seeds specified in connection string'
));
}

$filteredValue = $gossipSeeds;
break;
}

foreach ($properties as $property) {
if (\strtolower($property->getName()) === $key) {
$property->setAccessible(true);
$property->setValue($settings, $filteredValue);
break;
}
}
}

return $settings;
}

public static function getUriFromConnectionString(string $connectionString): ?Uri
{
$values = \explode(';', $connectionString);

if (1 === \count($values)) {
return new Uri($connectionString);
}

foreach ($values as $value) {
list($key, $value) = \explode('=', $value);

if (\strtolower($key) === 'connectto') {
return new Uri($value);
}
}

return null;
}
}

0 comments on commit 5f15db8

Please sign in to comment.