1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zewa; |
4
|
|
|
|
5
|
|
|
use Zewa\Exception\Exception; |
6
|
|
|
use Zewa\Exception\LookupException; |
7
|
|
|
|
8
|
|
|
class Dependency |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Container |
12
|
|
|
*/ |
13
|
|
|
private $dependencies; |
14
|
|
|
|
15
|
38 |
|
public function __construct(Config $config, Container $container) |
16
|
|
|
{ |
17
|
|
|
$local = [ |
18
|
38 |
|
'\Zewa\Config' => $config, |
19
|
38 |
|
'\Zewa\Container' => $container, |
20
|
38 |
|
'\Zewa\Dependency' => $this |
21
|
|
|
]; |
22
|
|
|
|
23
|
38 |
|
$this->dependencies = $container; |
24
|
38 |
|
$this->dependencies->set('__dependencies', $local); |
25
|
38 |
|
} |
26
|
|
|
|
27
|
1 |
View Code Duplication |
public function flushDependency(string $class) |
|
|
|
|
28
|
|
|
{ |
29
|
1 |
|
$dependencies = $this->dependencies->get('__dependencies'); |
30
|
|
|
|
31
|
1 |
|
if (isset($dependencies[$class]) === true) { |
32
|
1 |
|
unset($dependencies[$class]); |
33
|
1 |
|
$this->dependencies->set('__dependencies', $dependencies); |
34
|
|
|
} |
35
|
1 |
|
} |
36
|
|
|
|
37
|
37 |
|
public function isDependencyLoaded(string $class) : bool |
38
|
|
|
{ |
39
|
37 |
|
$dependencies = $this->dependencies->get('__dependencies'); |
40
|
|
|
|
41
|
37 |
|
if (isset($dependencies[$class]) === false) { |
42
|
36 |
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
34 |
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
33 |
|
public function getDependency(string $class) |
49
|
|
|
{ |
50
|
33 |
|
$dependencies = $this->dependencies->get('__dependencies'); |
51
|
33 |
|
return $dependencies[$class] ?? null; |
52
|
|
|
} |
53
|
|
|
|
54
|
34 |
View Code Duplication |
private function load(string $class, $dependency, bool $persist) |
|
|
|
|
55
|
|
|
{ |
56
|
34 |
|
if ($persist === true) { |
57
|
33 |
|
$dependencies = $this->dependencies->get('__dependencies'); |
58
|
33 |
|
$dependencies[$class] = $dependency; |
59
|
33 |
|
$this->dependencies->set('__dependencies', $dependencies); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// $this->injectAppInstance($dependency); |
|
|
|
|
63
|
34 |
|
return $dependency; |
64
|
|
|
} |
65
|
|
|
|
66
|
35 |
|
public function resolve($class, $persist = false) |
67
|
|
|
{ |
68
|
35 |
|
if ($this->isDependencyLoaded($class)) { |
69
|
32 |
|
return $this->getDependency($class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
35 |
|
$reflectionClass = new \ReflectionClass($class); |
74
|
|
|
|
75
|
34 |
|
$constructor = $reflectionClass->getConstructor(); |
76
|
|
|
|
77
|
34 |
|
if (!$constructor || $constructor->getParameters() === 0) { |
78
|
33 |
|
$dependency = new $class; |
79
|
33 |
|
return $this->load($class, $dependency, $persist); |
80
|
|
|
} |
81
|
|
|
|
82
|
33 |
|
$params = $this->constructConstructorParameters($reflectionClass); |
83
|
33 |
|
$dependency = $reflectionClass->newInstanceArgs($params); |
84
|
|
|
|
85
|
33 |
|
return $this->load($class, $dependency, $persist); |
86
|
1 |
|
} catch (\ReflectionException $e) { |
87
|
1 |
|
return false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param \ReflectionClass $reflection |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
33 |
|
private function constructConstructorParameters(\ReflectionClass $reflection) : array |
96
|
|
|
{ |
97
|
33 |
|
$constructor = $reflection->getConstructor(); |
98
|
33 |
|
$params = $constructor->getParameters(); |
99
|
|
|
|
100
|
33 |
|
$injection = []; |
101
|
|
|
|
102
|
33 |
|
foreach ($params as $param) { |
103
|
|
|
// Here we should perform a bunch of checks, such as: isArray(), isCallable(), isDefaultValueAvailable() |
104
|
|
|
// isOptional() etc. |
105
|
33 |
|
if (is_null($param->getClass())) { |
106
|
1 |
|
$injection[] = null; |
107
|
1 |
|
continue; |
108
|
|
|
} |
109
|
32 |
|
$injection[] = $this->resolve("\\" . $param->getClass()->getName()); |
110
|
|
|
} |
111
|
|
|
|
112
|
33 |
|
return $injection; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.