Skip to content

Commit a4f1046

Browse files
authored
Merge pull request #200 from hydephp/throw-exception-if-route-is-not-found
Throw exception if route is not found
2 parents e319bd5 + 13a909a commit a4f1046

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

packages/framework/src/Facades/Route.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
class Route implements RouteFacadeContract
1515
{
1616
/** @inheritDoc */
17-
public static function get(string $routeKey): ?RouteModel
17+
public static function get(string $routeKey): RouteModel
1818
{
1919
return RouteModel::get($routeKey);
2020
}
2121

2222
/** @inheritDoc */
23-
public static function getFromKey(string $routeKey): ?RouteModel
23+
public static function getFromKey(string $routeKey): RouteModel
2424
{
2525
return RouteModel::getFromKey($routeKey);
2626
}
2727

2828
/** @inheritDoc */
29-
public static function getFromSource(string $sourceFilePath): ?RouteModel
29+
public static function getFromSource(string $sourceFilePath): RouteModel
3030
{
3131
return RouteModel::getFromSource($sourceFilePath);
3232
}
3333

3434
/** @inheritDoc */
35-
public static function getFromModel(PageContract $page): ?RouteModel
35+
public static function getFromModel(PageContract $page): RouteModel
3636
{
3737
return RouteModel::getFromModel($page);
3838
}

packages/framework/src/Modules/Routing/Route.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -74,23 +74,23 @@ protected function constructRouteKey(): string
7474
}
7575

7676
/** @inheritDoc */
77-
public static function get(string $routeKey): ?static
77+
public static function get(string $routeKey): static
7878
{
7979
return static::getFromKey($routeKey);
8080
}
8181

8282
/** @inheritDoc */
83-
public static function getFromKey(string $routeKey): ?static
83+
public static function getFromKey(string $routeKey): static
8484
{
85-
return Router::getInstance()->getRoutes()->get($routeKey);
85+
return Router::getInstance()->getRoutes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
8686
}
8787

8888
/** @inheritDoc */
89-
public static function getFromSource(string $sourceFilePath): ?static
89+
public static function getFromSource(string $sourceFilePath): static
9090
{
9191
return Router::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
9292
return $route->getSourceFilePath() === $sourceFilePath;
93-
});
93+
}) ?? throw new RouteNotFoundException($sourceFilePath);
9494
}
9595

9696
/** @inheritDoc */

packages/framework/src/Modules/Routing/RouteContract.php

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
use Hyde\Framework\Contracts\PageContract;
66

7+
/**
8+
* This contract defines the methods that a Route object must implement.
9+
* These methods are each applied to the single route instance.
10+
*
11+
* In Hyde, the route models also serve as a facade for all routes, see the dedicated interface:
12+
*
13+
* @see \Hyde\Framework\Modules\Routing\RouteFacadeContract for the static facade methods.
14+
*/
715
interface RouteContract
816
{
917
/**

packages/framework/src/Modules/Routing/RouteFacadeContract.php

+21-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
use Hyde\Framework\Contracts\PageContract;
66
use Illuminate\Support\Collection;
77

8+
/**
9+
* This contract defines the static facade methods for the Route class.
10+
*
11+
* @see \Hyde\Framework\Modules\Routing\RouteContract for the interface that each route model must implement.
12+
*/
813
interface RouteFacadeContract
914
{
1015
/**
@@ -13,33 +18,41 @@ interface RouteFacadeContract
1318
* Alias for static::getFromKey().
1419
*
1520
* @param string $routeKey Example: posts/foo.md
16-
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
21+
* @return \Hyde\Framework\Modules\Routing\RouteContract
22+
*
23+
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
1724
*/
18-
public static function get(string $routeKey): ?RouteContract;
25+
public static function get(string $routeKey): RouteContract;
1926

2027
/**
2128
* Get a route from the Router index for the specified route key.
2229
*
2330
* @param string $routeKey Example: posts/foo.md
24-
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
31+
* @return \Hyde\Framework\Modules\Routing\RouteContract
32+
*
33+
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
2534
*/
26-
public static function getFromKey(string $routeKey): ?RouteContract;
35+
public static function getFromKey(string $routeKey): RouteContract;
2736

2837
/**
2938
* Get a route from the Router index for the specified source file path.
3039
*
3140
* @param string $sourceFilePath Example: _posts/foo.md
32-
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
41+
* @return \Hyde\Framework\Modules\Routing\RouteContract
42+
*
43+
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
3344
*/
34-
public static function getFromSource(string $sourceFilePath): ?RouteContract;
45+
public static function getFromSource(string $sourceFilePath): RouteContract;
3546

3647
/**
3748
* Get a route from the Router index for the supplied page model.
3849
*
3950
* @param \Hyde\Framework\Contracts\PageContract $page
40-
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
51+
* @return \Hyde\Framework\Modules\Routing\RouteContract
52+
*
53+
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
4154
*/
42-
public static function getFromModel(PageContract $page): ?RouteContract;
55+
public static function getFromModel(PageContract $page): RouteContract;
4356

4457
/**
4558
* Get all routes from the Router index.

packages/framework/tests/Feature/DarkmodeFeatureTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function test_layout_has_toggle_button_and_script_when_enabled()
3838
{
3939
Config::set('hyde.features', [
4040
Features::markdownPages(),
41+
Features::bladePages(),
4142
Features::darkmode(),
4243
]);
4344

@@ -74,6 +75,7 @@ public function test_dark_mode_theme_button_is_hidden_in_layouts_when_disabled()
7475
{
7576
Config::set('hyde.features', [
7677
Features::markdownPages(),
78+
Features::bladePages(),
7779
]);
7880

7981
$view = view('hyde::layouts/page')->with([

packages/framework/tests/Feature/RouteTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Hyde\Framework\Models\Pages\MarkdownPost;
1010
use Hyde\Framework\Modules\Routing\Route;
1111
use Hyde\Framework\Modules\Routing\RouteContract;
12+
use Hyde\Framework\Modules\Routing\RouteNotFoundException;
1213
use Hyde\Framework\Modules\Routing\Router;
1314
use Hyde\Testing\TestCase;
1415

@@ -77,9 +78,10 @@ public function test_get_from_key_returns_route_from_router_index()
7778
$this->assertInstanceOf(RouteContract::class, Route::get('index'));
7879
}
7980

80-
public function test_get_from_key_returns_null_if_route_is_not_found()
81+
public function test_get_from_key_throws_exception_if_route_is_not_found()
8182
{
82-
$this->assertNull(Route::get('not-found'));
83+
$this->expectException(RouteNotFoundException::class);
84+
Route::get('not-found');
8385
}
8486

8587
public function test_get_from_source_returns_route_from_router_index()
@@ -90,7 +92,8 @@ public function test_get_from_source_returns_route_from_router_index()
9092

9193
public function test_get_from_source_returns_null_if_route_is_not_found()
9294
{
93-
$this->assertNull(Route::getFromSource('not-found'));
95+
$this->expectException(RouteNotFoundException::class);
96+
Route::getFromSource('not-found');
9497
}
9598

9699
public function test_get_from_source_can_find_blade_pages()

0 commit comments

Comments
 (0)