Skip to content

Commit 131a24c

Browse files
committed
Add Dependency Injector on method
1 parent e45c388 commit 131a24c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

Reflection.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Reflection
1414
private $reflect;
1515
private $args;
1616
private $allowInterfaces = true;
17+
private $dependMethod = null;
1718

1819

1920
private static $class = array();
@@ -66,14 +67,13 @@ public function allowInterfaces(bool $bool): void
6667
* Call dependency injector
6768
* @return object
6869
*/
69-
public function dependencyInjector(): object
70+
public function dependencyInjector(?object $class = null, ?string $method = null): object
7071
{
7172
$args = array();
72-
$constructor = $this->reflect->getConstructor();
73+
$constructor = $this->setDependMethod($method, $this->reflect);
7374
if(!is_null($constructor)) {
7475
$params = $constructor->getParameters();
7576
$this->injectRecursion($params, $this->reflect->getName());
76-
7777
foreach ($params as $param) {
7878
if ($param->getType() && !$param->getType()->isBuiltin()) {
7979
$classKey = $param->getType()->getName();
@@ -83,9 +83,23 @@ public function dependencyInjector(): object
8383
}
8484
}
8585
}
86+
if(!is_null($this->dependMethod)) {
87+
$this->dependMethod = null;
88+
return $constructor->invokeArgs($class, $args);
89+
}
8690
return $this->reflect->newInstanceArgs($args);
8791
}
8892

93+
function setDependMethod(?string $method, ReflectionClass $inst)
94+
{
95+
$method = ($method === "constructor") ? null : $method;
96+
$this->dependMethod = $method;
97+
if(is_null($this->dependMethod)) {
98+
return $inst->getConstructor();
99+
}
100+
return $inst->getMethod($this->dependMethod);
101+
}
102+
89103
/**
90104
* This will return reflection if class exist or error pointing to file where error existed,
91105
* @param class-string $className
@@ -121,6 +135,7 @@ private function injectRecursion(array $params, string $fromClass, array $_args
121135
$inst = $this->initReclusiveReflect($classNameA, $fromClass);
122136
$reflectParam = array();
123137
$constructor = $inst->getConstructor();
138+
124139
if (!$inst->isInterface()) {
125140
$reflectParam = ($constructor) ? $constructor->getParameters() : [];
126141
}

0 commit comments

Comments
 (0)