|
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
|
51 |
|
public function __construct(ContainerContract $container, ArgumentResolverContract $resolver) |
|
35
|
|
|
{ |
|
36
|
51 |
|
$this->container = $container; |
|
37
|
51 |
|
$this->resolver = $resolver; |
|
38
|
51 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
*/ |
|
43
|
17 |
|
public function call($callable, array $arguments = []) |
|
44
|
|
|
{ |
|
45
|
17 |
|
return $this->invoke(new Invokable($callable), $arguments); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritDoc |
|
50
|
|
|
*/ |
|
51
|
38 |
|
public function invoke(Invokable $invokable, array $arguments = []) |
|
52
|
|
|
{ |
|
53
|
38 |
|
$reflection = $invokable->reflection(); |
|
54
|
38 |
|
$arguments = $this->resolver->resolve($reflection, $arguments); |
|
55
|
|
|
|
|
56
|
34 |
|
if ($invokable->isFunction()) { |
|
57
|
|
|
// We have Closure or "functionName" string. |
|
58
|
11 |
|
$callable = $invokable->callable(); |
|
59
|
|
|
|
|
60
|
11 |
|
return $callable(...$arguments); |
|
61
|
|
|
} |
|
62
|
25 |
|
list($object, $method) = $invokable->callable(); |
|
63
|
25 |
|
if ($reflection->isStatic()) { |
|
64
|
4 |
|
return $object::$method(...$arguments); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
21 |
|
if ($method === '__construct') { |
|
68
|
11 |
|
return new $object(...$arguments); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
12 |
|
if (is_string($object)) { |
|
72
|
8 |
|
$object = $this->container->get($object); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
11 |
|
return $object->$method(...$arguments); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritDoc |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function isCallable($callable): bool |
|
82
|
|
|
{ |
|
83
|
|
|
try { |
|
84
|
2 |
|
$invokable = new Invokable($callable); |
|
|
|
|
|
|
85
|
2 |
|
if ($invokable->isFunction()) { |
|
86
|
1 |
|
return true; |
|
87
|
|
|
} |
|
88
|
2 |
|
$object = $invokable->callable()[0]; |
|
89
|
|
|
|
|
90
|
2 |
|
return is_object($object) || $this->container->has($object); |
|
91
|
1 |
|
} catch (InvalidArgumentException $e) { |
|
92
|
1 |
|
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: