mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
start a restructure
This commit is contained in:
19
Structural/DependencyInjection/AbstractConfig.php
Normal file
19
Structural/DependencyInjection/AbstractConfig.php
Normal 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;
|
||||
}
|
||||
}
|
36
Structural/DependencyInjection/ArrayConfig.php
Normal file
36
Structural/DependencyInjection/ArrayConfig.php
Normal 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;
|
||||
}
|
||||
}
|
49
Structural/DependencyInjection/Connection.php
Normal file
49
Structural/DependencyInjection/Connection.php
Normal 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;
|
||||
}
|
||||
}
|
26
Structural/DependencyInjection/Parameters.php
Normal file
26
Structural/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);
|
||||
}
|
16
Structural/DependencyInjection/README.md
Normal file
16
Structural/DependencyInjection/README.md
Normal 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)
|
@@ -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
Structural/DependencyInjection/Test/config.php
Normal file
3
Structural/DependencyInjection/Test/config.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
return array('host' => 'github.com');
|
Reference in New Issue
Block a user