|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sensorario\Container; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
|
|
7
|
|
|
class Resolver |
|
8
|
|
|
{ |
|
9
|
|
|
private $instances = array(); |
|
10
|
|
|
|
|
11
|
2 |
|
public function resolve($service, ArgumentBuilder $builder) |
|
12
|
|
|
{ |
|
13
|
2 |
|
$builder->setParams($service->getParams()); |
|
14
|
|
|
|
|
15
|
2 |
|
$arguments = $builder->getArguments(); |
|
16
|
|
|
|
|
17
|
2 |
|
if (!isset($this->instances[$service->getName()])) { |
|
18
|
2 |
|
$refObj = new ReflectionClass($service->getClass()); |
|
19
|
2 |
|
$this->instances[$service->getName()] = $refObj->newInstanceArgs($arguments); |
|
20
|
2 |
|
} |
|
21
|
|
|
|
|
22
|
2 |
|
return $this->instances[$service->getName()]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
5 |
|
public function simpleResolver($service) |
|
26
|
|
|
{ |
|
27
|
5 |
|
if ($service->classNotExists()) { |
|
28
|
1 |
|
throw new \RuntimeException( |
|
29
|
1 |
|
'Oops! Class ' . $service->getClass() . |
|
30
|
1 |
|
' defined as ' . $service->getName() . |
|
31
|
|
|
' not found!!!' |
|
32
|
1 |
|
); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
4 |
|
$serviceClass = $service->getClass(); |
|
36
|
|
|
|
|
37
|
4 |
|
return new $serviceClass(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function methods($service) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$resolution = $this->simpleResolver($service); |
|
43
|
|
|
|
|
44
|
1 |
|
foreach ($service->getMethods() as $methodName => $value) { |
|
45
|
1 |
|
$argument = Objects\Argument::fromString($value); |
|
46
|
|
|
|
|
47
|
1 |
|
if ($argument->isService()) { |
|
48
|
1 |
|
$collabortor = Objects\Service::box(array( |
|
49
|
1 |
|
'name' => $argument->getServiceName(), |
|
50
|
1 |
|
'services' => $service->getServicesConfiguration(), |
|
51
|
1 |
|
)); |
|
52
|
|
|
|
|
53
|
1 |
|
$resolution->$methodName( |
|
54
|
1 |
|
$this->simpleResolver($collabortor) |
|
55
|
1 |
|
); |
|
56
|
1 |
|
} else { |
|
57
|
|
|
$resolution->$methodName($value); |
|
58
|
|
|
} |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return $resolution; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|