diff --git a/Structural/DependencyInjection/AbstractConfig.php b/Structural/DependencyInjection/AbstractConfig.php deleted file mode 100644 index f2da4dc..0000000 --- a/Structural/DependencyInjection/AbstractConfig.php +++ /dev/null @@ -1,19 +0,0 @@ -storage = $storage; - } -} diff --git a/Structural/DependencyInjection/ArrayConfig.php b/Structural/DependencyInjection/ArrayConfig.php deleted file mode 100644 index f26c089..0000000 --- a/Structural/DependencyInjection/ArrayConfig.php +++ /dev/null @@ -1,39 +0,0 @@ -storage[$key])) { - return $this->storage[$key]; - } - - return $default; - } - - /** - * Set parameter. - * - * @param string|int $key - * @param mixed $value - */ - public function set($key, $value) - { - $this->storage[$key] = $value; - } -} diff --git a/Structural/DependencyInjection/Connection.php b/Structural/DependencyInjection/Connection.php deleted file mode 100644 index 1a5764c..0000000 --- a/Structural/DependencyInjection/Connection.php +++ /dev/null @@ -1,47 +0,0 @@ -configuration = $config; - } - - /** - * connection using the injected config. - */ - public function connect() - { - $host = $this->configuration->get('host'); - // connection to host, authentication etc... - - //if connected - $this->host = $host; - } - - /* - * Get currently connected host - * - * @return string - */ - - public function getHost() - { - return $this->host; - } -} diff --git a/Structural/DependencyInjection/DatabaseConfiguration.php b/Structural/DependencyInjection/DatabaseConfiguration.php new file mode 100644 index 0000000..51d72d7 --- /dev/null +++ b/Structural/DependencyInjection/DatabaseConfiguration.php @@ -0,0 +1,54 @@ +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; + } +} diff --git a/Structural/DependencyInjection/DatabaseConnection.php b/Structural/DependencyInjection/DatabaseConnection.php new file mode 100644 index 0000000..b5af8ba --- /dev/null +++ b/Structural/DependencyInjection/DatabaseConnection.php @@ -0,0 +1,34 @@ +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() + ); + } +} diff --git a/Structural/DependencyInjection/Parameters.php b/Structural/DependencyInjection/Parameters.php deleted file mode 100644 index c49f4c7..0000000 --- a/Structural/DependencyInjection/Parameters.php +++ /dev/null @@ -1,26 +0,0 @@ -`__. +directly in ``DatabaseConnection``, which is not very good for testing and +extending it. Examples -------- @@ -45,27 +38,15 @@ Code You can also find these code on `GitHub`_ -AbstractConfig.php +DatabaseConfiguration.php -.. literalinclude:: AbstractConfig.php +.. literalinclude:: DatabaseConfiguration.php :language: php :linenos: -Parameters.php +DatabaseConnection.php -.. literalinclude:: Parameters.php - :language: php - :linenos: - -ArrayConfig.php - -.. literalinclude:: ArrayConfig.php - :language: php - :linenos: - -Connection.php - -.. literalinclude:: Connection.php +.. literalinclude:: DatabaseConnection.php :language: php :linenos: @@ -78,11 +59,5 @@ Tests/DependencyInjectionTest.php :language: php :linenos: -Tests/config.php - -.. literalinclude:: Tests/config.php - :language: php - :linenos: - .. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/DependencyInjection .. __: http://en.wikipedia.org/wiki/Dependency_injection diff --git a/Structural/DependencyInjection/Tests/DependencyInjectionTest.php b/Structural/DependencyInjection/Tests/DependencyInjectionTest.php index 6df82a7..3749d57 100644 --- a/Structural/DependencyInjection/Tests/DependencyInjectionTest.php +++ b/Structural/DependencyInjection/Tests/DependencyInjectionTest.php @@ -2,24 +2,16 @@ namespace DesignPatterns\Structural\DependencyInjection\Tests; -use DesignPatterns\Structural\DependencyInjection\ArrayConfig; -use DesignPatterns\Structural\DependencyInjection\Connection; +use DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration; +use DesignPatterns\Structural\DependencyInjection\DatabaseConnection; class DependencyInjectionTest extends \PHPUnit_Framework_TestCase { - protected $config; - protected $source; - - public function setUp() - { - $this->source = include 'config.php'; - $this->config = new ArrayConfig($this->source); - } - public function testDependencyInjection() { - $connection = new Connection($this->config); - $connection->connect(); - $this->assertEquals($this->source['host'], $connection->getHost()); + $config = new DatabaseConfiguration('localhost', 3306, 'domnikl', '1234'); + $connection = new DatabaseConnection($config); + + $this->assertEquals('domnikl:1234@localhost:3306', $connection->getDsn()); } } diff --git a/Structural/DependencyInjection/Tests/config.php b/Structural/DependencyInjection/Tests/config.php deleted file mode 100644 index 29d3683..0000000 --- a/Structural/DependencyInjection/Tests/config.php +++ /dev/null @@ -1,3 +0,0 @@ - 'github.com'); diff --git a/Structural/DependencyInjection/uml/DependencyInjection.uml b/Structural/DependencyInjection/uml/DependencyInjection.uml index e058b19..2434332 100644 --- a/Structural/DependencyInjection/uml/DependencyInjection.uml +++ b/Structural/DependencyInjection/uml/DependencyInjection.uml @@ -1,38 +1,20 @@ - - - PHP - \DesignPatterns\Structural\DependencyInjection\AbstractConfig - - \DesignPatterns\Structural\DependencyInjection\Connection - \DesignPatterns\Structural\DependencyInjection\ArrayConfig - \DesignPatterns\Structural\DependencyInjection\Parameters - \DesignPatterns\Structural\DependencyInjection\AbstractConfig - - - - - - - - - - - - - - - - - - - \DesignPatterns\Structural\DependencyInjection\AbstractConfig - - - Fields - Constants - Constructors - Methods - - private - - + + + PHP + \DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration + + \DesignPatterns\Structural\DependencyInjection\DatabaseConnection + \DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration + + + + + + + Methods + Constants + Fields + + private + + diff --git a/Structural/DependencyInjection/uml/uml.png b/Structural/DependencyInjection/uml/uml.png index 67e6ca1..992d9a1 100644 Binary files a/Structural/DependencyInjection/uml/uml.png and b/Structural/DependencyInjection/uml/uml.png differ diff --git a/Structural/DependencyInjection/uml/uml.svg b/Structural/DependencyInjection/uml/uml.svg index 78d603d..1e15816 100644 --- a/Structural/DependencyInjection/uml/uml.svg +++ b/Structural/DependencyInjection/uml/uml.svg @@ -1,425 +1,573 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - configuration - - - - - - - - - - host - - - - - - - - - - - - - __construct(config) - - - - - - - - - - - - - connect() - - - - - - - - - - getHost() - - - - - - - - - - - - - Connection - - - Connection - - - - - - - - - - - - - - - - - - - get(key, default) - - - - - - - - - - set(key, value) - - - - - - - - - - - - - ArrayConfig - - - ArrayConfig - - - - - - - - - - - - - - - - - - - get(key) - - - - - - - - - - set(key, value) - - - - - - - - - - - - - Parameters - - - Parameters - - - - - - - - - - - - - - - - - - - storage - - - - - - - - - - - - - __construct(storage) - - - - - - - - - - - - - AbstractConfig - - - AbstractConfig - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + configuration + + + + + + + + + + + + + + + + getDsn() + + + + + + + + + + + + + DatabaseConnection + + + DatabaseConnection + + + + + + + + + + + + + + + + + + + + + + configuration + + + + + + + + + + + + + + + + + + + getDsn() + + + + + + + + + + + + + DatabaseConnection + + + DatabaseConnection + + + + + + + + + + + + + + + + + + + + + + + + + password + + + + + + + + + + + + + + + + port + + + + + + + + + + + + + + + + host + + + + + + + + + + + + + + + + username + + + + + + + + + + + + + + + + getHost() + + + + + + + + + + + + + getPort() + + + + + + + + + + + + + getUsername() + + + + + + + + + + + + + getPassword() + + + + + + + + + + + + + DatabaseConfiguration + + + DatabaseConfiguration + + + + + + + + + + + + + + + + + + + + + + password + + + + + + + + + + + + + + + + port + + + + + + + + + + + + + + + + host + + + + + + + + + + + + + + + + username + + + + + + + + + + + + + + + + + + + getHost() + + + + + + + + + + + + + + + + getPort() + + + + + + + + + + + + + + + + getUsername() + + + + + + + + + + + + + + + + getPassword() + + + + + + + + + + + + + DatabaseConfiguration + + + DatabaseConfiguration + + +