1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Models; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Concerns\JsonSerializesArrayable; |
6
|
|
|
use Hyde\Framework\Contracts\PageContract; |
7
|
|
|
use Hyde\Framework\Contracts\RouteContract; |
8
|
|
|
use Hyde\Framework\Contracts\RouteFacadeContract; |
9
|
|
|
use Hyde\Framework\Exceptions\RouteNotFoundException; |
10
|
|
|
use Hyde\Framework\Hyde; |
11
|
|
|
use Hyde\Framework\Services\RoutingService; |
12
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
13
|
|
|
use Illuminate\Support\Collection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @see \Hyde\Framework\Testing\Feature\RouteTest |
17
|
|
|
*/ |
18
|
|
|
class Route implements RouteContract, RouteFacadeContract, \Stringable, \JsonSerializable, Arrayable |
19
|
|
|
{ |
20
|
|
|
use JsonSerializesArrayable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The source model for the route. |
24
|
|
|
* |
25
|
|
|
* @var \Hyde\Framework\Contracts\PageContract |
26
|
|
|
*/ |
27
|
|
|
protected PageContract $sourceModel; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The unique route key for the route. |
31
|
|
|
* |
32
|
|
|
* @var string The route key. Generally <output-directory/slug>. |
33
|
|
|
*/ |
34
|
|
|
protected string $routeKey; |
35
|
|
|
|
36
|
|
|
/** @inheritDoc */ |
37
|
|
|
public function __construct(PageContract $sourceModel) |
38
|
|
|
{ |
39
|
|
|
$this->sourceModel = $sourceModel; |
40
|
|
|
$this->routeKey = $this->constructRouteKey(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @inheritDoc */ |
44
|
|
|
public function toArray() |
45
|
|
|
{ |
46
|
|
|
return [ |
|
|
|
|
47
|
|
|
'routeKey' => $this->routeKey, |
48
|
|
|
'sourceModelPath' => $this->sourceModel->getSourcePath(), |
49
|
|
|
'sourceModelType' => $this->sourceModel::class, |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** @inheritDoc */ |
54
|
|
|
public function getPageType(): string |
55
|
|
|
{ |
56
|
|
|
return $this->sourceModel::class; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @inheritDoc */ |
60
|
|
|
public function getSourceModel(): PageContract |
61
|
|
|
{ |
62
|
|
|
return $this->sourceModel; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @inheritDoc */ |
66
|
|
|
public function getRouteKey(): string |
67
|
|
|
{ |
68
|
|
|
return $this->routeKey; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @inheritDoc */ |
72
|
|
|
public function getSourceFilePath(): string |
73
|
|
|
{ |
74
|
|
|
return $this->sourceModel->getSourcePath(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @inheritDoc */ |
78
|
|
|
public function getOutputFilePath(): string |
79
|
|
|
{ |
80
|
|
|
return $this->sourceModel->getOutputPath(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @inheritDoc */ |
84
|
|
|
public function getQualifiedUrl(): string |
85
|
|
|
{ |
86
|
|
|
return Hyde::url($this->getOutputFilePath()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @inheritDoc */ |
90
|
|
|
public function getLink(): string |
91
|
|
|
{ |
92
|
|
|
return Hyde::relativeLink($this->getOutputFilePath()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @inheritDoc */ |
96
|
|
|
public function __toString(): string |
97
|
|
|
{ |
98
|
|
|
return $this->getLink(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** @deprecated Use the route key property */ |
102
|
|
|
protected function constructRouteKey(): string |
103
|
|
|
{ |
104
|
|
|
return $this->sourceModel->getCurrentPagePath(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** @inheritDoc */ |
108
|
|
|
public static function get(string $routeKey): static |
109
|
|
|
{ |
110
|
|
|
return static::getFromKey($routeKey); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** @inheritDoc */ |
114
|
|
|
public static function getFromKey(string $routeKey): static |
115
|
|
|
{ |
116
|
|
|
return RoutingService::getInstance()->getRoutes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** @inheritDoc */ |
120
|
|
|
public static function getFromSource(string $sourceFilePath): static |
121
|
|
|
{ |
122
|
|
|
return RoutingService::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) { |
|
|
|
|
123
|
|
|
return $route->getSourceFilePath() === $sourceFilePath; |
124
|
|
|
}) ?? throw new RouteNotFoundException($sourceFilePath); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** @inheritDoc */ |
128
|
|
|
public static function getFromModel(PageContract $page): RouteContract |
129
|
|
|
{ |
130
|
|
|
return $page->getRoute(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @inheritDoc */ |
134
|
|
|
public static function all(): Collection |
135
|
|
|
{ |
136
|
|
|
return RoutingService::getInstance()->getRoutes(); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** @inheritDoc */ |
140
|
|
|
public static function current(): RouteContract |
141
|
|
|
{ |
142
|
|
|
return Hyde::currentRoute() ?? throw new RouteNotFoundException('current'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** @inheritDoc */ |
146
|
|
|
public static function home(): RouteContract |
147
|
|
|
{ |
148
|
|
|
return static::getFromKey('index'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** @todo add to contract */ |
152
|
|
|
public static function exists(string $routeKey): bool |
153
|
|
|
{ |
154
|
|
|
return RoutingService::getInstance()->getRoutes()->has($routeKey); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.