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

Merge from master #191

Merged
merged 18 commits into from
Jul 5, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ HydePHP consists of two primary components, Hyde/Hyde and Hyde/Framework. Develo

<!-- CHANGELOG_START -->

## [v0.47.0-beta](https://github.com/hydephp/develop/releases/tag/v0.47.0-beta) - 2022-07-05

### Added
- Add macroable trait to Hyde facade


## [v0.46.0-beta](https://github.com/hydephp/develop/releases/tag/v0.46.0-beta) - 2022-07-03

### Added
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {},
"name": "hyde",
"description": "Elegant and Powerful Static App Builder",
"version": "0.46.0",
"version": "0.47.0",
"main": "hyde",
"directories": {
"test": "tests"
Expand Down
8 changes: 8 additions & 0 deletions packages/framework/src/Contracts/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Hyde\Framework\Contracts;

use Hyde\Framework\Concerns\HasPageMetadata;
use Hyde\Framework\Modules\Routing\Route;
use Hyde\Framework\Modules\Routing\RouteContract;
use Hyde\Framework\Services\CollectionService;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -115,4 +117,10 @@ public function getCurrentPagePath(): string
{
return trim(static::getOutputDirectory().'/'.$this->slug, '/');
}

/** @inheritDoc */
public function getRoute(): RouteContract
{
return new Route($this);
}
}
8 changes: 8 additions & 0 deletions packages/framework/src/Contracts/PageContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Contracts;

use Hyde\Framework\Modules\Routing\RouteContract;
use Illuminate\Support\Collection;

interface PageContract
Expand Down Expand Up @@ -110,4 +111,11 @@ public function getOutputPath(): string;
* @return string URI path relative to the site root.
*/
public function getCurrentPagePath(): string;

/**
* Get the route for the page.
*
* @return \Hyde\Framework\Modules\Routing\RouteContract
*/
public function getRoute(): RouteContract;
}
44 changes: 11 additions & 33 deletions packages/framework/src/Facades/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,38 @@

namespace Hyde\Framework\Facades;

use Hyde\Framework\Contracts\PageContract;
use Hyde\Framework\Modules\Routing\Route as RouteModel;
use Hyde\Framework\Modules\Routing\RouteContract;
use Hyde\Framework\Modules\Routing\RouteFacadeContract;

/**
* @see \Hyde\Framework\Modules\Routing\Route
* @see \Hyde\Framework\Testing\Feature\RouteFacadeTest
*/
class Route
class Route implements RouteFacadeContract
{
/**
* Get a route from the Router index for the specified route key.
*
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
*/
/** @inheritDoc */
public static function get(string $routeKey): ?RouteContract
{
return RouteModel::get($routeKey);
}

/**
* Same as static::get(), but throws an exception if the route key is not found.
*
* @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
/** @inheritDoc */
public static function getFromKey(string $routeKey): ?RouteContract
{
return RouteModel::getOrFail($routeKey);
return RouteModel::getFromKey($routeKey);
}

/**
* 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
*/
/** @inheritDoc */
public static function getFromSource(string $sourceFilePath): ?RouteContract
{
return RouteModel::getFromSource($sourceFilePath);
}

/**
* 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
/** @inheritDoc */
public static function getFromModel(PageContract $page): ?RouteContract
{
return RouteModel::getFromSourceOrFail($sourceFilePath);
return RouteModel::getFromModel($page);
}
}
22 changes: 11 additions & 11 deletions packages/framework/src/Modules/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* @see \Hyde\Framework\Testing\Feature\RouteTest
*/
class Route implements RouteContract
class Route implements RouteContract, RouteFacadeContract
{
/**
* The source model for the route.
Expand Down Expand Up @@ -60,16 +60,21 @@ public function getOutputFilePath(): string
return $this->sourceModel->getOutputPath();
}

protected function constructRouteKey(): string
{
return $this->sourceModel->getCurrentPagePath();
}

/** @inheritDoc */
public static function get(string $routeKey): ?RouteContract
{
return Router::getInstance()->getRoutes()->get($routeKey);
return static::getFromKey($routeKey);
}

/** @inheritDoc */
public static function getOrFail(string $routeKey): RouteContract
public static function getFromKey(string $routeKey): ?RouteContract
{
return static::get($routeKey) ?? throw new RouteNotFoundException($routeKey);
return Router::getInstance()->getRoutes()->get($routeKey);
}

/** @inheritDoc */
Expand All @@ -81,13 +86,8 @@ public static function getFromSource(string $sourceFilePath): ?RouteContract
}

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

