Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
AppUrl | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
8 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDomain | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPublicUrl | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
addPublicUrl | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\App\Models; |
6 | |
7 | use PeServer\Core\Code; |
8 | use PeServer\Core\Text; |
9 | use PeServer\Core\Web\Url; |
10 | use PeServer\Core\Web\UrlPath; |
11 | use PeServer\Core\Web\UrlQuery; |
12 | |
13 | /** |
14 | * アプリ設定のURLをなるべく事故らず使う処理。 |
15 | */ |
16 | class AppUrl |
17 | { |
18 | #region variable |
19 | |
20 | private Url|null $publicUrl = null; |
21 | |
22 | #endregion |
23 | |
24 | public function __construct( |
25 | private AppConfiguration $appConfiguration |
26 | ) { |
27 | } |
28 | |
29 | #region function |
30 | |
31 | /** |
32 | * ドメインを取得。 |
33 | * |
34 | * @return string |
35 | */ |
36 | public function getDomain(): string |
37 | { |
38 | return $this->appConfiguration->setting->config->address->domain; |
39 | } |
40 | |
41 | /** |
42 | * 公開URLを取得。 |
43 | * |
44 | * @return Url |
45 | */ |
46 | public function getPublicUrl(): Url |
47 | { |
48 | if ($this->publicUrl === null) { |
49 | $url = Text::replaceMap( |
50 | Code::toLiteralString($this->appConfiguration->setting->config->address->publicUrl), |
51 | [ |
52 | 'DOMAIN' => $this->getDomain() |
53 | ] |
54 | ); |
55 | |
56 | $this->publicUrl = Url::parse($url); |
57 | } |
58 | |
59 | return $this->publicUrl; |
60 | } |
61 | |
62 | /** |
63 | * 公開URLに対してパスの追加とクエリを設定する。 |
64 | * |
65 | * Url はパスを全置換えする挙動のため公開URLがパス付き(リバースプロキシ経由とか)の可能性があるのでそのあたりをいい感じにする感じ |
66 | * |
67 | * ※本来は IUrlHelper の役割だと思ってたけどそっちに手を入れる前にこっちが実装されてしまった感。 |
68 | * |
69 | * @param UrlPath $path |
70 | * @param UrlQuery|null $query |
71 | * @return Url |
72 | */ |
73 | public function addPublicUrl(UrlPath $path, ?UrlQuery $query = null): Url |
74 | { |
75 | $url = $this->getPublicUrl(); |
76 | |
77 | if (!$path->isEmpty()) { |
78 | $elements = $path->getElements(); |
79 | if (0 < count($elements)) { |
80 | $path = $url->path->add($elements); |
81 | $url = $url->changePath($path); |
82 | } |
83 | } |
84 | |
85 | if ($query !== null) { |
86 | $url = $url->changeQuery($query); |
87 | } |
88 | |
89 | return $url; |
90 | } |
91 | |
92 | |
93 | #endregion |
94 | } |