1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tleckie\Assets; |
6
|
|
|
|
7
|
|
|
use Tleckie\Assets\Versioned\VersionedInterface; |
8
|
|
|
use function ltrim; |
9
|
|
|
use function rtrim; |
10
|
|
|
use function sprintf; |
11
|
|
|
use function str_contains; |
12
|
|
|
use function str_starts_with; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Bucket |
16
|
|
|
* |
17
|
|
|
* @package Tleckie\Assets |
18
|
|
|
* @category Bucket |
19
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Bucket implements BucketInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var VersionedInterface |
25
|
|
|
*/ |
26
|
|
|
protected VersionedInterface $version; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected string $path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Bucket constructor. |
35
|
|
|
* |
36
|
|
|
* @param VersionedInterface $version |
37
|
|
|
* @param string $path |
38
|
|
|
*/ |
39
|
|
|
public function __construct(VersionedInterface $version, string $path = '') |
40
|
|
|
{ |
41
|
|
|
$this->version = $version; |
42
|
|
|
$this->path = rtrim($path, '/'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function url(string $asset): string |
49
|
|
|
{ |
50
|
|
|
$versioned = ltrim($this->version->applyVersion($asset), '/'); |
51
|
|
|
|
52
|
|
|
if ($this->hasScheme($this->path())) { |
53
|
|
|
return sprintf("%s/%s", $this->path(), $versioned); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($this->isAbsolute($asset)) { |
57
|
|
|
return sprintf('/%s', $versioned); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (empty($this->path)) { |
61
|
|
|
return $versioned; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return sprintf("%s/%s", $this->path(), $versioned); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $path |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
private function hasScheme(string $path): bool |
72
|
|
|
{ |
73
|
|
|
return str_contains($path, '://') || str_starts_with($path, '//'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function path(): string |
80
|
|
|
{ |
81
|
|
|
return $this->path; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $asset |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
private function isAbsolute(string $asset) |
89
|
|
|
{ |
90
|
|
|
return str_starts_with($asset, '/'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
|
|
public function version(): string |
97
|
|
|
{ |
98
|
|
|
return $this->version->version(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|