|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TaskQueue\Invoker; |
|
4
|
|
|
|
|
5
|
|
|
use DependencyInjection\Container; |
|
6
|
|
|
use TaskQueue\Invoker\Exception\ArrayPairLengthAwareException; |
|
7
|
|
|
use TaskQueue\Invoker\Exception\ClassInstanceException; |
|
8
|
|
|
use TaskQueue\Invoker\Exception\ClassMethodException; |
|
9
|
|
|
|
|
10
|
|
|
class MethodInvoker implements InvokerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
private $method; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Container $container, $args) |
|
18
|
|
|
{ |
|
19
|
|
|
if (!is_array($args)) { |
|
20
|
|
|
throw new \InvalidArgumentException( |
|
21
|
|
|
sprintf("Parameter 1 of %s must be an array.", __METHOD__) |
|
22
|
|
|
); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
if (sizeof($args) !== 2) { |
|
26
|
|
|
throw new ArrayPairLengthAwareException( |
|
27
|
|
|
sprintf("Parameter 1 of %s must be an array with length === 2.", __METHOD__) |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if (!isset($args['instance'])) { |
|
32
|
|
|
throw new ClassInstanceException("\$args index key 'instance' must exists."); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (!isset($args['method'])) { |
|
36
|
|
|
throw new ClassMethodException("\$args index key 'method' must exists."); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$args['instance'] = (!is_object($args['instance']) |
|
40
|
|
|
? (class_exists($args['instance']) |
|
41
|
|
|
? $container->make($args['instance']) |
|
42
|
|
|
: null) |
|
43
|
|
|
: $args['instance']); |
|
44
|
|
|
|
|
45
|
|
|
$this->method = $args; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function invoke() |
|
52
|
|
|
{ |
|
53
|
|
|
return call_user_func_array( |
|
54
|
|
|
[$this->method['instance'], $this->method['method']], func_get_args() |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function invokeWithArgs($args) |
|
62
|
|
|
{ |
|
63
|
|
|
if (!is_array($args)) { |
|
64
|
|
|
throw new \InvalidArgumentException( |
|
65
|
|
|
sprintf("Parameter 1 of %s must be an array.", __METHOD__) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return call_user_func_array( |
|
70
|
|
|
[$this->method['instance'], $this->method['method']], $args |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|