Skip to content

Commit c8e4ffd

Browse files
authored
Merge pull request #1215 from hydephp/finalize-facades
Merge Route facade into Foundation Routes facade
2 parents 8a4bfa5 + e589221 commit c8e4ffd

File tree

23 files changed

+148
-150
lines changed

23 files changed

+148
-150
lines changed

_ide_helper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
class Hyde extends \Hyde\Hyde {}
2929
class Site extends \Hyde\Facades\Site {}
3030
class Meta extends \Hyde\Facades\Meta {}
31-
class Route extends \Hyde\Facades\Route {}
3231
/** @mixin \Hyde\Framework\Services\AssetService */
3332
class Asset extends \Hyde\Facades\Asset {}
3433
class Author extends \Hyde\Facades\Author {}
@@ -42,3 +41,5 @@ class DocumentationPage extends \Hyde\Pages\DocumentationPage {}
4241
/** @mixin \Illuminate\Filesystem\Filesystem */
4342
class Filesystem extends \Hyde\Facades\Filesystem {}
4443
class DataCollection extends \Hyde\Support\DataCollection {}
44+
/** @mixin \Hyde\Foundation\Kernel\RouteCollection */
45+
class Routes extends \Hyde\Foundation\Facades\Routes {}

app/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
'Hyde' => Hyde\Hyde::class,
9292
'Site' => \Hyde\Facades\Site::class,
9393
'Meta' => \Hyde\Facades\Meta::class,
94-
'Route' => \Hyde\Facades\Route::class,
9594
'Asset' => \Hyde\Facades\Asset::class,
9695
'Author' => \Hyde\Facades\Author::class,
9796
'Includes' => \Hyde\Facades\Includes::class,
@@ -103,6 +102,7 @@
103102
'DocumentationPage' => \Hyde\Pages\DocumentationPage::class,
104103
'Filesystem' => \Hyde\Facades\Filesystem::class,
105104
'DataCollection' => \Hyde\Support\DataCollection::class,
105+
'Routes' => \Hyde\Foundation\Facades\Routes::class,
106106
],
107107

108108
];

docs/getting-started/basic-architecture-concepts.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,23 @@ Don't worry if this sounds complex, as the key takeaway is that the index is cre
135135

136136
### Accessing routes
137137

138-
Each route in your site is represented by a Route object. It's very easy to get a Route object instance from the Router's index. There are a few ways to do this, but most commonly you'll use the Route facade's `get()` method where you provide a route key, and it will return the Route object. The route key is generally `<output-directory/slug>`. Here are some examples:
138+
Each route in your site is represented by a Route object. It's very easy to get a Route object instance from the Router's index. There are a few ways to do this, but most commonly you'll use the Routes facade's `get()` method where you provide a route key, and it will return the Route object. The route key is generally `<output-directory/slug>`. Here are some examples:
139139

140140
```php
141141
// Source file: _pages/index.md/index.blade.php
142142
// Compiled file: _site/index.html
143-
Route::get('index')
143+
Routes::get('index')
144144

145145
// Source file: _posts/my-post.md
146146
// Compiled file: _site/posts/my-post.html
147-
Route::get('posts/my-post')
147+
Routes::get('posts/my-post')
148148

149149
// Source file: _docs/readme.md
150150
// Compiled file: _site/docs/readme.html
151-
Route::get('docs/readme')
151+
Routes::get('docs/readme')
152152
```
153153

154-
>info Tip: You can also use "dot notation" instead of slashes, for example `Route::get('posts.my-post')`.
154+
>info Tip: You can also use "dot notation" instead of slashes, for example `Routes::get('posts.my-post')`.
155155
156156
### Using the `x-link` component
157157

@@ -165,12 +165,12 @@ You can of course, use it just like a normal anchor tag like so:
165165
But where it really shines is when you supply a route. This will then resolve the proper relative link, and format it to use pretty URLs if your site is configured to use them.
166166

167167
```blade
168-
<x-link :href="Route::get('index')">Home</x-link>
168+
<x-link :href="Routes::get('index')">Home</x-link>
169169
```
170170

171171
You can of course, also supply extra attributes like classes:
172172
```blade
173-
<x-link :href="Route::get('index')" class="btn btn-primary">Home</x-link>
173+
<x-link :href="Routes::get('index')" class="btn btn-primary">Home</x-link>
174174
```
175175

176176
## Nested directories
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<a href="{{ Route::get('index') }}" class="font-bold px-4" aria-label="Home page">
1+
<a href="{{ Routes::get('index') }}" class="font-bold px-4" aria-label="Home page">
22
{{ config('hyde.name', 'HydePHP') }}
33
</a>

packages/framework/src/Facades/Route.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

packages/framework/src/Foundation/Facades/Routes.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@
66

77
use Hyde\Foundation\HydeKernel;
88
use Hyde\Foundation\Kernel\RouteCollection;
9+
use Hyde\Hyde;
10+
use Hyde\Support\Models\Route;
11+
use Hyde\Support\Models\RouteKey;
912
use Illuminate\Support\Facades\Facade;
1013

