|
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\Distribution\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use GuzzleHttp\Psr7\Uri; |
|
15
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
|
16
|
|
|
use Psr\Http\Message\UriInterface; |
|
17
|
|
|
|
|
18
|
|
|
class StaticResolver extends Resolver |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private const URI_PATH_DELIMITER = '/'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var UriInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $host; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param UriInterface $host |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(UriInterface $host) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->host = clone $host; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param UriFactoryInterface $factory |
|
40
|
|
|
* @param string $host |
|
41
|
|
|
* @return static |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function fromFactory(UriFactoryInterface $factory, string $host): self |
|
44
|
|
|
{ |
|
45
|
|
|
return new static($factory->createUri($host)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $host |
|
50
|
|
|
* @return static |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function create(string $host): self |
|
53
|
|
|
{ |
|
54
|
|
|
return new static(new Uri($host)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $file |
|
59
|
|
|
* @param array<string, string> $query |
|
60
|
|
|
* @return UriInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function resolve(string $file, array $query = []): UriInterface |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->host->withPath($this->suffix($file)) |
|
65
|
|
|
->withQuery(\http_build_query($query)) |
|
66
|
|
|
; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $file |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
private function suffix(string $file): string |
|
74
|
|
|
{ |
|
75
|
|
|
$prefix = $this->host->getPath(); |
|
76
|
|
|
|
|
77
|
|
|
return \implode(self::URI_PATH_DELIMITER, [ |
|
78
|
|
|
\trim($prefix, self::URI_PATH_DELIMITER), |
|
79
|
|
|
\trim($file, self::URI_PATH_DELIMITER), |
|
80
|
|
|
]); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|