1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simply\Container; |
4
|
|
|
|
5
|
|
|
use Simply\Container\Entry\CallableEntry; |
6
|
|
|
use Simply\Container\Entry\MixedEntry; |
7
|
|
|
use Simply\Container\Entry\ProviderEntry; |
8
|
|
|
use Simply\Container\Exception\ContainerException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class that provides convenience functionality for setting up containers. |
12
|
|
|
* |
13
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
14
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
15
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
16
|
|
|
*/ |
17
|
|
|
class ContainerBuilder |
18
|
|
|
{ |
19
|
|
|
/** @var Container The container that is being built */ |
20
|
|
|
private $container; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ContainerBuilder constructor. |
24
|
|
|
*/ |
25
|
2 |
|
public function __construct() |
26
|
|
|
{ |
27
|
2 |
|
$this->container = new Container(); |
28
|
2 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the container that is being built. |
32
|
|
|
* @return Container The container that is being built |
33
|
|
|
*/ |
34
|
2 |
|
public function getContainer(): Container |
35
|
|
|
{ |
36
|
2 |
|
return $this->container; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Registers an array with identifier value pairs to the container as mixed entries. |
41
|
|
|
* @param array $configuration The configuration array to register |
42
|
|
|
* @throws ContainerException If trying to register values that already exist |
43
|
|
|
*/ |
44
|
1 |
|
public function registerConfiguration(array $configuration): void |
45
|
|
|
{ |
46
|
1 |
|
foreach ($configuration as $identifier => $value) { |
47
|
1 |
|
$this->container->addEntry($identifier, new MixedEntry($value)); |
48
|
|
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Registers the given provider as a callable and applicable methods as provider methods. |
53
|
|
|
* @param EntryProvider $provider The entry provider to register |
54
|
|
|
* @throws ContainerException If the provider tries to provide something that has already been registered |
55
|
|
|
*/ |
56
|
1 |
|
public function registerProvider(EntryProvider $provider): void |
57
|
|
|
{ |
58
|
1 |
|
$class = \get_class($provider); |
59
|
1 |
|
$reflection = new \ReflectionClass($class); |
60
|
|
|
|
61
|
1 |
|
$this->container->addEntry($class, new CallableEntry([$class, 'initialize'])); |
62
|
|
|
|
63
|
1 |
|
foreach ($reflection->getMethods() as $method) { |
64
|
1 |
|
$identifier = $this->getMethodIdentifier($method); |
65
|
|
|
|
66
|
1 |
|
if ($identifier !== null) { |
67
|
1 |
|
$this->container->addEntry($identifier, new ProviderEntry([$provider, $method->getName()])); |
68
|
|
|
} |
69
|
|
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Tells the identifier to use for the given provider method. |
74
|
|
|
* @param \ReflectionMethod $method The provider method |
75
|
|
|
* @return string|null The identifier for the method or null if not applicable |
76
|
|
|
*/ |
77
|
1 |
|
private function getMethodIdentifier(\ReflectionMethod $method): ?string |
78
|
|
|
{ |
79
|
1 |
|
if (!$method->isPublic() || $method->isStatic()) { |
80
|
1 |
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
if (strncmp($method->getName(), '__', 2) === 0) { |
84
|
1 |
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$type = $method->getReturnType(); |
88
|
|
|
|
89
|
1 |
|
if ($type === null || $type->isBuiltin()) { |
90
|
1 |
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $type->getName(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|