|
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 Spiral\Core\Container\SingletonInterface; |
|
15
|
|
|
use Spiral\Storage\Config\ConfigInterface; |
|
16
|
|
|
use Spiral\Storage\Config\DTO\FileSystemInfo\FileSystemInfoInterface; |
|
17
|
|
|
use Spiral\Storage\Config\StorageConfig; |
|
18
|
|
|
use Spiral\Storage\Exception\ResolveException; |
|
19
|
|
|
use Spiral\Storage\Exception\StorageException; |
|
20
|
|
|
use Spiral\Storage\Parser\UriParser; |
|
21
|
|
|
use Spiral\Storage\Parser\UriParserInterface; |
|
22
|
|
|
use Spiral\Storage\Resolver\AdapterResolverInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @psalm-import-type UriLikeType from UriResolverInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
class UriResolver implements UriResolverInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ConfigInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $config; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var UriParserInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $parser; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array<AdapterResolverInterface> |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $resolvers = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param ConfigInterface $config |
|
46
|
|
|
* @param UriParserInterface|null $uriParser |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct(ConfigInterface $config, UriParserInterface $parser = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->config = $config; |
|
51
|
|
|
$this->parser = $parser ?? new UriParser(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritDoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function resolveAll(iterable $uris): iterable |
|
58
|
|
|
{ |
|
59
|
|
|
foreach ($uris as $uri) { |
|
60
|
|
|
yield $this->resolve($uri); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function resolve($uri) |
|
68
|
|
|
{ |
|
69
|
|
|
try { |
|
70
|
|
|
$uri = $this->parser->parse($uri); |
|
71
|
|
|
|
|
72
|
|
|
return $this->getResolver($uri->getFileSystem()) |
|
73
|
|
|
->buildUrl($uri->getPath()) |
|
74
|
|
|
; |
|
75
|
|
|
} catch (StorageException $e) { |
|
76
|
|
|
throw $e; |
|
77
|
|
|
} catch (\Throwable $e) { |
|
78
|
|
|
throw new ResolveException($e->getMessage(), (int)$e->getCode(), $e); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get resolver for filesystem by key |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $fileSystem |
|
86
|
|
|
* |
|
87
|
|
|
* @return AdapterResolverInterface |
|
88
|
|
|
* |
|
89
|
|
|
* @throws StorageException |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function getResolver(string $fileSystem): AdapterResolverInterface |
|
92
|
|
|
{ |
|
93
|
|
|
if (!\array_key_exists($fileSystem, $this->resolvers)) { |
|
94
|
|
|
$this->resolvers[$fileSystem] = $this->prepareResolverForFileSystem( |
|
95
|
|
|
$this->config->buildFileSystemInfo($fileSystem) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->resolvers[$fileSystem]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Prepare resolver by provided filesystem info |
|
104
|
|
|
* |
|
105
|
|
|
* @param FileSystemInfoInterface $fsInfo |
|
106
|
|
|
* |
|
107
|
|
|
* @return AdapterResolverInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function prepareResolverForFileSystem(FileSystemInfoInterface $fsInfo): AdapterResolverInterface |
|
110
|
|
|
{ |
|
111
|
|
|
$resolver = $fsInfo->getResolverClass(); |
|
112
|
|
|
|
|
113
|
|
|
return new $resolver($this->parser, $this->config, $fsInfo->getName()); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|