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\Resolver; |
13
|
|
|
|
14
|
|
|
use Spiral\Storage\Config\ConfigInterface; |
15
|
|
|
use Spiral\Storage\Config\DTO\FileSystemInfo\FileSystemInfoInterface; |
16
|
|
|
use Spiral\Storage\Exception\StorageException; |
17
|
|
|
use Spiral\Storage\Exception\UriException; |
18
|
|
|
use Spiral\Storage\Parser\UriParserInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Abstract class for any resolver |
22
|
|
|
* Depends on adapter by default |
23
|
|
|
*/ |
24
|
|
|
abstract class AbstractAdapterResolver implements AdapterResolverInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Filesystem info class required for resolver |
28
|
|
|
* In case other filesystem info will be provided - exception will be thrown |
29
|
|
|
*/ |
30
|
|
|
protected const FILE_SYSTEM_INFO_CLASS = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var FileSystemInfoInterface |
34
|
|
|
*/ |
35
|
|
|
protected $fsInfo; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var UriParserInterface |
39
|
|
|
*/ |
40
|
|
|
protected $uriParser; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param UriParserInterface $uriParser |
44
|
|
|
* @param ConfigInterface $config |
45
|
|
|
* @param string $fs |
46
|
|
|
* |
47
|
|
|
* @throws StorageException |
48
|
|
|
*/ |
49
|
|
|
public function __construct(UriParserInterface $uriParser, ConfigInterface $config, string $fs) |
50
|
|
|
{ |
51
|
|
|
$requiredClass = static::FILE_SYSTEM_INFO_CLASS; |
52
|
|
|
|
53
|
|
|
$fsInfo = $config->buildFileSystemInfo($fs); |
54
|
|
|
|
55
|
|
|
if (empty($requiredClass) || !$fsInfo instanceof $requiredClass) { |
|
|
|
|
56
|
|
|
throw new StorageException( |
57
|
|
|
\sprintf( |
58
|
|
|
'Wrong filesystem info (`%s`) for resolver `%s`', |
59
|
|
|
get_class($fsInfo), |
60
|
|
|
static::class |
61
|
|
|
) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->uriParser = $uriParser; |
66
|
|
|
|
67
|
|
|
$this->fsInfo = $fsInfo; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Normalize filepath for filesystem operation |
72
|
|
|
* In case uri provided path to file will be extracted |
73
|
|
|
* In case filepath provided it will be returned |
74
|
|
|
* |
75
|
|
|
* @param string $filePath |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function normalizeFilePath(string $filePath): string |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$uri = $this->uriParser->parse($filePath); |
|
|
|
|
83
|
|
|
|
84
|
|
|
return $uri->getPath(); |
85
|
|
|
} catch (UriException $e) { |
86
|
|
|
// if filePath is not uri we suppose it is short form of filepath - without fs part |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $filePath; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|