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

Add static HydePage::get() method to get pages from the page collection #713

Merged
merged 6 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/framework/src/Framework/Actions/SourceFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class SourceFileParser
protected string $identifier;
protected HydePage $page;

/**
* @throws \Hyde\Framework\Exceptions\FileNotFoundException If the file does not exist.
*/
public function __construct(string $pageClass, string $identifier)
{
$this->validateExistence($pageClass, $identifier);
Expand Down
15 changes: 15 additions & 0 deletions packages/framework/src/Pages/Concerns/HydePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,26 @@ public function __construct(string $identifier = '', FrontMatter|array $matter =

// Section: Query

/**
* Get a page instance from the Kernel's page index by its identifier.
*
* @param string $identifier
* @return \Hyde\Pages\Concerns\HydePage
*
* @throws \Hyde\Framework\Exceptions\FileNotFoundException If the page does not exist.
*/
public static function get(string $identifier): HydePage
{
return Hyde::pages()->getPage(static::sourcePath($identifier));
}

/**
* Parse a source file into a page model instance.
*
* @param string $identifier The identifier of the page to parse.
* @return static New page model instance for the parsed source file.
*
* @throws \Hyde\Framework\Exceptions\FileNotFoundException If the file does not exist.
*/
public static function parse(string $identifier): HydePage
{
Expand Down
12 changes: 12 additions & 0 deletions packages/framework/tests/Feature/HydePageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Exceptions\FileNotFoundException;
use Hyde\Hyde;
use Hyde\Markdown\Models\Markdown;
use Hyde\Pages\BladePage;
Expand Down Expand Up @@ -208,6 +209,17 @@ public function test_get_identifier_returns_identifier_property()
$this->assertEquals('foo', $page->getIdentifier());
}

public function test_static_get_method_returns_discovered_page()
{
$this->assertEquals(BladePage::parse('index'), BladePage::get('index'));
}

public function test_static_get_method_throws_exception_if_page_not_found()
{
$this->expectException(FileNotFoundException::class);
BladePage::get('foo');
}

public function test_parse_parses_supplied_slug_into_a_page_model()
{
Hyde::touch(('_pages/foo.md'));
Expand Down