1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zewa; |
4
|
|
|
|
5
|
|
|
use Zewa\Exception\Exception; |
6
|
|
|
|
7
|
|
|
class Dependency |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Container |
11
|
|
|
*/ |
12
|
|
|
private $dependencies; |
13
|
|
|
|
14
|
|
|
public function __construct(Config $config, Container $container) |
15
|
|
|
{ |
16
|
|
|
$local = [ |
17
|
|
|
'\Zewa\Config' => $config, |
18
|
|
|
'\Zewa\Container' => $container, |
19
|
|
|
'\Zewa\Dependency' => $this |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
$this->dependencies = $container; |
23
|
|
|
$this->dependencies->set('__dependencies', $local); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
private function isDependencyLoaded(string $class) : bool |
27
|
|
|
{ |
28
|
|
|
$dependencies = $this->dependencies->get('__dependencies'); |
29
|
|
|
|
30
|
|
|
if (isset($dependencies[$class]) === false) { |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return true; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function getDependency(string $class) |
38
|
|
|
{ |
39
|
|
|
$dependencies = $this->dependencies->get('__dependencies'); |
40
|
|
|
return $dependencies[$class] ?? null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function load(string $class, $dependency, bool $persist) |
44
|
|
|
{ |
45
|
|
|
if ($persist === true) { |
46
|
|
|
$dependencies = $this->dependencies->get('__dependencies'); |
47
|
|
|
$dependencies[$class] = $dependency; |
48
|
|
|
$this->dependencies->set('__dependencies', $dependencies); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// $this->injectAppInstance($dependency); |
|
|
|
|
52
|
|
|
return $dependency; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function resolve($class, $persist = false) |
56
|
|
|
{ |
57
|
|
|
if ($this->isDependencyLoaded($class)) { |
58
|
|
|
return $this->getDependency($class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
$reflectionClass = new \ReflectionClass($class); |
63
|
|
|
|
64
|
|
|
$constructor = $reflectionClass->getConstructor(); |
65
|
|
|
|
66
|
|
|
if (!$constructor || $constructor->getParameters() === 0) { |
67
|
|
|
$dependency = new $class; |
68
|
|
|
return $this->load($class, $dependency, $persist); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$params = $this->constructConstructorParameters($reflectionClass); |
72
|
|
|
$dependency = $reflectionClass->newInstanceArgs($params); |
73
|
|
|
|
74
|
|
|
return $this->load($class, $dependency, $persist); |
75
|
|
|
} catch (\Exception $e) { |
76
|
|
|
echo "<PRE>"; |
77
|
|
|
print_r($e->getMessage()); //->getMessage(); |
78
|
|
|
var_dump($e->getTrace()[0]); |
|
|
|
|
79
|
|
|
echo "</PRE>"; |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param \ReflectionClass $reflection |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
private function constructConstructorParameters(\ReflectionClass $reflection) : array |
89
|
|
|
{ |
90
|
|
|
$constructor = $reflection->getConstructor(); |
91
|
|
|
$params = $constructor->getParameters(); |
92
|
|
|
|
93
|
|
|
$injection = []; |
94
|
|
|
|
95
|
|
View Code Duplication |
foreach ($params as $param) { |
|
|
|
|
96
|
|
|
// Here we should perform a bunch of checks, such as: isArray(), isCallable(), isDefaultValueAvailable() |
97
|
|
|
// isOptional() etc. |
98
|
|
|
if (is_null($param->getClass())) { |
99
|
|
|
$injection[] = null; |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
$injection[] = $this->resolve("\\" . $param->getClass()->getName()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $injection; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
private function injectAppInstance($dependency) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
if ($dependency instanceof Controller) { |
112
|
|
|
$dependency->setEvent($this->resolve('\Sabre\Event\Emitter')); |
|
|
|
|
113
|
|
|
$dependency->setRequest($this->resolve('\Zewa\HTTP\Request')); |
|
|
|
|
114
|
|
|
$dependency->setRouter($this->resolve('\Zewa\Router')); |
|
|
|
|
115
|
|
|
$dependency->setConfig($this->resolve('\Zewa\Config')); |
|
|
|
|
116
|
|
|
$dependency->setDependency($this->resolve('\Zewa\Dependency')); |
|
|
|
|
117
|
|
|
$dependency->setView($this->resolve('\Zewa\View')); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.