|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Venta\Container; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use ReflectionMethod; |
|
7
|
|
|
use Venta\Contracts\Container\ArgumentResolver as ArgumentResolverContract; |
|
8
|
|
|
use Venta\Contracts\Container\ServiceInflector as ServiceInflectorContract; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class ServiceInflector |
|
12
|
|
|
* |
|
13
|
|
|
* @package Venta\Container |
|
14
|
|
|
*/ |
|
15
|
|
|
final class ServiceInflector implements ServiceInflectorContract |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ArgumentResolverContract |
|
20
|
|
|
*/ |
|
21
|
|
|
private $argumentResolver; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A list of methods with arguments which will be invoked on the subject object. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string[][] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $inflections = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* ServiceInflector constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param ArgumentResolverContract $argumentResolver |
|
34
|
|
|
*/ |
|
35
|
47 |
|
public function __construct(ArgumentResolverContract $argumentResolver) |
|
36
|
|
|
{ |
|
37
|
47 |
|
$this->argumentResolver = $argumentResolver; |
|
38
|
47 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function addInflection(string $id, string $method, array $arguments = []) |
|
44
|
|
|
{ |
|
45
|
5 |
|
if (!method_exists($id, $method)) { |
|
46
|
1 |
|
throw new InvalidArgumentException(sprintf('Method "%s" not found in "%s".', $method, $id)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
4 |
|
$this->inflections[$id][$method] = $arguments; |
|
50
|
4 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritDoc |
|
54
|
|
|
*/ |
|
55
|
28 |
|
public function inflect($object) |
|
56
|
|
|
{ |
|
57
|
28 |
|
foreach ($this->inflections as $type => $methods) { |
|
58
|
4 |
|
if (!$object instanceof $type) { |
|
59
|
3 |
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
foreach ($methods as $method => $inflection) { |
|
63
|
|
|
// $inflection may be array of arguments to pass to the method |
|
64
|
|
|
// OR prepared closure to call with the provided object context. |
|
65
|
4 |
|
if (is_array($inflection)) { |
|
66
|
|
|
|
|
67
|
|
|
// Reflect and resolve method arguments. |
|
68
|
4 |
|
$arguments = $this->argumentResolver->resolve(new ReflectionMethod($type, $method), $inflection); |
|
69
|
|
|
|
|
70
|
|
|
// Wrap calling method with closure to avoid reflecting / resolving each time inflection applied. |
|
71
|
3 |
|
$this->inflections[$type][$method] = $inflection = function () use ($method, $arguments) { |
|
72
|
3 |
|
$this->$method(...$arguments); |
|
73
|
3 |
|
}; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// We have callable inflection ready, so we simply swap the context to provided object and call it. |
|
77
|
3 |
|
$inflection->call($object); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
27 |
|
return $object; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |