-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProxyContainer.php
71 lines (60 loc) · 2.03 KB
/
ProxyContainer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Dhii\Container\Exception\ContainerException;
use Dhii\Container\Util\StringTranslatingTrait;
use Psr\Container\ContainerInterface as BaseContainerInterface;
/**
* A proxy for another container, and nothing more.
*
* The advantage is that its setter can be used at any point after construction,
* which solves the chicken-egg problem of two co-dependent containers.
*/
class ProxyContainer implements BaseContainerInterface
{
use StringTranslatingTrait;
/**
* @var ?BaseContainerInterface
*/
protected $innerContainer;
/**
* @param BaseContainerInterface|null $innerContainer The inner container, if any.
* May also be set later with {@see setInnerContainer()}.
*/
public function __construct(BaseContainerInterface $innerContainer = null)
{
$this->innerContainer = $innerContainer;
}
/**
* @inheritDoc
*/
public function get(string $key)
{
if (!($this->innerContainer instanceof BaseContainerInterface)) {
throw new ContainerException($this->__('Inner container not set'));
}
return $this->innerContainer->get($key);
}
/**
* @inheritDoc
*/
public function has(string $key): bool
{
if (!($this->innerContainer instanceof BaseContainerInterface)) {
/** @psalm-suppress MissingThrowsDocblock The exception class implements declared thrown interface */
throw new ContainerException($this->__('Inner container not set'));
}
return $this->innerContainer->has($key);
}
/**
* Assigns an inner container tot his proxy.
*
* Calls to `has()` and `get()` will be forwarded to this inner container.
*
* @param BaseContainerInterface $innerContainer The inner container to proxy.
*/
public function setInnerContainer(BaseContainerInterface $innerContainer): void
{
$this->innerContainer = $innerContainer;
}
}