Implement InversionOfControl aware DependencyInjection

This commit is contained in:
Anton Nizhegorodov
2013-12-07 01:45:15 +02:00
committed by Dominik Liebler
parent 3114cf1013
commit 5c36fadf1a
7 changed files with 140 additions and 40 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace DesignPatterns\DependencyInjection;
/**
* class AbstractConfig
*/
abstract class AbstractConfig implements Parameters
{
/**
* @var Storage of data
*/
protected $storage;
public function __construct($storage)
{
$this->storage = $storage;
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace DesignPatterns\DependencyInjection;
/**
* class ArrayConfig
*
* uses array as data source
*/
class ArrayConfig extends AbstractConfig
{
/**
* Get parameter
*
* @param string|int $key
*
* @return mixed
*/
public function get($key, $default = null) {;
if (isset($this->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;
}
}

View File

@@ -1,34 +0,0 @@
<?php
namespace DesignPatterns\DependencyInjection;
/**
* class Configuration
*/
class Configuration
{
/**
* @var string
*/
protected $host;
/**
* @param string $host
*
* @return Configuration
*/
public function setHost($host)
{
$this->host = $host;
return $this; // for a fluent interface
}
/**
* @return string
*/
public function getHost()
{
return $this->host;
}
}

View File

@@ -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 <http://en.wikipedia.org/wiki/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;
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace DesignPatterns\DependencyInjection;
/**
* Parameters interface
*/
interface Parameters
{
/**
* Get parameter
*
* @param string|int $key
*
* @return mixed
*/
public function get($key);
/**
* Set parameter
*
* @param string|int $key
* @param mixed $value
*/
public function set($key, $value);
}

View File

@@ -0,0 +1,27 @@
<?php
namespace DesignPatterns\Tests\DependencyInjection;
use DesignPatterns\DependencyInjection\Parameters;
use DesignPatterns\DependencyInjection\AbstractConfig;
use DesignPatterns\DependencyInjection\ArrayConfig;
use DesignPatterns\DependencyInjection\Connection;
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());
}
}

View File

@@ -0,0 +1,3 @@
<?php
return array('host' => 'github.com');