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