Passed
Push — master ( d6782e...149d2c )
by Caen
12:13 queued 11s
created

Route::getSourcePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Concerns\HydePage;
6
use Hyde\Framework\Concerns\JsonSerializesArrayable;
7
use Hyde\Framework\Exceptions\RouteNotFoundException;
8
use Hyde\Framework\Foundation\RouteCollection;
9
use Hyde\Framework\Hyde;
10
use Illuminate\Contracts\Support\Arrayable;
11
12
/**
13
 * The Route class bridges the gaps between Hyde pages and their respective compiled static webpages
14
 * by providing helper methods and information allowing you to easily access and interact with the
15
 * various paths associated with a page, both source and compiled file paths as well as the URL.
16
 *
17
 * @see \Hyde\Framework\Testing\Feature\RouteTest
18
 */
19
class Route implements \Stringable, \JsonSerializable, Arrayable
20
{
21
    use JsonSerializesArrayable;
22
23
    /**
24
     * The source model for the route.
25
     *
26
     * @var \Hyde\Framework\Concerns\HydePage
27
     */
28
    protected HydePage $sourceModel;
29
30
    /**
31
     * The unique route key for the route.
32
     *
33
     * @var string The route key. Generally <output-directory/slug>.
34
     */
35
    protected string $routeKey;
36
37
    protected string $sourcePath;
38
    protected string $outputPath;
39
    protected string $uriPath;
40
41
    /**
42
     * Construct a new Route instance for the given page model.
43
     *
44
     * @param  \Hyde\Framework\Concerns\HydePage  $page
45
     */
46
    public function __construct(HydePage $page)
47
    {
48
        $this->sourceModel = $page;
49
        $this->routeKey = $page->getRouteKey();
50
        $this->sourcePath = $page->getSourcePath();
51
        $this->outputPath = $page->getOutputPath();
52
        $this->uriPath = $page->getLink();
53
    }
54
55
    /**
56
     * Cast a route object into a string that can be used in a href attribute.
57
     * Should be the same as getLink().
58
     */
59
    public function __toString(): string
60
    {
61
        return $this->getLink();
62
    }
63
64
    /**
65
     * Get the instance as an array.
66
     *
67
     * @return array<string, string>
68
     */
69
    public function toArray(): array
70
    {
71
        return [
0 ignored issues
show
introduced by
The expression return array('routeKey' ...is->sourceModel::class) returns an array which contains values of type string which are incompatible with the return type Illuminate\Contracts\Support\TValue mandated by Illuminate\Contracts\Support\Arrayable::toArray().
Loading history...
72
            'routeKey' => $this->routeKey,
73
            'sourcePath' => $this->sourcePath,
74
            'outputPath' => $this->outputPath,
75
            'sourceModel' => $this->sourceModel::class,
76
        ];
77
    }
78
79
    /**
80
     * Resolve a site web link to the file, using pretty URLs if enabled.
81
     *
82
     * @return string Relative URL path to the route site file.
83
     */
84
    public function getLink(): string
85
    {
86
        return Hyde::relativeLink($this->uriPath);
87
    }
88
89
    /**
90
     * Get the page type for the route.
91
     *
92
     * @return class-string<\Hyde\Framework\Concerns\HydePage>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Framework\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Framework\Concerns\HydePage>.
Loading history...
93
     */
94
    public function getPageType(): string
95
    {
96
        return $this->sourceModel::class;
97
    }
98
99
    /**
100
     * Get the source model for the route.
101
     *
102
     * @return \Hyde\Framework\Concerns\HydePage
103
     */
104
    public function getSourceModel(): HydePage
105
    {
106
        return $this->sourceModel;
107
    }
108
109
    /**
110
     * Get the unique route key for the route.
111
     *
112
     * @return string The route key. Generally <output-directory/slug>.
113
     */
114
    public function getRouteKey(): string
115
    {
116
        return $this->routeKey;
117
    }
118
119
    /**
120
     * Get the path to the source file.
121
     *
122
     * @return string Path relative to the root of the project.
123
     */
124
    public function getSourcePath(): string
125
    {
126
        return $this->sourcePath;
127
    }
128
129
    /**
130
     * Get the path to the output file.
131
     *
132
     * @return string Path relative to the site output directory.
133
     */
134
    public function getOutputPath(): string
135
    {
136
        return $this->outputPath;
137
    }
138
139
    /**
140
     * Get the qualified URL for the route, using pretty URLs if enabled.
141
     *
142
     * @return string Fully qualified URL using the configured base URL.
143
     */
144
    public function getQualifiedUrl(): string
145
    {
146
        return Hyde::url($this->outputPath);
147
    }
148
149
    /**
150
     * @param  \Hyde\Framework\Models\Route|string  $route  A route instance or route key string
151
     */
152
    public function is(Route|string $route): bool
153
    {
154
        if ($route instanceof Route) {
155
            return $this->getRouteKey() === $route->getRouteKey();
156
        }
157
158
        return $this->getRouteKey() === $route;
159
    }
160
161
    /**
162
     * Get a route from the route index for the specified route key.
163
     *
164
     * Alias for static::getFromKey().
165
     *
166
     * @param  string  $routeKey  Example: posts/foo.md
167
     * @return \Hyde\Framework\Models\Route
168
     *
169
     * @throws \Hyde\Framework\Exceptions\RouteNotFoundException
170
     */
171
    public static function get(string $routeKey): static
172
    {
173
        return static::getFromKey($routeKey);
174
    }
175
176
    /**
177
     * Get a route from the route index for the specified route key.
178
     *
179
     * @param  string  $routeKey  Example: posts/foo.md
180
     * @return \Hyde\Framework\Models\Route
181
     *
182
     * @throws \Hyde\Framework\Exceptions\RouteNotFoundException
183
     */
184
    public static function getFromKey(string $routeKey): static
185
    {
186
        return Hyde::routes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
187
    }
188
189
    /**
190
     * Get a route from the route index for the specified source file path.
191
     *
192
     * @param  string  $sourceFilePath  Example: _posts/foo.md
193
     * @return \Hyde\Framework\Models\Route
194
     *
195
     * @throws \Hyde\Framework\Exceptions\RouteNotFoundException
196
     */
197
    public static function getFromSource(string $sourceFilePath): static
198
    {
199
        return Hyde::routes()->first(function (Route $route) use ($sourceFilePath) {
200
            return $route->getSourcePath() === $sourceFilePath;
201
        }) ?? throw new RouteNotFoundException($sourceFilePath);
202
    }
203
204
    /**
205
     * Get a route from the route index for the supplied page model.
206
     *
207
     * @param  \Hyde\Framework\Concerns\HydePage  $page
208
     * @return \Hyde\Framework\Models\Route
209
     */
210
    public static function getFromModel(HydePage $page): Route
211
    {
212
        return $page->getRoute();
213
    }
214
215
    /**
216
     * Get all routes from the route index.
217
     *
218
     * @return \Hyde\Framework\Foundation\RouteCollection<\Hyde\Framework\Models\Route>
219
     */
220
    public static function all(): RouteCollection
221
    {
222
        return Hyde::routes();
223
    }
224
225
    /**
226
     * Get the current route for the page being rendered.
227
     */
228
    public static function current(): Route
229
    {
230
        return Hyde::currentRoute() ?? throw new RouteNotFoundException('current');
231
    }
232
233
    /**
234
     * Get the home route, usually the index page route.
235
     */
236
    public static function home(): Route
237
    {
238
        return static::getFromKey('index');
239
    }
240
241
    /**
242
     * Determine if the supplied route key exists in the route index.
243
     *
244
     * @param  string  $routeKey
245
     * @return bool
246
     */
247
    public static function exists(string $routeKey): bool
248
    {
249
        return Hyde::routes()->has($routeKey);
250
    }
251
}
252