1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Florian Krämer (https://florian-kraemer.net) |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net) |
10
|
|
|
* @author Florian Krämer |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types=1); |
16
|
|
|
|
17
|
|
|
namespace Phauthentic\Infrastructure\Storage\UrlBuilder; |
18
|
|
|
|
19
|
|
|
use Phauthentic\Infrastructure\Storage\FileInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Local URL Builder |
23
|
|
|
* |
24
|
|
|
* This URL builder assumes that your stored files are available using the |
25
|
|
|
* path under which they were stored and just prefixes their path with an URL |
26
|
|
|
* schema and host and a base path. |
27
|
|
|
* |
28
|
|
|
* This is a pretty basic URL builder based on a simple setup and assumption. |
29
|
|
|
* If you need a more complex URL building logic that involves your custom |
30
|
|
|
* router class or something like that, please implement your own URL builder |
31
|
|
|
* using the interface. |
32
|
|
|
*/ |
33
|
|
|
class LocalUrlBuilder implements UrlBuilderInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected string $basePath = '/'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $basePath Base Path |
42
|
|
|
*/ |
43
|
1 |
|
public function __construct(string $basePath) |
44
|
|
|
{ |
45
|
1 |
|
$this->basePath = $basePath; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
*/ |
51
|
1 |
|
public function url(FileInterface $file): string |
52
|
|
|
{ |
53
|
1 |
|
return str_replace('\\', '/', $this->buildBaseUrl() . $file->path()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
1 |
|
public function urlForVariant(FileInterface $file, string $variant): string |
60
|
|
|
{ |
61
|
1 |
|
if (!isset($file->variants()[$variant])) { |
62
|
|
|
return ''; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $this->buildBaseUrl() . $file->variants()[$variant]['url']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
1 |
|
protected function buildBaseUrl(): string |
72
|
|
|
{ |
73
|
1 |
|
return $this->basePath; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|