Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provides default dsn value for charset in factories #56

Merged
merged 6 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpunit.xml.pdo_mysql
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
<env name="DB_PASSWORD" value="" />
<env name="DB_NAME" value="event_store_tests" />
<env name="DB_PORT" value="3306"/>
<env name="DB_CHARSET" value="utf8"/>
</php>
</phpunit>
1 change: 1 addition & 0 deletions phpunit.xml.pdo_pgsql
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
<env name="DB_PASSWORD" value="" />
<env name="DB_NAME" value="event_store_tests" />
<env name="DB_PORT" value="5432"/>
<env name="DB_CHARSET" value="utf8"/>
</php>
</phpunit>
29 changes: 7 additions & 22 deletions src/Container/AbstractEventStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,6 @@ abstract class AbstractEventStoreFactory implements
*/
private $configId;

/**
* @var array
*/
private $driverSchemeAliases = [
'pdo_mysql' => 'mysql',
'pdo_pgsql' => 'pgsql',
];

private $driverSchemeSeparators = [
'pdo_mysql' => ';',
'pdo_pgsql' => ' ',
];

/**
* Creates a new instance from a specified config, specifically meant to be used as static factory.
*
Expand Down Expand Up @@ -94,15 +81,11 @@ public function __invoke(ContainerInterface $container): EventStore
if (isset($config['connection_service'])) {
$connection = $container->get($config['connection_service']);
} else {
$separator = $this->driverSchemeSeparators[$config['connection_options']['driver']];
$dsn = $this->driverSchemeAliases[$config['connection_options']['driver']] . ':';
$dsn .= 'host=' . $config['connection_options']['host'] . $separator;
$dsn .= 'port=' . $config['connection_options']['port'] . $separator;
$dsn .= 'dbname=' . $config['connection_options']['dbname'] . $separator;
$dsn = rtrim($dsn);
$user = $config['connection_options']['user'];
$password = $config['connection_options']['password'];
$connection = new PDO($dsn, $user, $password);
$connection = new PDO(
$this->buildConnectionDsn($config['connection_options']),
$config['connection_options']['user'],
$config['connection_options']['password']
);
}

$eventStoreClassName = $this->eventStoreClassName();
Expand Down Expand Up @@ -165,6 +148,8 @@ abstract protected function createActionEventEmitterEventStore(EventStore $event

abstract protected function eventStoreClassName(): string;

abstract protected function buildConnectionDsn(array $params): string;

public function dimensions(): iterable
{
return ['prooph', 'event_store'];
Expand Down
25 changes: 24 additions & 1 deletion src/Container/MySqlEventStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,39 @@ protected function eventStoreClassName(): string
return MySqlEventStore::class;
}

protected function buildConnectionDsn(array $params): string
{
$dsn = 'mysql:';

if (isset($params['host']) && $params['host'] !== '') {
$dsn .= 'host=' . $params['host'] . ';';
}

if (isset($params['port'])) {
$dsn .= 'port=' . $params['port'] . ';';
}

if (isset($params['dbname'])) {
$dsn .= 'dbname=' . $params['dbname'] . ';';
}

if (isset($params['charset'])) {
$dsn .= 'charset=' . $params['charset'] . ';';
}

return $dsn;
}

public function defaultOptions(): iterable
{
return [
'connection_options' => [
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'host' => '127.0.0.1',
'dbname' => 'event_store',
'port' => 3306,
'charset' => 'utf8',
],
'load_batch_size' => 1000,
'event_streams_table' => 'event_streams',
Expand Down
25 changes: 24 additions & 1 deletion src/Container/PostgresEventStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,39 @@ protected function eventStoreClassName(): string
return PostgresEventStore::class;
}

protected function buildConnectionDsn(array $params): string
{
$dsn = 'pgsql:';

if (isset($params['host']) && $params['host'] !== '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not needed anymore.

$dsn .= 'host=' . $params['host'] . ';';
}

if (isset($params['port']) && $params['port'] !== '') {
$dsn .= 'port=' . $params['port'] . ';';
}

if (isset($params['dbname'])) {
$dsn .= 'dbname=' . $params['dbname'] . ';';
}

if (isset($params['charset'])) {
$dsn .= "options='--client_encoding=".$params['charset']."'";
}

return $dsn;
}

public function defaultOptions(): iterable
{
return [
'connection_options' => [
'driver' => 'pdo_pgsql',
'user' => 'postgres',
'password' => 'postgres',
'host' => '127.0.0.1',
'dbname' => 'event_store',
'port' => 5432,
'charset' => 'utf8',
],
'load_batch_size' => 1000,
'event_streams_table' => 'event_streams',
Expand Down
14 changes: 13 additions & 1 deletion tests/TestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static function getConnection(): PDO
$dsn .= 'host=' . $connectionParams['host'] . $separator;
$dsn .= 'port=' . $connectionParams['port'] . $separator;
$dsn .= 'dbname=' . $connectionParams['dbname'] . $separator;
$dsn .= self::getCharsetValue($connectionParams['charset'], $connectionParams['driver']) . $separator;
$dsn = rtrim($dsn);
self::$connection = new PDO($dsn, $connectionParams['user'], $connectionParams['password']);
}
Expand Down Expand Up @@ -109,7 +110,8 @@ private static function hasRequiredConnectionParams(): bool
$env['DB_PASSWORD'],
$env['DB_HOST'],
$env['DB_NAME'],
$env['DB_PORT']
$env['DB_PORT'],
$env['DB_CHARSET']
);
}

Expand All @@ -122,6 +124,16 @@ private static function getSpecifiedConnectionParams(): array
'host' => getenv('DB_HOST'),
'dbname' => getenv('DB_NAME'),
'port' => getenv('DB_PORT'),
'charset' => getenv('DB_CHARSET'),
];
}

private static function getCharsetValue(string $charset, string $driver): string
{
if ('pdo_pgsql' === $driver) {
return "options='--client_encoding=$charset'";
}

return "charset=$charset";
}
}