Skip to content

Commit 8d18f07

Browse files
author
github-actions
committed
Merge pull request #182 from hydephp/refactor-rebuild-service-to-use-the-router
Refactor RebuildService to use the Router hydephp/develop@bd543bd
1 parent c14bc89 commit 8d18f07

File tree

6 files changed

+112
-42
lines changed

6 files changed

+112
-42
lines changed

src/Modules/Routing/Route.php

+14
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ public static function getOrFail(string $routeKey): RouteContract
7272
return static::get($routeKey) ?? throw new RouteNotFoundException($routeKey);
7373
}
7474

75+
/** @inheritDoc */
76+
public static function getFromSource(string $sourceFilePath): ?RouteContract
77+
{
78+
return Router::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
79+
return $route->getSourceFilePath() === $sourceFilePath;
80+
});
81+
}
82+
83+
/** @inheritDoc */
84+
public static function getFromSourceOrFail(string $sourceFilePath): RouteContract
85+
{
86+
return static::getFromSource($sourceFilePath) ?? throw new RouteNotFoundException($sourceFilePath);
87+
}
88+
7589
protected function constructRouteKey(): string
7690
{
7791
return $this->sourceModel->getCurrentPagePath();

src/Modules/Routing/RouteContract.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,36 @@ public function getOutputFilePath(): string;
5151
/**
5252
* Get a route from the Router index for the specified route key.
5353
*
54-
* @param string $routeKey
54+
* @param string $routeKey Example: posts/foo.md
5555
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
5656
*/
5757
public static function get(string $routeKey): ?RouteContract;
5858

5959
/**
6060
* Same as static::get(), but throws an exception if the route key is not found.
6161
*
62-
* @param string $routeKey
62+
* @param string $routeKey Example: posts/foo.md
6363
* @return \Hyde\Framework\Modules\Routing\RouteContract
6464
*
6565
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
6666
*/
6767
public static function getOrFail(string $routeKey): RouteContract;
68+
69+
/**
70+
* Get a route from the Router index for the specified source file path.
71+
*
72+
* @param string $sourceFilePath Example: _posts/foo.md
73+
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
74+
*/
75+
public static function getFromSource(string $sourceFilePath): ?RouteContract;
76+
77+
/**
78+
* Same as static::getFromSource(), but throws an exception if the source file path is not found.
79+
*
80+
* @param string $sourceFilePath Example: _posts/foo.md
81+
* @return \Hyde\Framework\Modules\Routing\RouteContract
82+
*
83+
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
84+
*/
85+
public static function getFromSourceOrFail(string $sourceFilePath): RouteContract;
6886
}

src/Services/DiscoveryService.php

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public static function getFilePathForModelClassFiles(string $model): string
5858
/**
5959
* Determine the Page Model to use for a given file path.
6060
*
61+
* @deprecated v0.47.0-beta - Use the Router instead.
62+
*
6163
* @param string $filepath
6264
* @return string|false The model class constant, or false if none was found.
6365
*

src/Services/RebuildService.php

+3-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Hyde\Framework\Services;
44

5+
use Hyde\Framework\Modules\Routing\Route;
56
use Hyde\Framework\StaticPageBuilder;
67

78
/**
@@ -19,15 +20,6 @@ class RebuildService
1920
*/
2021
public string $filepath;
2122

22-
/**
23-
* The model of the source file.
24-
*
25-
* @var string
26-
*
27-
* @internal
28-
*/
29-
public string $model;
30-
3123
/**
3224
* The page builder instance.
3325
* Used to get debug output from the builder.
@@ -39,12 +31,11 @@ class RebuildService
3931
/**
4032
* Construct the service class instance.
4133
*
42-
* @param string $filepath
34+
* @param string $filepath Relative source file to compile. Example: _posts/foo.md
4335
*/
4436
public function __construct(string $filepath)
4537
{
4638
$this->filepath = $filepath;
47-
$this->model = DiscoveryService::findModelFromFilePath($this->filepath);
4839
}
4940