protected function constructRouteKey(): string
public static function getFromModel(PageContract $page): ?RouteContract
{
return $this->sourceModel->getCurrentPagePath();
return $page->getRoute();
}
}
36 changes: 0 additions & 36 deletions packages/framework/src/Modules/Routing/RouteContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,40 +47,4 @@ public function getSourceFilePath(): string;
* @return string Path relative to the site output directory.
*/
public function getOutputFilePath(): string;

/**
* Get a route from the Router index for the specified route key.
*
* @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 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;
}
42 changes: 42 additions & 0 deletions packages/framework/src/Modules/Routing/RouteFacadeContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Hyde\Framework\Modules\Routing;

use Hyde\Framework\Contracts\PageContract;

interface RouteFacadeContract
{
/**
* Get a route from the Router index for the specified route key.
*
* Alias for static::getFromKey().
*
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
*/
public static function get(string $routeKey): ?RouteContract;

/**
* Get a route from the Router index for the specified route key.
*
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
*/
public static function getFromKey(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;

/**
* Get a route from the Router index for the supplied page model.
*
* @param \Hyde\Framework\Contracts\PageContract $page
* @return \Hyde\Framework\Modules\Routing\RouteContract|null
*/
public static function getFromModel(PageContract $page): ?RouteContract;
}
7 changes: 7 additions & 0 deletions packages/framework/tests/Feature/AbstractPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Models\Parsers\MarkdownPageParser;
use Hyde\Framework\Models\Parsers\MarkdownPostParser;
use Hyde\Framework\Modules\Routing\Route;
use Hyde\Testing\TestCase;

/**
Expand Down Expand Up @@ -353,4 +354,10 @@ public function test_blade_pages_do_not_extend_abstract_markdown_page()
{
$this->assertNotInstanceOf(AbstractMarkdownPage::class, new BladePage('foo'));
}

public function test_get_route_returns_page_route()
{
$page = new MarkdownPage();
$this->assertEquals(new Route($page), $page->getRoute());
}
}
18 changes: 8 additions & 10 deletions packages/framework/tests/Feature/RouteFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,35 @@
namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Facades\Route;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Modules\Routing\Route as BaseRoute;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\Facades\Route
* @covers \Hyde\Framework\Modules\Routing\Route
*/
class RouteFacadeTest extends TestCase
{
/** @covers Route::get */
public function test_route_facade_get_method_calls_get_method()
{
$this->assertEquals(BaseRoute::get('index'), Route::get('index'));
}

/** @covers Route::getOrFail */
public function test_route_facade_getOrFail_method_calls_getOrFail_method()
public function test_route_facade_get_from_key_method_calls_get_from_key_method()
{
$this->assertEquals(BaseRoute::getOrFail('index'), Route::getOrFail('index'));
$this->assertEquals(BaseRoute::getFromKey('index'), Route::getFromKey('index'));
}

/** @covers Route::getFromSource */
public function test_route_facade_getFromSource_method_calls_getFromSource_method()
public function test_route_facade_get_from_source_method_calls_get_from_source_method()
{
$this->assertEquals(BaseRoute::getFromSource('_pages/index.blade.php'),
Route::getFromSource('_pages/index.blade.php'));
}

/** @covers Route::getFromSourceOrFail */
public function test_route_facade_getFromSourceOrFail_method_calls_getFromSourceOrFail_method()
public function test_route_facade_get_from_model_method_calls_get_from_model_method()
{
$this->assertEquals(BaseRoute::getFromSourceOrFail('_pages/index.blade.php'),
Route::getFromSourceOrFail('_pages/index.blade.php'));
$page = new BladePage('index');
$this->assertEquals(BaseRoute::getFromModel($page), Route::getFromModel($page));
}
}
Loading