|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Venta\Container; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Venta\Contracts\Container\Container as ContainerContract; |
|
7
|
|
|
use Venta\Contracts\Container\Invoker as InvokerContract; |
|
8
|
|
|
use Venta\Contracts\Container\ServiceDecorator as ServiceDecoratorContract; |
|
9
|
|
|
use Venta\Contracts\Container\ServiceInflector as ServiceInflectorContract; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ServiceDecorator |
|
13
|
|
|
* |
|
14
|
|
|
* @package Venta\Contracts\Container |
|
15
|
|
|
*/ |
|
16
|
|
|
final class ServiceDecorator implements ServiceDecoratorContract |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var ContainerContract |
|
20
|
|
|
*/ |
|
21
|
|
|
private $container; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Array of decorator definitions. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Invokable[][]|string[][] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $decorators = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var ServiceInflectorContract |
|
32
|
|
|
*/ |
|
33
|
|
|
private $inflector; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var InvokerContract |
|
37
|
|
|
*/ |
|
38
|
|
|
private $invoker; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* ServiceDecorator constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param ContainerContract $container |
|
44
|
|
|
* @param ServiceInflectorContract $inflector |
|
45
|
|
|
* @param InvokerContract $invoker |
|
46
|
|
|
*/ |
|
47
|
45 |
|
public function __construct( |
|
48
|
|
|
ContainerContract $container, |
|
49
|
|
|
ServiceInflectorContract $inflector, |
|
50
|
|
|
InvokerContract $invoker |
|
51
|
|
|
) { |
|
52
|
45 |
|
$this->container = $container; |
|
53
|
45 |
|
$this->inflector = $inflector; |
|
54
|
45 |
|
$this->invoker = $invoker; |
|
55
|
45 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
4 |
|
public function addDecorator(string $id, $decorator) |
|
61
|
|
|
{ |
|
62
|
4 |
|
if (is_string($decorator)) { |
|
63
|
1 |
|
if (!class_exists($decorator)) { |
|
64
|
1 |
|
throw new InvalidArgumentException(sprintf('Invalid decorator class "%s" provided.', $decorator)); |
|
65
|
|
|
} |
|
66
|
|
|
} else { |
|
67
|
3 |
|
$decorator = new Invokable($decorator); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
$this->decorators[$id][] = $decorator; |
|
71
|
4 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $id |
|
76
|
|
|
* @param $object |
|
77
|
|
|
* @param bool $once |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
29 |
|
public function decorate(string $id, $object, bool $once = false) |
|
81
|
|
|
{ |
|
82
|
29 |
|
if (empty($this->decorators[$id])) { |
|
83
|
27 |
|
return $object; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
foreach ($this->decorators[$id] as $decorator) { |
|
87
|
4 |
|
$object = $decorator instanceof Invokable |
|
88
|
3 |
|
? $this->invoker->invoke($decorator, [$object]) |
|
89
|
4 |
|
: $this->container->get($decorator, [$object]); |
|
90
|
4 |
|
$this->inflector->inflect($object); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
if ($once) { |
|
94
|
1 |
|
unset($this->decorators[$id]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
return $object; |
|
98
|
|
|
} |
|
99
|
|
|
} |