Created
September 20, 2017 14:45
-
-
Save MaximStrutinskiy/9162824aa2f51a751f35d56c5412f2c9 to your computer and use it in GitHub Desktop.
DI pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DesignPatterns\Structural\DependencyInjection; | |
class DatabaseConfiguration | |
{ | |
/** | |
* @var string | |
*/ | |
private $host; | |
/** | |
* @var int | |
*/ | |
private $port; | |
/** | |
* @var string | |
*/ | |
private $username; | |
/** | |
* @var string | |
*/ | |
private $password; | |
public function __construct(string $host, int $port, string $username, string $password) | |
{ | |
$this->host = $host; | |
$this->port = $port; | |
$this->username = $username; | |
$this->password = $password; | |
} | |
public function getHost(): string | |
{ | |
return $this->host; | |
} | |
public function getPort(): int | |
{ | |
return $this->port; | |
} | |
public function getUsername(): string | |
{ | |
return $this->username; | |
} | |
public function getPassword(): string | |
{ | |
return $this->password; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DesignPatterns\Structural\DependencyInjection; | |
class DatabaseConnection | |
{ | |
/** | |
* @var DatabaseConfiguration | |
*/ | |
private $configuration; | |
/** | |
* @param DatabaseConfiguration $config | |
*/ | |
public function __construct(DatabaseConfiguration $config) | |
{ | |
$this->configuration = $config; | |
} | |
public function getDsn(): string | |
{ | |
// this is just for the sake of demonstration, not a real DSN | |
// notice that only the injected config is used here, so there is | |
// a real separation of concerns here | |
return sprintf( | |
'%s:%s@%s:%d', | |
$this->configuration->getUsername(), | |
$this->configuration->getPassword(), | |
$this->configuration->getHost(), | |
$this->configuration->getPort() | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DesignPatterns\Structural\DependencyInjection\Tests; | |
use DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration; | |
use DesignPatterns\Structural\DependencyInjection\DatabaseConnection; | |
use PHPUnit\Framework\TestCase; | |
class DependencyInjectionTest extends TestCase | |
{ | |
public function testDependencyInjection() | |
{ | |
$config = new DatabaseConfiguration('localhost', 3306, 'domnikl', '1234'); | |
$connection = new DatabaseConnection($config); | |
$this->assertEquals('domnikl:1234@localhost:3306', $connection->getDsn()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment