Passed
Push — master ( 246ca5...f72f04 )
by Caen
03:27 queued 14s
created

Route::getSourcePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models\Support;
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
0 ignored issues
show
Bug introduced by
The type Hyde\Framework\Models\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::getFromKey($routeKey) returns the type Hyde\Framework\Models\Support\Route which is incompatible with the documented return type Hyde\Framework\Models\Route.
Loading history...
174
    }
175
176
    /**
177
     * Get a route from the route index for the specified route key.
178
     *
179
     * @param  string  $routeKey  Example: posts/foo, posts.foo
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(str_replace('.', '/', $routeKey))
187
            ?? throw new RouteNotFoundException($routeKey);
188
    }
189
190
    /**
191
     * Get a route from the route index for the specified source file path.
192
     *
193
     * @param  string  $sourceFilePath  Example: _posts/foo.md
194
     * @return \Hyde\Framework\Models\Route
195
     *
196
     * @throws \Hyde\Framework\Exceptions\RouteNotFoundException
197
     */
198
    public static function getFromSource(string $sourceFilePath): static
199
    {
200
        return Hyde::routes()->first(function (Route $route) use ($sourceFilePath) {
201
            return $route->getSourcePath() === $sourceFilePath;
202
        }) ?? throw new RouteNotFoundException($sourceFilePath);
203
    }
204
205
    /**
206
     * Get a route from the route index for the supplied page model.
207
     *
208
     * @param  \Hyde\Framework\Concerns\HydePage  $page
209
     * @return \Hyde\Framework\Models\Route
210
     */
211
    public static function getFromModel(HydePage $page): Route
212
    {
213
        return $page->getRoute();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $page->getRoute() returns the type Hyde\Framework\Models\Support\Route which is incompatible with the documented return type Hyde\Framework\Models\Route.
Loading history...
214
    }
215
216
    /**
217
     * Get all routes from the route index.
218
     *
219
     * @return \Hyde\Framework\Foundation\RouteCollection<\Hyde\Framework\Models\Route>
220
     */
221
    public static function all(): RouteCollection
222
    {
223
        return Hyde::routes();
224
    }
225
226
    /**
227
     * Get the current route for the page being rendered.
228
     */
229
    public static function current(): Route
230
    {
231
        return Hyde::currentRoute() ?? throw new RouteNotFoundException('current');
232
    }
233
234
    /**
235
     * Get the home route, usually the index page route.
236
     */
237
    public static function home(): Route
238
    {
239
        return static::getFromKey('index');
240
    }
241
242
    /**
243
     * Determine if the supplied route key exists in the route index.
244
     *
245
     * @param  string  $routeKey
246
     * @return bool
247
     */
248
    public static function exists(string $routeKey): bool
249
    {
250
        return Hyde::routes()->has($routeKey);
251
    }
252
}
253