Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RebuildService to use the Router #182

Merged
merged 13 commits into from
Jul 5, 2022
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This serves two purposes:
- internal refactor: Creates a new build service to handle the build process

### Deprecated
- for soon-to-be removed features.
- Deprecated `DiscoveryService::findModelFromFilePath()` - Use the Router instead.

### Removed
- The "no pages found, skipping" message has been removed as the build loop no longer recieves empty collections.
Expand Down
14 changes: 14 additions & 0 deletions packages/framework/src/Modules/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ public static function getOrFail(string $routeKey): RouteContract
return static::get($routeKey) ?? throw new RouteNotFoundException($routeKey);
}

/** @inheritDoc */
public static function getFromSource(string $sourceFilePath): ?RouteContract
{
return Router::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
return $route->getSourceFilePath() === $sourceFilePath;
});
}

/** @inheritDoc */
public static function getFromSourceOrFail(string $sourceFilePath): RouteContract
{
return static::getFromSource($sourceFilePath) ?? throw new RouteNotFoundException($sourceFilePath);
}

protected function constructRouteKey(): string
{
return $this->sourceModel->getCurrentPagePath();
Expand Down
22 changes: 20 additions & 2 deletions packages/framework/src/Modules/Routing/RouteContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,36 @@ public function getOutputFilePath(): string;
/**
* Get a route from the Router index for the specified route key.
*
* @param string $routeKey
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
*/
public static function get(string $routeKey): ?RouteContract;

/**
* Same as static::get(), but throws an exception if the route key is not found.
*
* @param string $routeKey
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract
*
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
*/
public static function getOrFail(string $routeKey): RouteContract;

/**
* Get a route from the Router index for the specified source file path.
*
* @param string $sourceFilePath Example: _posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
*/
public static function getFromSource(string $sourceFilePath): ?RouteContract;

/**
* Same as static::getFromSource(), but throws an exception if the source file path is not found.
*
* @param string $sourceFilePath Example: _posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract
*
* @throws \Hyde\Framework\Modules\Routing\RouteNotFoundException
*/
public static function getFromSourceOrFail(string $sourceFilePath): RouteContract;
}
2 changes: 2 additions & 0 deletions packages/framework/src/Services/DiscoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static function getFilePathForModelClassFiles(string $model): string
/**
* Determine the Page Model to use for a given file path.
*
* @deprecated v0.47.0-beta - Use the Router instead.
*
* @param string $filepath
* @return string|false The model class constant, or false if none was found.
*
Expand Down
25 changes: 3 additions & 22 deletions packages/framework/src/Services/RebuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Services;

use Hyde\Framework\Modules\Routing\Route;
use Hyde\Framework\StaticPageBuilder;

/**
Expand All @@ -19,15 +20,6 @@ class RebuildService
*/
public string $filepath;

/**
* The model of the source file.
*
* @var string
*
* @internal
*/
public string $model;

/**
* The page builder instance.
* Used to get debug output from the builder.
Expand All @@ -39,12 +31,11 @@ class RebuildService
/**
* Construct the service class instance.
*
* @param string $filepath
* @param string $filepath Relative source file to compile. Example: _posts/foo.md
*/
public function __construct(string $filepath)
{
$this->filepath = $filepath;
$this->model = DiscoveryService::findModelFromFilePath($this->filepath);
}

