1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Venta\Container; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Venta\Contracts\Container\ArgumentResolver as ArgumentResolverContract; |
7
|
|
|
use Venta\Contracts\Container\Container as ContainerContract; |
8
|
|
|
use Venta\Contracts\Container\Invoker as InvokerContract; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Invoker |
12
|
|
|
* |
13
|
|
|
* @package Venta\Container |
14
|
|
|
*/ |
15
|
|
|
class Invoker implements InvokerContract |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ContainerContract |
20
|
|
|
*/ |
21
|
|
|
private $container; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ArgumentResolverContract |
25
|
|
|
*/ |
26
|
|
|
private $resolver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Invoker constructor. |
30
|
|
|
* |
31
|
|
|
* @param ContainerContract $container |
32
|
|
|
* @param ArgumentResolverContract $resolver |
33
|
|
|
*/ |
34
|
44 |
|
public function __construct(ContainerContract $container, ArgumentResolverContract $resolver) |
35
|
|
|
{ |
36
|
44 |
|
$this->container = $container; |
37
|
44 |
|
$this->resolver = $resolver; |
38
|
44 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritDoc |
42
|
|
|
*/ |
43
|
15 |
|
public function call($callable, array $arguments = []) |
44
|
|
|
{ |
45
|
15 |
|
return $this->invoke(new Invokable($callable), $arguments); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
32 |
|
public function invoke(Invokable $invokable, array $arguments = []) |
52
|
|
|
{ |
53
|
32 |
|
$reflection = $invokable->reflection(); |
54
|
32 |
|
$arguments = $this->resolver->resolve($reflection, $arguments); |
55
|
|
|
|
56
|
28 |
|
if ($invokable->isFunction()) { |
57
|
|
|
// We have Closure or "functionName" string. |
58
|
9 |
|
$callable = $invokable->callable(); |
59
|
|
|
|
60
|
9 |
|
return $callable(...$arguments); |
61
|
|
|
} |
62
|
21 |
|
list($object, $method) = $invokable->callable(); |
63
|
21 |
|
if ($reflection->isStatic()) { |
64
|
3 |
|
return $object::$method(...$arguments); |
65
|
|
|
} |
66
|
|
|
|
67
|
18 |
|
if ($method === '__construct') { |
68
|
12 |
|
return new $object(...$arguments); |
69
|
|
|
} |
70
|
|
|
|
71
|
11 |
|
if (is_string($object)) { |
72
|
7 |
|
$object = $this->container->get($object); |
73
|
|
|
} |
74
|
|
|
|
75
|
10 |
|
return $object->$method(...$arguments); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
1 |
|
public function isCallable($callable): bool |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
1 |
|
$invokable = new Invokable($callable); |
|
|
|
|
85
|
1 |
|
if ($invokable->isFunction()) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
1 |
|
$object = $invokable->callable()[0]; |
89
|
|
|
|
90
|
1 |
|
return is_object($object) || $this->container->has($object); |
91
|
|
|
} catch (InvalidArgumentException $e) { |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
} |
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: