mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
Implement InversionOfControl aware DependencyInjection
This commit is contained in:
committed by
Dominik Liebler
parent
3114cf1013
commit
5c36fadf1a
19
DependencyInjection/AbstractConfig.php
Normal file
19
DependencyInjection/AbstractConfig.php
Normal 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;
|
||||
}
|
||||
}
|
36
DependencyInjection/ArrayConfig.php
Normal file
36
DependencyInjection/ArrayConfig.php
Normal 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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
26
DependencyInjection/Parameters.php
Normal file
26
DependencyInjection/Parameters.php
Normal 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);
|
||||
}
|
27
Tests/DependencyInjection/DependencyInjectionTest.php
Normal file
27
Tests/DependencyInjection/DependencyInjectionTest.php
Normal 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());
|
||||
}
|
||||
}
|
3
Tests/DependencyInjection/config.php
Normal file
3
Tests/DependencyInjection/config.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
return array('host' => 'github.com');
|
Reference in New Issue
Block a user