1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Cocotte\Template\Traefik; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Cocotte\DigitalOcean\Hostname; |
7
|
|
|
use Cocotte\DigitalOcean\HostnameCollection; |
8
|
|
|
use Cocotte\Environment\FromEnvLazyFactory; |
9
|
|
|
use Cocotte\Environment\LazyEnvironmentValue; |
10
|
|
|
use Cocotte\Environment\LazyExportableOption; |
11
|
|
|
use Cocotte\Shell\Env; |
12
|
|
|
|
13
|
|
|
class TraefikHostname implements LazyExportableOption, FromEnvLazyFactory |
14
|
|
|
{ |
15
|
|
|
const TRAEFIK_UI_HOSTNAME = 'TRAEFIK_UI_HOSTNAME'; |
16
|
|
|
const OPTION_NAME = 'traefik-ui-hostname'; |
17
|
|
|
const REGEX = '/^[a-zA-Z0-9_@#%?&*+=!-]+$/'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Hostname |
21
|
|
|
*/ |
22
|
|
|
private $hostname; |
23
|
|
|
|
24
|
|
|
public function __construct(Hostname $hostname) |
25
|
|
|
{ |
26
|
|
|
Assertion::false($hostname->isRoot(), "$hostname does not have a subdomain."); |
27
|
|
|
$this->hostname = $hostname; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Env $env |
32
|
|
|
* @return LazyEnvironmentValue|self |
33
|
|
|
* @throws \Exception |
34
|
|
|
*/ |
35
|
|
|
public static function fromEnv(Env $env): LazyEnvironmentValue |
36
|
|
|
{ |
37
|
|
|
return new self(Hostname::parse($env->get(self::TRAEFIK_UI_HOSTNAME, ""))); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function toEnv(string $value, Env $env): void |
41
|
|
|
{ |
42
|
|
|
$env->put(self::TRAEFIK_UI_HOSTNAME, $value); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function optionName(): string |
46
|
|
|
{ |
47
|
|
|
return self::OPTION_NAME; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function __toString() |
51
|
|
|
{ |
52
|
|
|
return $this->toString(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function toString(): string |
56
|
|
|
{ |
57
|
|
|
return $this->hostname->toString(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function toHostnameCollection(): HostnameCollection |
61
|
|
|
{ |
62
|
|
|
return new HostnameCollection($this->hostname); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function toLocal(): Hostname |
66
|
|
|
{ |
67
|
|
|
return $this->hostname->toLocal(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function domainName() |
71
|
|
|
{ |
72
|
|
|
return $this->hostname->domainName(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function toHostname(): Hostname |
76
|
|
|
{ |
77
|
|
|
return $this->hostname; |
78
|
|
|
} |
79
|
|
|
} |