|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Util\Console\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Magento\Framework\ObjectManager\ObjectManager; |
|
6
|
|
|
use Symfony\Component\Console\Helper\Helper as AbstractHelper; |
|
7
|
|
|
|
|
8
|
|
|
class InjectionHelper extends AbstractHelper |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Returns the canonical name of this helper. |
|
12
|
|
|
* |
|
13
|
|
|
* @return string The canonical name |
|
14
|
|
|
* |
|
15
|
|
|
* @api |
|
16
|
|
|
*/ |
|
17
|
|
|
public function getName() |
|
18
|
|
|
{ |
|
19
|
|
|
return 'injection'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Object $object |
|
24
|
|
|
* @param string $methodName |
|
25
|
|
|
*/ |
|
26
|
|
|
public function methodInjection($object, $methodName, ObjectManager $objectManager) |
|
27
|
|
|
{ |
|
28
|
|
|
$parameters = $this->getMethod($object, $methodName); |
|
|
|
|
|
|
29
|
|
|
$argumentsToInject = array_map([$objectManager, 'get'], $parameters); |
|
30
|
|
|
|
|
31
|
|
|
call_user_func_array([$object, $methodName], $argumentsToInject); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function constructorInjection($class, ObjectManager $objectManager) |
|
35
|
|
|
{ |
|
36
|
|
|
$parameters = $this->getMethod($class, '__construct'); |
|
37
|
|
|
$argumentsToInject = array_map([$objectManager, 'get'], $parameters); |
|
38
|
|
|
|
|
39
|
|
|
$refl = new \ReflectionClass($class); |
|
40
|
|
|
$object = $refl->newInstanceArgs($argumentsToInject); |
|
41
|
|
|
|
|
42
|
|
|
return $object; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Read class method signature |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $class |
|
49
|
|
|
* @param string $methodName |
|
50
|
|
|
* @return array |
|
51
|
|
|
* @throws \ReflectionException |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getMethod($class, $methodName) |
|
54
|
|
|
{ |
|
55
|
|
|
$refl = new \ReflectionClass($class); |
|
56
|
|
|
if (!$refl->hasMethod($methodName)) { |
|
57
|
|
|
return []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$method = $refl->getMethod($methodName); |
|
61
|
|
|
if (!$method) { |
|
62
|
|
|
throw new \InvalidArgumentException( |
|
63
|
|
|
sprintf("Unable to obtain method \"%s\" for class \"%s\"", $class, $methodName) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$result = array_map([$this, 'getParameterClass'], $method->getParameters()); |
|
68
|
|
|
|
|
69
|
|
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function getParameterClass(\ReflectionParameter $parameter) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
return $parameter->getClass() !== null ? $parameter->getClass()->getName() : null; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: