|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* webtrees: online genealogy |
|
4
|
|
|
* Copyright (C) 2018 webtrees development team |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU General Public License for more details. |
|
13
|
|
|
* You should have received a copy of the GNU General Public License |
|
14
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15
|
|
|
*/ |
|
16
|
|
|
namespace Fisharebest\Webtrees; |
|
17
|
|
|
|
|
18
|
|
|
use ReflectionClass; |
|
19
|
|
|
use ReflectionMethod; |
|
20
|
|
|
use ReflectionParameter; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Simple dependency injection. |
|
24
|
|
|
*/ |
|
25
|
|
|
class Resolver |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var object[] */ |
|
28
|
|
|
private $bindings = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* For some classes (e.g. Request, Tree, User), we inject a specific instance |
|
32
|
|
|
* of an object, rather than a newly instantiated object |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $class |
|
35
|
|
|
* @param object $object |
|
36
|
|
|
*/ |
|
37
|
|
|
public function bind(string $class, $object) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->bindings[$class] = $object; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create an instance of a class, injecting all its dependencies. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $class |
|
46
|
|
|
* |
|
47
|
|
|
* @return object |
|
48
|
|
|
*/ |
|
49
|
|
|
public function resolve(string $class) |
|
50
|
|
|
{ |
|
51
|
|
|
if (array_key_exists($class, $this->bindings)) { |
|
52
|
|
|
return $this->bindings[$class]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$reflector = new ReflectionClass($class); |
|
56
|
|
|
|
|
57
|
|
|
$constructor = $reflector->getConstructor(); |
|
58
|
|
|
|
|
59
|
|
|
// No constructor? Nothing to inject. |
|
60
|
|
|
if ($constructor === null) { |
|
61
|
|
|
return new $class; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$parameters = $this->resolveParameters($constructor->getParameters()); |
|
65
|
|
|
|
|
66
|
|
|
return $reflector->newInstanceArgs($parameters); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Call an object's method, injecting all its dependencies. |
|
71
|
|
|
* |
|
72
|
|
|
* @param object $object |
|
73
|
|
|
* @param string $method |
|
74
|
|
|
* |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
|
|
public function dispatch($object, string $method) |
|
78
|
|
|
{ |
|
79
|
|
|
$reflector = new ReflectionMethod($object, $method); |
|
80
|
|
|
|
|
81
|
|
|
$parameters = $this->resolveParameters($reflector->getParameters()); |
|
82
|
|
|
|
|
83
|
|
|
return $reflector->invoke($object, ...$parameters); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param array $parameters |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
private function resolveParameters(array $parameters): array |
|
92
|
|
|
{ |
|
93
|
|
|
return array_map(function (ReflectionParameter $parameter) { |
|
94
|
|
|
return $this->resolve($parameter->getClass()->name); |
|
95
|
|
|
}, $parameters); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|