1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simply\Container\Entry; |
4
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Container entry that loads the value using a separate entry provider. |
9
|
|
|
* |
10
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
12
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
class ProviderEntry implements EntryInterface |
15
|
|
|
{ |
16
|
|
|
/** @var string The provider class used to load the value */ |
17
|
|
|
private $provider; |
18
|
|
|
|
19
|
|
|
/** @var string The method called for the provider class to load the value */ |
20
|
|
|
private $method; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ProviderEntry constructor. |
24
|
|
|
* @param callable $providerMethod The provider callable provided as an array |
25
|
|
|
*/ |
26
|
3 |
|
public function __construct(callable $providerMethod) |
27
|
|
|
{ |
28
|
3 |
|
if (!\is_array($providerMethod)) { |
29
|
1 |
|
throw new \InvalidArgumentException('The provider entry callable must be provided as an array'); |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
[$provider, $method] = $providerMethod; |
33
|
|
|
|
34
|
2 |
|
$this->setProvider($provider); |
35
|
2 |
|
$this->setMethod($method); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets the provider class based on the given provider instance. |
40
|
|
|
* @param object $provider The provider instance |
41
|
|
|
*/ |
42
|
2 |
|
private function setProvider(object $provider): void |
43
|
|
|
{ |
44
|
2 |
|
$this->provider = \get_class($provider); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sets the method to call for the provider instance. |
49
|
|
|
* @param string $method The method to call for the provider |
50
|
|
|
*/ |
51
|
2 |
|
private function setMethod(string $method): void |
52
|
|
|
{ |
53
|
2 |
|
$this->method = $method; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** {@inheritdoc} */ |
57
|
1 |
|
public static function createFromCacheParameters(array $parameters): EntryInterface |
58
|
|
|
{ |
59
|
|
|
/** @var ProviderEntry $entry */ |
60
|
1 |
|
$entry = (new \ReflectionClass(static::class))->newInstanceWithoutConstructor(); |
61
|
|
|
|
62
|
|
|
[ |
63
|
1 |
|
$entry->provider, |
64
|
1 |
|
$entry->method, |
65
|
1 |
|
] = $parameters; |
66
|
|
|
|
67
|
1 |
|
return $entry; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** {@inheritdoc} */ |
71
|
1 |
|
public function getCacheParameters(): array |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
1 |
|
$this->provider, |
75
|
1 |
|
$this->method, |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** {@inheritdoc} */ |
80
|
2 |
|
public function isFactory(): bool |
81
|
|
|
{ |
82
|
2 |
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** {@inheritdoc} */ |
86
|
2 |
|
public function getValue(ContainerInterface $container) |
87
|
|
|
{ |
88
|
2 |
|
$provider = $container->get($this->provider); |
89
|
|
|
|
90
|
2 |
|
return $provider->{$this->method}($container); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|