|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Spiral Framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Storage; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Http\Message\UriInterface; |
|
15
|
|
|
use Spiral\Storage\Exception\InvalidArgumentException; |
|
16
|
|
|
use Spiral\Storage\Manager\ReadableTrait; |
|
17
|
|
|
use Spiral\Storage\Manager\WritableTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @psalm-type UriType = string | UriInterface | \Stringable |
|
21
|
|
|
* @see UriInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
final class Manager implements MutableManagerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use ReadableTrait; |
|
26
|
|
|
use WritableTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
public const DEFAULT_STORAGE = 'default'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private const ERROR_REDEFINITION = 'Can not redefine already defined storage `%s`'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private const ERROR_NOT_FOUND = 'Storage `%s` has not been defined'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array<string, StorageInterface> |
|
45
|
|
|
*/ |
|
46
|
|
|
private $storages = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $default; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(string $name = self::DEFAULT_STORAGE) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->default = $name; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $name |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
|
|
public function withDefault(string $name): ManagerInterface |
|
66
|
|
|
{ |
|
67
|
|
|
$self = clone $this; |
|
68
|
|
|
$self->default = $name; |
|
69
|
|
|
|
|
70
|
|
|
return $self; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* {@inheritDoc} |
|
75
|
|
|
*/ |
|
76
|
|
|
public function storage(string $name = null): StorageInterface |
|
77
|
|
|
{ |
|
78
|
|
|
$name = $name ?? $this->default; |
|
79
|
|
|
|
|
80
|
|
|
if (!isset($this->storages[$name])) { |
|
81
|
|
|
throw new InvalidArgumentException(\sprintf(self::ERROR_NOT_FOUND, $name)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->storages[$name]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritDoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function add(string $name, StorageInterface $storage, bool $overwrite = false): void |
|
91
|
|
|
{ |
|
92
|
|
|
if ($overwrite === false && isset($this->storages[$name])) { |
|
93
|
|
|
throw new \InvalidArgumentException(\sprintf(self::ERROR_REDEFINITION, $name)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->storages[$name] = $storage; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritDoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getIterator(): \Traversable |
|
103
|
|
|
{ |
|
104
|
|
|
return new \ArrayIterator($this->storages); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* {@inheritDoc} |
|
109
|
|
|
*/ |
|
110
|
|
|
public function count(): int |
|
111
|
|
|
{ |
|
112
|
|
|
return \count($this->storages); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param UriType $uri |
|
|
|
|
|
|
117
|
|
|
* @param bool $withScheme |
|
118
|
|
|
* @return array{0: string|null, 1: string} |
|
119
|
|
|
* @throws InvalidArgumentException |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function parseUri($uri, bool $withScheme = true): array |
|
122
|
|
|
{ |
|
123
|
|
|
$uri = $this->uriToString($uri); |
|
124
|
|
|
$result = \parse_url($uri); |
|
125
|
|
|
|
|
126
|
|
|
if ($result === false) { |
|
127
|
|
|
$message = 'URI argument must be a valid URI in "[STORAGE]://[PATH_TO_FILE]" format, but `%s` given'; |
|
128
|
|
|
throw new InvalidArgumentException(\sprintf($message, $uri)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (!isset($result['scheme'])) { |
|
132
|
|
|
$result['scheme'] = $withScheme ? $this->default : null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (!isset($result['host'])) { |
|
136
|
|
|
$result['host'] = ''; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return [ |
|
140
|
|
|
$result['scheme'] ?? null, |
|
141
|
|
|
$result['host'] . \rtrim($result['path'] ?? '', '/'), |
|
142
|
|
|
]; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param UriType $uri |
|
147
|
|
|
* @return string |
|
148
|
|
|
* @throws InvalidArgumentException |
|
149
|
|
|
*/ |
|
150
|
|
|
private function uriToString($uri): string |
|
151
|
|
|
{ |
|
152
|
|
|
switch (true) { |
|
153
|
|
|
case $uri instanceof UriInterface: |
|
154
|
|
|
case $uri instanceof \Stringable: |
|
155
|
|
|
case \is_object($uri) && \method_exists($uri, '__toString'): |
|
156
|
|
|
return (string)$uri; |
|
157
|
|
|
|
|
158
|
|
|
case \is_string($uri): |
|
159
|
|
|
return $uri; |
|
160
|
|
|
|
|
161
|
|
|
default: |
|
162
|
|
|
$message = 'URI must be a string or instance of Stringable interface, but %s given'; |
|
163
|
|
|
throw new InvalidArgumentException(\sprintf($message, \get_debug_type($uri))); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths