From a964d6fb24df1dc102d7443dea73c54c6953fa88 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Tue, 22 Jan 2019 10:32:00 -0600 Subject: [PATCH 1/4] define-database-port | set port as a string and use sprintf to attach to DSN --- src/Environment/Parameters.yml | 1 + src/PDO/Builder.php | 23 ++++++++++++++++++++++- src/PDO/Builder.yml | 3 ++- src/PDO/BuilderInterface.php | 4 +++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Environment/Parameters.yml b/src/Environment/Parameters.yml index 3b84af4b..eff0d418 100644 --- a/src/Environment/Parameters.yml +++ b/src/Environment/Parameters.yml @@ -5,5 +5,6 @@ parameters: neighborhoods.kojo.environment.parameters.database_password: '' neighborhoods.kojo.environment.parameters.database_adapter: '' neighborhoods.kojo.environment.parameters.database_host: '' + neighborhoods.kojo.environment.parameters.database_port: '' neighborhoods.kojo.environment.parameters.database_name: '' neighborhoods.kojo.environment.parameters.lock_prefix: '' diff --git a/src/PDO/Builder.php b/src/PDO/Builder.php index 5eed0194..0409f137 100644 --- a/src/PDO/Builder.php +++ b/src/PDO/Builder.php @@ -12,6 +12,7 @@ class Builder implements BuilderInterface protected const PROP_DATA_SOURCE_NAME = 'data_source_name'; protected const PROP_USER_NAME = 'user_name'; protected const PROP_PASSWORD = 'password'; + protected const PROP_PORT = 'port'; protected const PROP_OPTIONS = 'options'; protected $_pdo; @@ -19,6 +20,9 @@ public function getPdo(): \PDO { if ($this->_pdo === null) { $dsn = $this->_getDataSourceName(); + if ($this->_hasPort() && is_numeric($this->_getPort())){ + $dsn = $dsn . sprintf(';port=%d', $this->_getPort()); + } $userName = $this->_getUserName(); $password = $this->_getPassword(); if ($this->_hasOptions()) { @@ -87,4 +91,21 @@ protected function _hasOptions(): bool { return $this->_exists(self::PROP_OPTIONS); } -} \ No newline at end of file + + public function setPort(string $port): BuilderInterface + { + $this->_create(self::PROP_PORT, $port); + + return $this; + } + + protected function _getPort(): string + { + return $this->_read(self::PROP_PORT); + } + + protected function _hasPort(): bool + { + return $this->_exists(self::PROP_PORT); + } +} diff --git a/src/PDO/Builder.yml b/src/PDO/Builder.yml index 6f7526e7..2c7915f4 100644 --- a/src/PDO/Builder.yml +++ b/src/PDO/Builder.yml @@ -7,6 +7,7 @@ services: - [setPassword, ['%neighborhoods.kojo.environment.parameters.database_password%']] - [setUserName, ['%neighborhoods.kojo.environment.parameters.database_user_name%']] - [setDataSourceName, ['%neighborhoods.kojo.environment.parameters.database_adapter%:dbname=%neighborhoods.kojo.environment.parameters.database_name%;host=%neighborhoods.kojo.environment.parameters.database_host%']] + - [setPort, ['%neighborhoods.kojo.environment.parameters.database_port%']] pdo.builder: alias: neighborhoods.kojo.pdo.builder - public: false \ No newline at end of file + public: false diff --git a/src/PDO/BuilderInterface.php b/src/PDO/BuilderInterface.php index 15b81246..2722bb41 100644 --- a/src/PDO/BuilderInterface.php +++ b/src/PDO/BuilderInterface.php @@ -14,4 +14,6 @@ public function setUserName(string $userName): BuilderInterface; public function setPassword(string $password): BuilderInterface; public function setOptions(array $options): BuilderInterface; -} \ No newline at end of file + + public function setPort(string $port): BuilderInterface; +} From 04ef40182dc90b4ed63e5a72b06fa44588bb77b6 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Tue, 22 Jan 2019 14:35:28 -0600 Subject: [PATCH 2/4] define-database-port | make `port` an int with a default of 0. If port is 0, don't add to DSN. --- src/Environment/Parameters.yml | 2 +- src/PDO/Builder.php | 6 +++--- src/PDO/BuilderInterface.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Environment/Parameters.yml b/src/Environment/Parameters.yml index eff0d418..03a19581 100644 --- a/src/Environment/Parameters.yml +++ b/src/Environment/Parameters.yml @@ -5,6 +5,6 @@ parameters: neighborhoods.kojo.environment.parameters.database_password: '' neighborhoods.kojo.environment.parameters.database_adapter: '' neighborhoods.kojo.environment.parameters.database_host: '' - neighborhoods.kojo.environment.parameters.database_port: '' + neighborhoods.kojo.environment.parameters.database_port: 0 neighborhoods.kojo.environment.parameters.database_name: '' neighborhoods.kojo.environment.parameters.lock_prefix: '' diff --git a/src/PDO/Builder.php b/src/PDO/Builder.php index 0409f137..545cdd84 100644 --- a/src/PDO/Builder.php +++ b/src/PDO/Builder.php @@ -20,7 +20,7 @@ public function getPdo(): \PDO { if ($this->_pdo === null) { $dsn = $this->_getDataSourceName(); - if ($this->_hasPort() && is_numeric($this->_getPort())){ + if ($this->_hasPort() && $this->_getPort() !== 0){ $dsn = $dsn . sprintf(';port=%d', $this->_getPort()); } $userName = $this->_getUserName(); @@ -92,14 +92,14 @@ protected function _hasOptions(): bool return $this->_exists(self::PROP_OPTIONS); } - public function setPort(string $port): BuilderInterface + public function setPort(int $port): BuilderInterface { $this->_create(self::PROP_PORT, $port); return $this; } - protected function _getPort(): string + protected function _getPort(): int { return $this->_read(self::PROP_PORT); } diff --git a/src/PDO/BuilderInterface.php b/src/PDO/BuilderInterface.php index 2722bb41..8ae29c21 100644 --- a/src/PDO/BuilderInterface.php +++ b/src/PDO/BuilderInterface.php @@ -15,5 +15,5 @@ public function setPassword(string $password): BuilderInterface; public function setOptions(array $options): BuilderInterface; - public function setPort(string $port): BuilderInterface; + public function setPort(int $port): BuilderInterface; } From eeada7d0e2111c3072a29d7983d22c21ef5c5874 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Tue, 22 Jan 2019 15:43:36 -0600 Subject: [PATCH 3/4] define-database-port | build the DSN explicitly instead of mutating it --- src/PDO/Builder.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/PDO/Builder.php b/src/PDO/Builder.php index 545cdd84..8193ef84 100644 --- a/src/PDO/Builder.php +++ b/src/PDO/Builder.php @@ -19,9 +19,10 @@ class Builder implements BuilderInterface public function getPdo(): \PDO { if ($this->_pdo === null) { - $dsn = $this->_getDataSourceName(); - if ($this->_hasPort() && $this->_getPort() !== 0){ - $dsn = $dsn . sprintf(';port=%d', $this->_getPort()); + if ($this->_getPort() !== 0){ + $dsn = sprintf('%s;port=%d', $this->_getDataSourceName(),$this->_getPort()); + } else { + $dsn = $this->_getDataSourceName(); } $userName = $this->_getUserName(); $password = $this->_getPassword(); From 58e768e33d392de07aa46efce0e23e37d2aa1e96 Mon Sep 17 00:00:00 2001 From: Alex Berryman Date: Tue, 22 Jan 2019 15:44:34 -0600 Subject: [PATCH 4/4] define-database-port | we always hasPort since it's default is 0 --- src/PDO/Builder.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/PDO/Builder.php b/src/PDO/Builder.php index 8193ef84..e5e6b3d6 100644 --- a/src/PDO/Builder.php +++ b/src/PDO/Builder.php @@ -104,9 +104,4 @@ protected function _getPort(): int { return $this->_read(self::PROP_PORT); } - - protected function _hasPort(): bool - { - return $this->_exists(self::PROP_PORT); - } }