1114
/**
15+
* Provides an easy way to access the Hyde pseudo-router.
16+
*
17+
* To access a route you need the route key which is the equivalent of the URL path of the compiled page.
18+
*
1219
* @mixin \Hyde\Foundation\Kernel\RouteCollection
1320
*/
1421
class Routes extends Facade
@@ -18,4 +25,32 @@ public static function getFacadeRoot(): RouteCollection
1825
{
1926
return HydeKernel::getInstance()->routes();
2027
}
28+
29+
public static function exists(string $routeKey): bool
30+
{
31+
return static::getFacadeRoot()->has(RouteKey::normalize($routeKey));
32+
}
33+
34+
public static function get(string $routeKey): ?Route
35+
{
36+
return static::getFacadeRoot()->get(RouteKey::normalize($routeKey));
37+
}
38+
39+
/** @throws \Hyde\Framework\Exceptions\RouteNotFoundException */
40+
public static function getOrFail(string $routeKey): Route
41+
{
42+
return static::getFacadeRoot()->getRoute(RouteKey::normalize($routeKey));
43+
}
44+
45+
/** @return \Hyde\Foundation\Kernel\RouteCollection<\Hyde\Support\Models\Route> */
46+
public static function all(): RouteCollection
47+
{
48+
return static::getFacadeRoot()->getRoutes();
49+
}
50+
51+
/** Get the current route for the page being rendered. */
52+
public static function current(): ?Route
53+
{
54+
return Hyde::currentRoute();
55+
}
2156
}

packages/framework/src/Framework/Features/Navigation/NavItem.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hyde\Framework\Features\Navigation;
66

7+
use Hyde\Foundation\Facades\Routes;
78
use Hyde\Hyde;
89
use Hyde\Support\Models\Route;
910
use Illuminate\Support\Str;
@@ -66,7 +67,7 @@ public static function forLink(string $href, string $label, int $priority = 500)
6667
*/
6768
public static function forRoute(Route|string $route, ?string $label = null, ?int $priority = null, ?string $group = null): static
6869
{
69-
return static::fromRoute($route instanceof Route ? $route : \Hyde\Facades\Route::getOrFail($route), $label, $priority, $group);
70+
return static::fromRoute($route instanceof Route ? $route : Routes::getOrFail($route), $label, $priority, $group);
7071
}
7172

7273
/**

packages/framework/src/Framework/Features/Paginator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Hyde\Framework\Features;
66

7+
use Hyde\Foundation\Facades\Routes;
78
use function collect;
89
use Hyde\Hyde;
910
use Hyde\Support\Models\Route;
@@ -81,7 +82,7 @@ public function getPageLinks(): array
8182
$pageRange = range(1, $this->totalPages());
8283
if (isset($this->routeBasename)) {
8384
foreach ($pageRange as $number) {
84-
$array[$number] = \Hyde\Facades\Route::get("$this->routeBasename/page-$number") ?? Hyde::formatLink("$this->routeBasename/page-$number");
85+
$array[$number] = Routes::get("$this->routeBasename/page-$number") ?? Hyde::formatLink("$this->routeBasename/page-$number");
8586
}
8687
} else {
8788
foreach ($pageRange as $number) {
@@ -200,7 +201,7 @@ protected function formatLink(int $offset): string
200201

201202
protected function getRoute(int $offset): Route|string
202203
{
203-
return \Hyde\Facades\Route::get("$this->routeBasename/{$this->formatPageName($offset)}") ?? Hyde::formatLink("$this->routeBasename/{$this->formatPageName($offset)}");
204+
return Routes::get("$this->routeBasename/{$this->formatPageName($offset)}") ?? Hyde::formatLink("$this->routeBasename/{$this->formatPageName($offset)}");
204205
}
205206

206207
protected function firstPage(): int

packages/framework/src/Framework/Features/XmlGenerators/SitemapGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Hyde\Framework\Features\XmlGenerators;
88

9+
use Hyde\Foundation\Facades\Routes;
910
use function config;
1011
use function date;
1112
use function filemtime;
@@ -29,7 +30,7 @@ class SitemapGenerator extends BaseXmlGenerator
2930

3031
public function generate(): static
3132
{
32-
\Hyde\Facades\Route::all()->each(function (Route $route): void {
33+
Routes::all()->each(function (Route $route): void {
3334
$this->addRoute($route);
3435
});
3536

packages/framework/src/Framework/Views/Components/BreadcrumbsComponent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Hyde\Framework\Views\Components;
66

7+
use Hyde\Foundation\Facades\Routes;
78
use Hyde\Hyde;
8-
use Hyde\Facades\Route;
99
use Illuminate\Contracts\View\Factory;
1010
use Illuminate\Contracts\View\View;
1111
use Illuminate\View\Component;
@@ -28,7 +28,7 @@ public function render(): Factory|View
2828
protected function makeBreadcrumbs(): array
2929
{
3030
$identifier = Hyde::currentRoute()->getPage()->getIdentifier();
31-
$breadcrumbs = [(Route::get('index')?->getLink() ?? '/') => 'Home'];
31+
$breadcrumbs = [(Routes::get('index')?->getLink() ?? '/') => 'Home'];
3232

3333
if ($identifier === 'index') {
3434
return $breadcrumbs;

0 commit comments

Comments
 (0)