/**
Expand All @@ -53,17 +44,7 @@ public function __construct(string $filepath)
public function execute(): StaticPageBuilder
{
return $this->builder = (new StaticPageBuilder(
DiscoveryService::getParserInstanceForModel(
$this->model,
basename(
str_replace(
DiscoveryService::getFilePathForModelClassFiles($this->model).'/',
'',
$this->filepath
),
DiscoveryService::getFileExtensionForModelFiles($this->model)
)
)->get(),
Route::getFromSource($this->filepath)->getSourceModel(),
true
));
}
Expand Down
27 changes: 9 additions & 18 deletions packages/framework/tests/Feature/RebuildServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,29 @@
use Hyde\Testing\TestCase;

/**
* Note that we don't actually test if the files were created,
* since the service is just a proxy for the actual builders,
* which have their own tests that include this feature.
* Note that we don't fully test the created files since the service is
* just a proxy for the actual builders, which have their own tests.
*
* @covers \Hyde\Framework\Services\RebuildService
*/
class RebuildServiceTest extends TestCase
{
public function test_service_method()
{
createTestPost();
$service = new RebuildService('_posts/test-post.md');
$service->execute();
$this->assertNotNull($service->model);
unlink(Hyde::path('_posts/test-post.md'));
unlink(Hyde::path('_site/posts/test-post.html'));
}

public function test_execute_methods()
{
$this->runExecuteTest('_posts');
$this->runExecuteTest('_pages');
$this->runExecuteTest('_docs');
$this->runExecuteTest('_pages', '.blade.php');

unlink(Hyde::path('_site/test-file.html'));
unlink(Hyde::path('_site/docs/test-file.html'));
unlink(Hyde::path('_site/posts/test-file.html'));
unlink(Hyde::path('_site/foo.html'));
unlink(Hyde::path('_site/docs/foo.html'));
unlink(Hyde::path('_site/posts/foo.html'));
}

protected function runExecuteTest(string $prefix, string $suffix = '.md')
{
$path = $prefix.'/test-file'.$suffix;
createTestPost($path);
$path = $prefix.'/foo'.$suffix;
touch(Hyde::path($path));
$service = new RebuildService($path);
$result = $service->execute();
$this->assertInstanceOf(StaticPageBuilder::class, $result);
Expand Down
64 changes: 64 additions & 0 deletions packages/framework/tests/Feature/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Modules\Routing\Route;
use Hyde\Framework\Modules\Routing\RouteContract;
use Hyde\Framework\Modules\Routing\RouteNotFoundException;
Expand Down Expand Up @@ -95,4 +98,65 @@ public function test_get_or_fail_does_not_return_null_if_route_is_not_found()
$this->expectException(RouteNotFoundException::class);
$this->assertNotNull(Route::getOrFail('not-found'));
}

public function test_get_from_source_returns_route_from_router_index()
{
$this->assertEquals(new Route(BladePage::parse('index')), Route::getFromSource('_pages/index.blade.php'));
$this->assertInstanceOf(RouteContract::class, Route::getFromSource('_pages/index.blade.php'));
}

public function test_get_from_source_returns_null_if_route_is_not_found()
{
$this->assertNull(Route::getFromSource('not-found'));
}

public function test_get_from_source_or_fail_returns_route_from_router_index()
{
$this->assertEquals(new Route(BladePage::parse('index')), Route::getFromSourceOrFail('_pages/index.blade.php'));
$this->assertInstanceOf(RouteContract::class, Route::getFromSourceOrFail('_pages/index.blade.php'));
}

/** @covers \Hyde\Framework\Modules\Routing\RouteNotFoundException */
public function test_get_from_source_or_fail_throws_exception_if_route_is_not_found()
{
$this->expectException(RouteNotFoundException::class);
$this->expectExceptionMessage("Route not found: 'not-found'");
$this->expectExceptionCode(404);

Route::getFromSourceOrFail('not-found');
}

public function test_get_from_source_or_fail_does_not_return_null_if_route_is_not_found()
{
$this->expectException(RouteNotFoundException::class);
$this->assertNotNull(Route::getFromSourceOrFail('not-found'));
}

public function test_get_from_source_can_find_blade_pages()
{
touch(Hyde::path('_pages/foo.blade.php'));
$this->assertEquals(new Route(BladePage::parse('foo')), Route::getFromSource('_pages/foo.blade.php'));
unlink(Hyde::path('_pages/foo.blade.php'));
}

public function test_get_from_source_can_find_markdown_pages()
{
touch(Hyde::path('_pages/foo.md'));
$this->assertEquals(new Route(MarkdownPage::parse('foo')), Route::getFromSource('_pages/foo.md'));
unlink(Hyde::path('_pages/foo.md'));
}

public function test_get_from_source_can_find_markdown_posts()
{
touch(Hyde::path('_posts/foo.md'));
$this->assertEquals(new Route(MarkdownPost::parse('foo')), Route::getFromSource('_posts/foo.md'));
unlink(Hyde::path('_posts/foo.md'));
}

public function test_get_from_source_can_find_documentation_pages()
{
touch(Hyde::path('_docs/foo.md'));
$this->assertEquals(new Route(DocumentationPage::parse('foo')), Route::getFromSource('_docs/foo.md'));
unlink(Hyde::path('_docs/foo.md'));
}
}
1 change: 1 addition & 0 deletions tests/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function deleteDirectory(string $directory)
}

if (! function_exists('createTestPost')) {
/** @deprecated - You usually don't need an actual post file anymore. Use touch() instead. */
function createTestPost(?string $path = null): string
{
$path = Hyde::path($path ?? '_posts/test-post.md');
Expand Down