Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ cd ./php-censor.local
./bin/console php-censor:install

# Non-interactive installation
./bin/console php-censor:install --url='http://php-censor.local' --db-type=pgsql --db-host=localhost --db-name=php-censor --db-user=php-censor --db-password=php-censor --db-port=null --admin-name=admin --admin-password=admin --admin-email='admin@php-censor.local' --queue-use=1 --queue-host=localhost --queue-name=php-censor
./bin/console php-censor:install --url='http://php-censor.local' --db-type=pgsql --db-host=localhost --db-pgsql-sslmode=prefer --db-name=php-censor --db-user=php-censor --db-password=php-censor --db-port=null --admin-name=admin --admin-password=admin --admin-email='admin@php-censor.local' --queue-use=1 --queue-host=localhost --queue-name=php-censor

# Non-interactive installation with prepared config.yml file
./bin/console php-censor:install --config-from-file=yes --admin-name=admin --admin-password=admin --admin-email='admin@php-censor.local'
Expand Down
10 changes: 6 additions & 4 deletions docs/en/configuring_application.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ b8:
database:
servers:
read:
- host: localhost
port: 3306
- host: localhost
port: 3306
pgsql-sslmode: prefer # disable, allow, prefer, require, verify-ca, or verify-full. See https://www.postgresql.org/docs/8.4/libpq-connect.html#LIBPQ-CONNECT-SSLMODE for details
write:
- host: localhost
port: 3306
- host: localhost
port: 3306
pgsql-sslmode: prefer
type: mysql # Database type: "mysql" or "pgsql"
name: php-censor-db
username: php-censor-user
Expand Down
17 changes: 17 additions & 0 deletions src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function configure()
->addOption('db-name', null, InputOption::VALUE_OPTIONAL, 'Database name')
->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'Database user')
->addOption('db-password', null, InputOption::VALUE_OPTIONAL, 'Database password')
->addOption('db-pgsql-sslmode', null, InputOption::VALUE_OPTIONAL, 'Postgres SSLMODE option')
->addOption('admin-name', null, InputOption::VALUE_OPTIONAL, 'Admin name')
->addOption('admin-password', null, InputOption::VALUE_OPTIONAL, 'Admin password')
->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'Admin email')
Expand Down Expand Up @@ -401,6 +402,11 @@ protected function getDatabaseInformation(InputInterface $input, OutputInterface
$dbPort = $helper->ask($input, $output, $questionPort);
}

if (strtolower($dbType) === "pgsql") {
$dbPgsqlSslmode = $input->getOption('db-pgsql-sslmode')
?: 'prefer';
}

if (!$dbName = $input->getOption('db-name')) {
$questionDb = new Question('Please enter your database name (default: php-censor-db): ', 'php-censor-db');
$dbName = $helper->ask($input, $output, $questionDb);
Expand All @@ -424,7 +430,12 @@ protected function getDatabaseInformation(InputInterface $input, OutputInterface
]
];

if ($dbType === "pgsql") {
$dbServers[0]['pgsql-sslmode'] = $dbPgsqlSslmode;
}

$dbPort = (integer)$dbPort;

if ($dbPort) {
$dbServers[0]['port'] = $dbPort;
}
Expand All @@ -451,11 +462,17 @@ protected function getDatabaseInformation(InputInterface $input, OutputInterface
protected function verifyDatabaseDetails(array $db, OutputInterface $output)
{
$dns = $db['type'] . ':host=' . $db['servers']['write'][0]['host'];

if (isset($db['servers']['write'][0]['port'])) {
$dns .= ';port=' . (integer)$db['servers']['write'][0]['port'];
}

$dns .= ';dbname=' . $db['name'];

if ($db["type"] === "pgsql") {
$dns .= ';sslmode=' . $db['servers']['write'][0]['pgsql-sslmode'];
}

Comment thread
mikebronner marked this conversation as resolved.
$pdoOptions = [
\PDO::ATTR_PERSISTENT => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
Expand Down
15 changes: 14 additions & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,20 @@ public function __construct($name = 'PHP Censor', $version = 'UNKNOWN')
}

if (!empty($databaseSettings['port'])) {
$phinxSettings['environments']['php-censor']['port'] = (integer)$databaseSettings['port'];
$phinxSettings['environments']['php-censor']['port'] =
(integer) $databaseSettings['port'];
}

if (! empty($databaseSettings['servers']['write'][0]['port'])) {
$phinxSettings['environments']['php-censor']['port'] =
(integer) $databaseSettings['servers']['write'][0]['port'];
}

Comment thread
mikebronner marked this conversation as resolved.
if (! empty($databaseSettings["type"])
&& $databaseSettings["type"] === "pgsql"
) {
$phinxSettings['environments']['php-censor']['host'] .=
';sslmode=' . $databaseSettings['servers']['write'][0]['pgsql-sslmode'];
}

$phinxConfig = new PhinxConfig($phinxSettings);
Expand Down
11 changes: 11 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,27 @@ public static function getConnection($type = 'read')
$server = array_shift($servers);

self::$dsn[$type] = self::$details['driver'] . ':host=' . $server['host'];

if (self::$details['driver'] === "pgsql") {
if (! array_key_exists("pgsql-sslmode", $server)) {
$server["pgsql-sslmode"] = "prefer";
}

self::$dsn[$type] .= ';sslmode=' . $server['pgsql-sslmode'];
}

if (isset($server['port'])) {
self::$dsn[$type] .= ';port=' . (integer)$server['port'];
}

self::$dsn[$type] .= ';dbname=' . self::$details['db'];

$pdoOptions = [
\PDO::ATTR_PERSISTENT => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_TIMEOUT => 2,
];

if (self::MYSQL_TYPE === self::$details['driver']) {
$pdoOptions[\PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES 'UTF8'";
}
Expand Down
4 changes: 2 additions & 2 deletions tests/src/DatabasePostgresqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testGetConnection()
self::assertEquals(POSTGRESQL_USER, $readDetails['user']);
self::assertEquals(POSTGRESQL_PASSWORD, $readDetails['pass']);

self::assertEquals('pgsql:host=localhost;dbname=b8_test', $readConnection->getDsn());
self::assertEquals('pgsql:host=localhost;sslmode=prefer;dbname=b8_test', $readConnection->getDsn());
}

public function testGetWriteConnectionWithPort()
Expand Down Expand Up @@ -171,7 +171,7 @@ public function testGetWriteConnectionWithPort()
self::assertInstanceOf('\PHPCensor\Database', $writeConnection);
self::assertInstanceOf('\PHPCensor\Database', $readConnection);

self::assertEquals('pgsql:host=localhost;port=5432;dbname=b8_test', $readConnection->getDsn());
self::assertEquals('pgsql:host=localhost;sslmode=prefer;port=5432;dbname=b8_test', $readConnection->getDsn());
}

/**
Expand Down