|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\di; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\orm\parser\Reflexion; |
|
6
|
|
|
use Ubiquity\utils\base\UArray; |
|
7
|
|
|
use Ubiquity\utils\base\UString; |
|
8
|
|
|
use Ubiquity\cache\ClassUtils; |
|
9
|
|
|
use Ubiquity\exceptions\DiException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Parse the controllers for dependency injections. |
|
13
|
|
|
* |
|
14
|
|
|
* Ubiquity\controllers\di$DiControllerParser |
|
15
|
|
|
* This class is part of Ubiquity |
|
16
|
|
|
* |
|
17
|
|
|
* @author jcheron <[email protected]> |
|
18
|
|
|
* @version 1.0.0 |
|
19
|
|
|
* @since Ubiquity 2.1.0 |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
class DiControllerParser { |
|
23
|
|
|
protected $injections = [ ]; |
|
24
|
|
|
|
|
25
|
|
|
public function parse($controllerClass, $config) { |
|
26
|
|
|
$instance = new $controllerClass (); |
|
27
|
|
|
$properties = Reflexion::getProperties ( $instance ); |
|
28
|
|
|
foreach ( $properties as $property ) { |
|
29
|
|
|
$propName = $property->getName (); |
|
30
|
|
|
$annot = Reflexion::getAnnotationMember ( $controllerClass, $propName, "@injected" ); |
|
31
|
|
|
if ($annot !== false) { |
|
32
|
|
|
$name = $annot->name; |
|
33
|
|
|
$this->injections [$propName] = $this->getInjection ( $name ?? $propName, $config,$controllerClass, $annot->code??null ); |
|
34
|
|
|
} else { |
|
35
|
|
|
$annot = Reflexion::getAnnotationMember ( $controllerClass, $propName, "@autowired" ); |
|
36
|
|
|
if ($annot !== false) { |
|
37
|
|
|
$type = Reflexion::getPropertyType ( $controllerClass, $propName ); |
|
38
|
|
|
if ($type !== false) { |
|
39
|
|
|
$this->injections [$propName] = "function(\$controller){return new " . $type . "();}"; |
|
40
|
|
|
}else { |
|
41
|
|
|
throw new DiException ( sprintf ( '%s property has no type and cannot be autowired!', $propName ) ); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
$this->scanGlobalDi ( $config ['di'] ?? [ ], $controllerClass ); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function scanGlobalDi($diConfig, $controller) { |
|
50
|
|
|
$classname = ClassUtils::getClassSimpleName ( $controller ); |
|
51
|
|
|
foreach ( $diConfig as $k => $v ) { |
|
52
|
|
|
if (UString::startswith ( $k, "*." ) || UString::startswith ( $k, $classname . "." )) { |
|
53
|
|
|
$dis = explode ( '.', $k ); |
|
54
|
|
|
$nkey = end ( $dis ); |
|
55
|
|
|
if (property_exists ( $controller, $nkey ) === false) { |
|
56
|
|
|
$this->injections [$nkey] = $v; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function getInjection($name, $config, $controller,$code = null) { |
|
63
|
|
|
if ($code != null) { |
|
64
|
|
|
return "function(\$controller){return " . $code . ";}"; |
|
65
|
|
|
} |
|
66
|
|
|
if (isset ( $config ["di"] )) { |
|
67
|
|
|
$di = $config ['di']; |
|
68
|
|
|
if ($name != null){ |
|
69
|
|
|
$classname = ClassUtils::getClassSimpleName ( $controller ); |
|
70
|
|
|
if(isset ( $di [$name] )) { |
|
71
|
|
|
return $di[$name]; |
|
72
|
|
|
}elseif(isset ( $di [$classname.'.'.$name] )){ |
|
73
|
|
|
return $di[$classname.'.'.$name]; |
|
74
|
|
|
}elseif(isset ( $di ['*.'.$name] )){ |
|
75
|
|
|
return $di['*.'.$name]; |
|
76
|
|
|
}else { |
|
77
|
|
|
throw new \Exception ( "key " . $name . " is not present in config di array" ); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} else { |
|
81
|
|
|
throw new \Exception ( "key di is not present in config array" ); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function __toString() { |
|
86
|
|
|
return "return " . UArray::asPhpArray ( $this->injections, "array" ) . ";"; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* |
|
91
|
|
|
* @return multitype: |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
public function getInjections() { |
|
94
|
|
|
return $this->injections; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths