-
Notifications
You must be signed in to change notification settings - Fork 3
/
PrefixingContainer.php
142 lines (125 loc) · 4.19 KB
/
PrefixingContainer.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Dhii\Collection\ContainerInterface;
use Dhii\Container\Exception\ContainerException;
use Dhii\Container\Exception\NotFoundException;
use Exception;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use RuntimeException;
/**
* A container implementation that wraps around an inner container and prefixes its keys, requiring consumers to
* include them when fetching or looking up data.
*/
class PrefixingContainer implements ContainerInterface
{
/** @var PsrContainerInterface */
protected $inner;
/** @var string */
protected $prefix;
/** @var bool */
protected $strict;
/**
* @param PsrContainerInterface $container The container whose keys to prefix.
* @param string $prefix The prefix to apply to the container's keys.
* @param bool $strict Whether or not to fallback to un-prefixed keys if a prefixed key does not
* exist in the inner container.
*/
public function __construct(PsrContainerInterface $container, string $prefix, bool $strict = true)
{
$this->inner = $container;
$this->prefix = $prefix;
$this->strict = $strict;
}
/**
* @inheritdoc
*/
public function get(string $key)
{
if (!$this->isPrefixed($key) && $this->strict) {
throw new NotFoundException(sprintf('Key "%s" does not exist', $key));
}
/**
* @psalm-suppress InvalidCatch
* The base interface does not extend Throwable, but in fact everything that is possible
* in theory to catch will be Throwable, and PSR-11 exceptions will implement this interface
*/
try {
return $this->inner->get($this->unprefix($key));
} catch (NotFoundExceptionInterface $nfException) {
if ($this->strict) {
throw $nfException;
}
}
return $this->inner->get($key);
}
/**
* @inheritdoc
*/
public function has(string $key): bool
{
if (!$this->isPrefixed($key) && $this->strict) {
return false;
}
try {
$realKey = $this->unprefix($key);
} catch (Exception $e) {
throw new ContainerException(sprintf('Could not unprefix key "%1$s"', $key), 0, $e);
}
return $this->inner->has($realKey) || (!$this->strict && $this->inner->has($key));
}
/**
* Retrieves the key to use for the inner container.
*
* @param string $key The outer key.
*
* @return string The inner key.
*/
protected function unprefix(string $key): string
{
return $this->isPrefixed($key)
? $this->substring($key, strlen($this->prefix))
: $key;
}
/**
* Checks if the key is prefixed.
*
* @param string $key The key to check.
*
* @return bool True if the key is prefixed, false if not.
*/
protected function isPrefixed(string $key): bool
{
return strlen($this->prefix) > 0 && strpos($key, $this->prefix) === 0;
}
/**
* Extracts a substring from the specified string.
*
* @see substr()
*
* @param string $string The string to extract from.
* @param int $offset The char position, at which to start extraction.
* @param int|null $length The char position, at which to end extraction; unlimited if `null`.
*
* @return string The extracted substring.
*
* @throws RuntimeException If unable to extract.
*/
protected function substring(string $string, int $offset = 0, ?int $length = null): string
{
$length = $length ?? strlen($string) - $offset;
$substring = substr($string, $offset, $length);
if ($substring === false) {
throw new RuntimeException(
sprintf(
'Could not extract substring starting at %1$d of length %2$s from string "%3$s"',
$offset,
$length ?: 'null',
$string
)
);
}
return $substring;
}
}