1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Support\Models; |
6
|
|
|
|
7
|
|
|
use Stringable; |
8
|
|
|
use Hyde\Pages\DocumentationPage; |
9
|
|
|
use Hyde\Pages\MarkdownPost; |
10
|
|
|
use Hyde\Framework\Features\Navigation\NumericalPageOrderingHelper; |
11
|
|
|
use Hyde\Framework\Features\Blogging\BlogPostDatePrefixHelper; |
12
|
|
|
|
13
|
|
|
use function Hyde\unslash; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Route keys provide the core bindings of the HydePHP routing system as they are what canonically identifies a page. |
17
|
|
|
* This class both provides a data object for normalized type-hintable values, and general related helper methods. |
18
|
|
|
* |
19
|
|
|
* In short, the route key is the URL path relative to the site webroot, without the file extension. |
20
|
|
|
* |
21
|
|
|
* For example, `_pages/index.blade.php` would be compiled to `_site/index.html` and thus has the route key of `index`. |
22
|
|
|
* As another example, `_posts/welcome.md` would be compiled to `_site/posts/welcome.html` and thus has the route key of `posts/welcome`. |
23
|
|
|
* |
24
|
|
|
* Note that if the source page's output directory is changed, the route key will change accordingly. |
25
|
|
|
* This can potentially cause links to break when changing the output directory for a page class. |
26
|
|
|
*/ |
27
|
|
|
final class RouteKey implements Stringable |
28
|
|
|
{ |
29
|
|
|
protected readonly string $key; |
30
|
|
|
|
31
|
|
|
public static function make(string $key): self |
32
|
|
|
{ |
33
|
|
|
return new self($key); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __construct(string $key) |
37
|
|
|
{ |
38
|
|
|
$this->key = $key; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function __toString(): string |
42
|
|
|
{ |
43
|
|
|
return $this->key; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function get(): string |
47
|
|
|
{ |
48
|
|
|
return $this->key; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass */ |
|
|
|
|
52
|
|
|
public static function fromPage(string $pageClass, string $identifier): self |
53
|
|
|
{ |
54
|
|
|
$identifier = self::stripPrefixIfNeeded($pageClass, $identifier); |
55
|
|
|
|
56
|
|
|
return new self(unslash("{$pageClass::baseRouteKey()}/$identifier")); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @experimental |
61
|
|
|
* |
62
|
|
|
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass |
|
|
|
|
63
|
|
|
* */ |
64
|
|
|
protected static function stripPrefixIfNeeded(string $pageClass, string $identifier): string |
65
|
|
|
{ |
66
|
|
|
if (is_a($pageClass, DocumentationPage::class, true)) { |
67
|
|
|
if (NumericalPageOrderingHelper::hasNumericalPrefix($identifier)) { |
68
|
|
|
return NumericalPageOrderingHelper::splitNumericPrefix($identifier)[1]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (is_a($pageClass, MarkdownPost::class, true)) { |
73
|
|
|
if (BlogPostDatePrefixHelper::hasDatePrefix($identifier)) { |
74
|
|
|
return BlogPostDatePrefixHelper::stripDatePrefix($identifier); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $identifier; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|