|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Simply\Container\Entry; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Represents an entry that is wired based on constructor arguments. |
|
9
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
|
11
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
12
|
|
|
*/ |
|
13
|
|
|
class WiredEntry implements EntryInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string The name of the class to instantiate */ |
|
16
|
|
|
private $class; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string[] The identifiers of the container entries to pass to the constructor */ |
|
19
|
|
|
private $arguments; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* WiredEntry constructor. |
|
23
|
|
|
* @param string $class The name of the class to instantiate |
|
24
|
|
|
* @param string[] $arguments The identifiers of the container entries to pass to the constructor |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function __construct(string $class, array $arguments) |
|
27
|
|
|
{ |
|
28
|
2 |
|
$this->class = $class; |
|
29
|
2 |
|
$this->setArguments(... $arguments); |
|
30
|
2 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Sets the identifiers for constructor arguments for the wired entry. |
|
34
|
|
|
* @param string ...$arguments The identifiers for constructor arguments |
|
35
|
|
|
*/ |
|
36
|
2 |
|
private function setArguments(string ... $arguments): void |
|
37
|
|
|
{ |
|
38
|
2 |
|
$this->arguments = $arguments; |
|
39
|
2 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** {@inheritdoc} */ |
|
42
|
1 |
|
public static function createFromCacheParameters(array $parameters): EntryInterface |
|
43
|
|
|
{ |
|
44
|
|
|
[ |
|
45
|
|
|
$class, |
|
46
|
|
|
$arguments, |
|
47
|
1 |
|
] = $parameters; |
|
48
|
|
|
|
|
49
|
1 |
|
return new static($class, $arguments); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** {@inheritdoc} */ |
|
53
|
1 |
|
public function getCacheParameters(): array |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
1 |
|
$this->class, |
|
57
|
1 |
|
$this->arguments, |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** {@inheritdoc} */ |
|
62
|
2 |
|
public function isFactory(): bool |
|
63
|
|
|
{ |
|
64
|
2 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** {@inheritdoc} */ |
|
68
|
2 |
|
public function getValue(ContainerInterface $container) |
|
69
|
|
|
{ |
|
70
|
|
|
$values = array_map(function (string $name) use ($container) { |
|
71
|
2 |
|
return $container->get($name); |
|
72
|
2 |
|
}, $this->arguments); |
|
73
|
|
|
|
|
74
|
2 |
|
return new $this->class(... $values); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|