Skip to content

Commit e6c67c5

Browse files
author
Dominik Liebler
committed
PHP7 Dependency Injection
1 parent 1f9348d commit e6c67c5

12 files changed

+694
-643
lines changed

Structural/DependencyInjection/AbstractConfig.php

-19
This file was deleted.

Structural/DependencyInjection/ArrayConfig.php

-39
This file was deleted.

Structural/DependencyInjection/Connection.php

-47
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\DependencyInjection;
4+
5+
class DatabaseConfiguration
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $host;
11+
12+
/**
13+
* @var int
14+
*/
15+
private $port;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $username;
21+
22+
/**
23+
* @var string
24+
*/
25+
private $password;
26+
27+
public function __construct(string $host, int $port, string $username, string $password)
28+
{
29+
$this->host = $host;
30+
$this->port = $port;
31+
$this->username = $username;
32+
$this->password = $password;
33+
}
34+
35+
public function getHost(): string
36+
{
37+
return $this->host;
38+
}
39+
40+
public function getPort(): int
41+
{
42+
return $this->port;
43+
}
44+
45+
public function getUsername(): string
46+
{
47+
return $this->username;
48+
}
49+
50+
public function getPassword(): string
51+
{
52+
return $this->password;
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\DependencyInjection;
4+
5+
class DatabaseConnection
6+
{
7+
/**
8+
* @var DatabaseConfiguration
9+
*/
10+
private $configuration;
11+
12+
/**
13+
* @param DatabaseConfiguration $config
14+
*/
15+
public function __construct(DatabaseConfiguration $config)
16+
{
17+
$this->configuration = $config;
18+
}
19+
20+
public function getDsn(): string
21+
{
22+
// this is just for the sake of demonstration, not a real DSN
23+
// notice that only the injected config is used here, so there is
24+
// a real separation of concerns here
25+
26+
return sprintf(
27+
'%s:%s@%s:%d',
28+
$this->configuration->getUsername(),
29+
$this->configuration->getPassword(),
30+
$this->configuration->getHost(),
31+
$this->configuration->getPort()
32+
);
33+
}
34+
}

Structural/DependencyInjection/Parameters.php

-26
This file was deleted.

Structural/DependencyInjection/README.rst

+7-32
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@ testable, maintainable and extendable code.
1010
Usage
1111
-----
1212

13-
Configuration gets injected and ``Connection`` will get all that it
13+
DatabaseConfiguration gets injected and ``DatabaseConnection`` will get all that it
1414
needs from ``$config``. Without DI, the configuration would be created
15-
directly in ``Connection``, which is not very good for testing and
16-
extending ``Connection``.
17-
18-
Notice we are following Inversion of control principle in ``Connection``
19-
by asking ``$config`` to implement ``Parameters`` interface. This
20-
decouples our components. We don't care where the source of information
21-
comes from, we only care that ``$config`` has certain methods to
22-
retrieve that information. Read more about Inversion of control
23-
`here <http://en.wikipedia.org/wiki/Inversion_of_control>`__.
15+
directly in ``DatabaseConnection``, which is not very good for testing and
16+
extending it.
2417

2518
Examples
2619
--------
@@ -45,27 +38,15 @@ Code
4538

4639
You can also find these code on `GitHub`_
4740

48-
AbstractConfig.php
41+
DatabaseConfiguration.php
4942

50-
.. literalinclude:: AbstractConfig.php
43+
.. literalinclude:: DatabaseConfiguration.php
5144
:language: php
5245
:linenos:
5346

54-
Parameters.php
47+
DatabaseConnection.php
5548

56-
.. literalinclude:: Parameters.php
57-
:language: php
58-
:linenos:
59-
60-
ArrayConfig.php
61-
62-
.. literalinclude:: ArrayConfig.php
63-
:language: php
64-
:linenos:
65-
66-
Connection.php
67-
68-
.. literalinclude:: Connection.php
49+
.. literalinclude:: DatabaseConnection.php
6950
:language: php
7051
:linenos:
7152

@@ -78,11 +59,5 @@ Tests/DependencyInjectionTest.php
7859
:language: php
7960
:linenos:
8061

81-
Tests/config.php
82-
83-
.. literalinclude:: Tests/config.php
84-
:language: php
85-
:linenos:
86-
8762
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/DependencyInjection
8863
.. __: http://en.wikipedia.org/wiki/Dependency_injection

Structural/DependencyInjection/Tests/DependencyInjectionTest.php

+6-14
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@
22

33
namespace DesignPatterns\Structural\DependencyInjection\Tests;
44

5-
use DesignPatterns\Structural\DependencyInjection\ArrayConfig;
6-
use DesignPatterns\Structural\DependencyInjection\Connection;
5+
use DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration;
6+
use DesignPatterns\Structural\DependencyInjection\DatabaseConnection;
77

88
class DependencyInjectionTest extends \PHPUnit_Framework_TestCase
99
{
10-
protected $config;
11-
protected $source;
12-
13-
public function setUp()
14-
{
15-
$this->source = include 'config.php';
16-
$this->config = new ArrayConfig($this->source);
17-
}
18-
1910
public function testDependencyInjection()
2011
{
21-
$connection = new Connection($this->config);
22-
$connection->connect();
23-
$this->assertEquals($this->source['host'], $connection->getHost());
12+
$config = new DatabaseConfiguration('localhost', 3306, 'domnikl', '1234');
13+
$connection = new DatabaseConnection($config);
14+
15+
$this->assertEquals('domnikl:1234@localhost:3306', $connection->getDsn());
2416
}
2517
}

Structural/DependencyInjection/Tests/config.php

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<Diagram>
3-
<ID>PHP</ID>
4-
<OriginalElement>\DesignPatterns\Structural\DependencyInjection\AbstractConfig</OriginalElement>
5-
<nodes>
6-
<node x="0.0" y="230.0">\DesignPatterns\Structural\DependencyInjection\Connection</node>
7-
<node x="51.0" y="118.0">\DesignPatterns\Structural\DependencyInjection\ArrayConfig</node>
8-
<node x="0.0" y="0.5">\DesignPatterns\Structural\DependencyInjection\Parameters</node>
9-
<node x="137.0" y="0.0">\DesignPatterns\Structural\DependencyInjection\AbstractConfig</node>
10-
</nodes>
11-
<notes />
12-
<edges>
13-
<edge source="\DesignPatterns\Structural\DependencyInjection\ArrayConfig" target="\DesignPatterns\Structural\DependencyInjection\AbstractConfig">
14-
<point x="39.5" y="-33.5" />
15-
<point x="169.5" y="93.0" />
16-
<point x="201.5" y="93.0" />
17-
<point x="0.0" y="34.0" />
18-
</edge>
19-
<edge source="\DesignPatterns\Structural\DependencyInjection\ArrayConfig" target="\DesignPatterns\Structural\DependencyInjection\Parameters">
20-
<point x="-39.5" y="-33.5" />
21-
<point x="90.5" y="93.0" />
22-
<point x="58.5" y="93.0" />
23-
<point x="0.0" y="33.5" />
24-
</edge>
25-
</edges>
26-
<settings layout="Hierarchic Group" zoom="1.0" x="133.0" y="179.5" />
27-
<SelectedNodes>
28-
<node>\DesignPatterns\Structural\DependencyInjection\AbstractConfig</node>
29-
</SelectedNodes>
30-
<Categories>
31-
<Category>Fields</Category>
32-
<Category>Constants</Category>
33-
<Category>Constructors</Category>
34-
<Category>Methods</Category>
35-
</Categories>
36-
<VISIBILITY>private</VISIBILITY>
37-
</Diagram>
38-
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Diagram>
3+
<ID>PHP</ID>
4+
<OriginalElement>\DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration</OriginalElement>
5+
<nodes>
6+
<node x="0.0" y="251.0">\DesignPatterns\Structural\DependencyInjection\DatabaseConnection</node>
7+
<node x="0.0" y="0.0">\DesignPatterns\Structural\DependencyInjection\DatabaseConfiguration</node>
8+
</nodes>
9+
<notes />
10+
<edges />
11+
<settings layout="Hierarchic Group" zoom="1.0" x="88.5" y="51.5" />
12+
<SelectedNodes />
13+
<Categories>
14+
<Category>Methods</Category>
15+
<Category>Constants</Category>
16+
<Category>Fields</Category>
17+
</Categories>
18+
<VISIBILITY>private</VISIBILITY>
19+
</Diagram>
20+
30.1 KB
Loading

0 commit comments

Comments
 (0)