Skip to content

Commit

Permalink
Merge pull request #52 from neighborhoods/define-database-port
Browse files Browse the repository at this point in the history
Define database port
  • Loading branch information
alexberryman committed Mar 28, 2019
2 parents 5bfe365 + f183941 commit de61d89
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Environment/Parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: 0
neighborhoods.kojo.environment.parameters.database_name: ''
neighborhoods.kojo.environment.parameters.lock_prefix: ''
21 changes: 19 additions & 2 deletions src/PDO/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ 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;

public function getPdo(): \PDO
{
if ($this->_pdo === null) {
$dsn = $this->_getDataSourceName();
if ($this->_getPort() !== 0){
$dsn = sprintf('%s;port=%d', $this->_getDataSourceName(),$this->_getPort());
} else {
$dsn = $this->_getDataSourceName();
}
$userName = $this->_getUserName();
$password = $this->_getPassword();
if ($this->_hasOptions()) {
Expand Down Expand Up @@ -87,4 +92,16 @@ protected function _hasOptions(): bool
{
return $this->_exists(self::PROP_OPTIONS);
}
}

public function setPort(int $port): BuilderInterface
{
$this->_create(self::PROP_PORT, $port);

return $this;
}

protected function _getPort(): int
{
return $this->_read(self::PROP_PORT);
}
}
3 changes: 2 additions & 1 deletion src/PDO/Builder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
public: false
4 changes: 3 additions & 1 deletion src/PDO/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public function setUserName(string $userName): BuilderInterface;
public function setPassword(string $password): BuilderInterface;

public function setOptions(array $options): BuilderInterface;
}

public function setPort(int $port): BuilderInterface;
}

0 comments on commit de61d89

Please sign in to comment.