start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace DesignPatterns\DependencyInjection;
/**
* class AbstractConfig
*/
abstract class AbstractConfig
{
/**
* @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 implements Parameters
{
/**
* 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

@@ -0,0 +1,49 @@
<?php
namespace DesignPatterns\DependencyInjection;
/**
* Class Connection
*/
class Connection
{
/**
* @var Configuration
*/
protected $configuration;
/**
* @var Currently connected host
*/
protected $host;
/**
* @param Parameters $config
*/
public function __construct(Parameters $config)
{
$this->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;
}
}

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,16 @@
# Dependency Injection
## Purpose
To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.
## Usage
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`.
Notice we are following Inversion of control principle in `Connection` by asking `$config` to implement `Parameters` interface. This decouples our components. We don't care where the source of information comes from, we only care that config has certain methods to retrieve that information. Read more about Inversion of control [here](http://en.wikipedia.org/wiki/Inversion_of_control).
## Examples
* the Doctrine2 ORM uses dependency injection e.g. for Configuration that is injected into a Connection object. for testing purposes, one can easily create a mock object of the configuration and inject that into the connection object
* Symfony and Zend Framework 2 already have containers for DI that create objects via a configuration array and inject them where needed (i.e. in Controllers)

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');