From 5c36fadf1a98f6a099e082ae695ae148f43412ae Mon Sep 17 00:00:00 2001 From: Anton Nizhegorodov Date: Sat, 7 Dec 2013 01:45:15 +0200 Subject: [PATCH] Implement InversionOfControl aware DependencyInjection --- DependencyInjection/AbstractConfig.php | 19 ++++++++++ DependencyInjection/ArrayConfig.php | 36 +++++++++++++++++++ DependencyInjection/Configuration.php | 34 ------------------ DependencyInjection/Connection.php | 35 ++++++++++++++---- DependencyInjection/Parameters.php | 26 ++++++++++++++ .../DependencyInjectionTest.php | 27 ++++++++++++++ Tests/DependencyInjection/config.php | 3 ++ 7 files changed, 140 insertions(+), 40 deletions(-) create mode 100644 DependencyInjection/AbstractConfig.php create mode 100644 DependencyInjection/ArrayConfig.php delete mode 100644 DependencyInjection/Configuration.php create mode 100644 DependencyInjection/Parameters.php create mode 100644 Tests/DependencyInjection/DependencyInjectionTest.php create mode 100644 Tests/DependencyInjection/config.php diff --git a/DependencyInjection/AbstractConfig.php b/DependencyInjection/AbstractConfig.php new file mode 100644 index 0000000..04182ce --- /dev/null +++ b/DependencyInjection/AbstractConfig.php @@ -0,0 +1,19 @@ +storage = $storage; + } +} diff --git a/DependencyInjection/ArrayConfig.php b/DependencyInjection/ArrayConfig.php new file mode 100644 index 0000000..acb6d41 --- /dev/null +++ b/DependencyInjection/ArrayConfig.php @@ -0,0 +1,36 @@ +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/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php deleted file mode 100644 index 839660a..0000000 --- a/DependencyInjection/Configuration.php +++ /dev/null @@ -1,34 +0,0 @@ -host = $host; - - return $this; // for a fluent interface - } - - /** - * @return string - */ - public function getHost() - { - return $this->host; - } -} diff --git a/DependencyInjection/Connection.php b/DependencyInjection/Connection.php index 02ff81b..64d7c18 100644 --- a/DependencyInjection/Connection.php +++ b/DependencyInjection/Connection.php @@ -13,13 +13,23 @@ class Connection protected $configuration; /** - * here, Configuration gets injected and Connection will get all that it needs from Configuration - * without DI, the configuration would be created directly in Connection, which is not very good + * @var Currently connected host + */ + protected $host; + + /** + * Here, Configuration gets injected and Connection will get all that it needs from Configuration + * Without DI, the configuration would be created directly in Connection, which is not very good * for testing and extending Connection * - * @param Configuration $config + * Notice we are following Inversion of control principle here by asking Config to implement + * Parameters interface. This decouples our components. We don't care where the sorce of information, + * we only care that config has certain methods to retrieve that information. Read more about Inversion + * of control + * + * @param Parameters $config */ - public function __construct(Configuration $config) + public function __construct(Parameters $config) { $this->configuration = $config; } @@ -29,7 +39,20 @@ class Connection */ public function connect() { - $host = $this->configuration->getHost(); - // ... + $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/DependencyInjection/Parameters.php b/DependencyInjection/Parameters.php new file mode 100644 index 0000000..a30f1a9 --- /dev/null +++ b/DependencyInjection/Parameters.php @@ -0,0 +1,26 @@ +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()); + } +} diff --git a/Tests/DependencyInjection/config.php b/Tests/DependencyInjection/config.php new file mode 100644 index 0000000..29d3683 --- /dev/null +++ b/Tests/DependencyInjection/config.php @@ -0,0 +1,3 @@ + 'github.com');