5041
/**
@@ -53,17 +44,7 @@ public function __construct(string $filepath)
5344
public function execute(): StaticPageBuilder
5445
{
5546
return $this->builder = (new StaticPageBuilder(
56-
DiscoveryService::getParserInstanceForModel(
57-
$this->model,
58-
basename(
59-
str_replace(
60-
DiscoveryService::getFilePathForModelClassFiles($this->model).'/',
61-
'',
62-
$this->filepath
63-
),
64-
DiscoveryService::getFileExtensionForModelFiles($this->model)
65-
)
66-
)->get(),
47+
Route::getFromSource($this->filepath)->getSourceModel(),
6748
true
6849
));
6950
}

tests/Feature/RebuildServiceTest.php

+9-18
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,29 @@
88
use Hyde\Testing\TestCase;
99

1010
/**
11-
* Note that we don't actually test if the files were created,
12-
* since the service is just a proxy for the actual builders,
13-
* which have their own tests that include this feature.
11+
* Note that we don't fully test the created files since the service is
12+
* just a proxy for the actual builders, which have their own tests.
13+
*
14+
* @covers \Hyde\Framework\Services\RebuildService
1415
*/
1516
class RebuildServiceTest extends TestCase
1617
{
17-
public function test_service_method()
18-
{
19-
createTestPost();
20-
$service = new RebuildService('_posts/test-post.md');
21-
$service->execute();
22-
$this->assertNotNull($service->model);
23-
unlink(Hyde::path('_posts/test-post.md'));
24-
unlink(Hyde::path('_site/posts/test-post.html'));
25-
}
26-
2718
public function test_execute_methods()
2819
{
2920
$this->runExecuteTest('_posts');
3021
$this->runExecuteTest('_pages');
3122
$this->runExecuteTest('_docs');
3223
$this->runExecuteTest('_pages', '.blade.php');
3324

34-
unlink(Hyde::path('_site/test-file.html'));
35-
unlink(Hyde::path('_site/docs/test-file.html'));
36-
unlink(Hyde::path('_site/posts/test-file.html'));
25+
unlink(Hyde::path('_site/foo.html'));
26+
unlink(Hyde::path('_site/docs/foo.html'));
27+
unlink(Hyde::path('_site/posts/foo.html'));
3728
}
3829

3930
protected function runExecuteTest(string $prefix, string $suffix = '.md')
4031
{
41-
$path = $prefix.'/test-file'.$suffix;
42-
createTestPost($path);
32+
$path = $prefix.'/foo'.$suffix;
33+
touch(Hyde::path($path));
4334
$service = new RebuildService($path);
4435
$result = $service->execute();
4536
$this->assertInstanceOf(StaticPageBuilder::class, $result);

tests/Feature/RouteTest.php

+64
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
namespace Hyde\Framework\Testing\Feature;
44

5+
use Hyde\Framework\Hyde;
56
use Hyde\Framework\Models\Pages\BladePage;
7+
use Hyde\Framework\Models\Pages\DocumentationPage;
68
use Hyde\Framework\Models\Pages\MarkdownPage;
9+
use Hyde\Framework\Models\Pages\MarkdownPost;
710
use Hyde\Framework\Modules\Routing\Route;
811
use Hyde\Framework\Modules\Routing\RouteContract;
912
use Hyde\Framework\Modules\Routing\RouteNotFoundException;
@@ -95,4 +98,65 @@ public function test_get_or_fail_does_not_return_null_if_route_is_not_found()
9598
$this->expectException(RouteNotFoundException::class);
9699
$this->assertNotNull(Route::getOrFail('not-found'));
97100
}
101+
102+
public function test_get_from_source_returns_route_from_router_index()
103+
{
104+
$this->assertEquals(new Route(BladePage::parse('index')), Route::getFromSource('_pages/index.blade.php'));
105+
$this->assertInstanceOf(RouteContract::class, Route::getFromSource('_pages/index.blade.php'));
106+
}
107+
108+
public function test_get_from_source_returns_null_if_route_is_not_found()
109+
{
110+
$this->assertNull(Route::getFromSource('not-found'));
111+
}
112+
113+
public function test_get_from_source_or_fail_returns_route_from_router_index()
114+
{
115+
$this->assertEquals(new Route(BladePage::parse('index')), Route::getFromSourceOrFail('_pages/index.blade.php'));
116+
$this->assertInstanceOf(RouteContract::class, Route::getFromSourceOrFail('_pages/index.blade.php'));
117+
}
118+
119+
/** @covers \Hyde\Framework\Modules\Routing\RouteNotFoundException */
120+
public function test_get_from_source_or_fail_throws_exception_if_route_is_not_found()
121+
{
122+
$this->expectException(RouteNotFoundException::class);
123+
$this->expectExceptionMessage("Route not found: 'not-found'");
124+
$this->expectExceptionCode(404);
125+
126+
Route::getFromSourceOrFail('not-found');
127+
}
128+
129+
public function test_get_from_source_or_fail_does_not_return_null_if_route_is_not_found()
130+
{
131+
$this->expectException(RouteNotFoundException::class);
132+
$this->assertNotNull(Route::getFromSourceOrFail('not-found'));
133+
}
134+
135+
public function test_get_from_source_can_find_blade_pages()
136+
{
137+
touch(Hyde::path('_pages/foo.blade.php'));
138+
$this->assertEquals(new Route(BladePage::parse('foo')), Route::getFromSource('_pages/foo.blade.php'));
139+
unlink(Hyde::path('_pages/foo.blade.php'));
140+
}
141+
142+
public function test_get_from_source_can_find_markdown_pages()
143+
{
144+
touch(Hyde::path('_pages/foo.md'));
145+
$this->assertEquals(new Route(MarkdownPage::parse('foo')), Route::getFromSource('_pages/foo.md'));
146+
unlink(Hyde::path('_pages/foo.md'));
147+
}
148+
149+
public function test_get_from_source_can_find_markdown_posts()
150+
{
151+
touch(Hyde::path('_posts/foo.md'));
152+
$this->assertEquals(new Route(MarkdownPost::parse('foo')), Route::getFromSource('_posts/foo.md'));
153+
unlink(Hyde::path('_posts/foo.md'));
154+
}
155+
156+
public function test_get_from_source_can_find_documentation_pages()
157+
{
158+
touch(Hyde::path('_docs/foo.md'));
159+
$this->assertEquals(new Route(DocumentationPage::parse('foo')), Route::getFromSource('_docs/foo.md'));
160+
unlink(Hyde::path('_docs/foo.md'));
161+
}
98162
}

0 commit comments

Comments
 (0)