1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Foundation\Facades; |
6
|
|
|
|
7
|
|
|
use Hyde\Foundation\HydeKernel; |
8
|
|
|
use Hyde\Foundation\Kernel\RouteCollection; |
9
|
|
|
use Hyde\Hyde; |
10
|
|
|
use Hyde\Support\Models\Route; |
11
|
|
|
use Illuminate\Support\Facades\Facade; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Provides an easy way to access the Hyde pseudo-router. |
15
|
|
|
* |
16
|
|
|
* To access a route you need the route key which is the equivalent of the URL path of the compiled page. |
17
|
|
|
* |
18
|
|
|
* @mixin \Hyde\Foundation\Kernel\RouteCollection |
19
|
|
|
*/ |
20
|
|
|
class Routes extends Facade |
21
|
|
|
{ |
22
|
|
|
public static function getFacadeRoot(): RouteCollection |
23
|
|
|
{ |
24
|
|
|
return HydeKernel::getInstance()->routes(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check if a route exists by its route key. |
29
|
|
|
*/ |
30
|
|
|
public static function exists(string $routeKey): bool |
31
|
|
|
{ |
32
|
|
|
return static::getFacadeRoot()->has($routeKey); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Try to get a route by its route key. If it doesn't exist, null is returned. |
37
|
|
|
*/ |
38
|
|
|
public static function find(string $routeKey): ?Route |
39
|
|
|
{ |
40
|
|
|
return static::getFacadeRoot()->get($routeKey); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get a route by its route key. If it doesn't exist, an exception is thrown. |
45
|
|
|
* |
46
|
|
|
* @throws \Hyde\Framework\Exceptions\RouteNotFoundException |
47
|
|
|
*/ |
48
|
|
|
public static function get(string $routeKey): Route |
49
|
|
|
{ |
50
|
|
|
return static::getFacadeRoot()->getRoute($routeKey); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all the routes for the site as a collection of route instances, keyed by route key. |
55
|
|
|
* |
56
|
|
|
* @return \Hyde\Foundation\Kernel\RouteCollection<string, \Hyde\Support\Models\Route> |
57
|
|
|
*/ |
58
|
|
|
public static function all(): RouteCollection |
59
|
|
|
{ |
60
|
|
|
return static::getFacadeRoot()->getRoutes(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the route instance for the page currently being rendered. |
65
|
|
|
* If a render is not in progress, this will return null. |
66
|
|
|
*/ |
67
|
|
|
public static function current(): ?Route |
68
|
|
|
{ |
69
|
|
|
return Hyde::currentRoute(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|