Skip to content

Commit

Permalink
Merge pull request #605 from hydephp/extract-routing-namespace
Browse files Browse the repository at this point in the history
Move routing related classes to new Routing namespace and refactor them
  • Loading branch information
caendesilva authored Oct 27, 2022
2 parents cee03cd + 4ebd0e8 commit 4d033c9
Show file tree
Hide file tree
Showing 32 changed files with 171 additions and 137 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ If you however have written custom code that explicitly references the old names
* Large changes to the Meta.php helper/facade; most of its business logic has been moved to the GlobalMetadataBag class.
* This might affect you if you relied on any of the helper methods that were removed from Meta.php.
* See https://github.com/hydephp/develop/pull/584 for more details.
- The Route helpers no longer throw exceptions when attempting to access a non-existent route.
* Instead, they return `null` if the route does not exist.
* This is to allow for more flexibility in the way routes are used.
* You can use Route::getOrFail() to get the same behavior as before, or null coalesce the result of Route::get().

### Other changes

Expand Down
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
'Hyde' => Hyde\Hyde::class,
'Asset' => \Hyde\Helpers\Asset::class,
'Site' => \Hyde\Support\Models\Site::class,
'Route' => \Hyde\Support\Models\Route::class,
'Route' => \Hyde\Routing\Route::class,
'BladePage' => \Hyde\Pages\BladePage::class,
'MarkdownPage' => \Hyde\Pages\MarkdownPage::class,
'MarkdownPost' => \Hyde\Pages\MarkdownPost::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle(): int
protected function getRoutes(): array
{
$routes = [];
/** @var \Hyde\Support\Models\Route $route */
/** @var \Hyde\Routing\Route $route */
foreach (Hyde::routes() as $route) {
$routes[] = [
$this->formatPageType($route->getPageType()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Hyde\Foundation\Concerns;

use Hyde\Framework\Concerns\HydePage;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;
use Illuminate\Support\Facades\View;

/**
Expand Down Expand Up @@ -40,7 +40,7 @@ public function currentPage(): ?string
/**
* Get the route for the page being rendered.
*
* @return \Hyde\Support\Models\Route|null
* @return \Hyde\Routing\Route|null
*/
public function currentRoute(): ?Route
{
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Foundation/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Hyde\Foundation\Concerns\BaseFoundationCollection;
use Hyde\Framework\Concerns\HydePage;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;

/**
* Pseudo-Router for Hyde.
Expand Down
6 changes: 3 additions & 3 deletions packages/framework/src/Framework/Concerns/HydePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
use Hyde\Hyde;
use Hyde\Markdown\Contracts\FrontMatter\PageSchema;
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Routing\Route;
use Hyde\Routing\RouteKey;
use Hyde\Support\Contracts\CompilableContract;
use Hyde\Support\Models\Route;
use Hyde\Support\Models\RouteKey;

/**
* The base class for all Hyde pages.
Expand Down Expand Up @@ -185,7 +185,7 @@ public function getRouteKey(): string
/**
* Get the route for the page.
*
* @return \Hyde\Support\Models\Route The page's route.
* @return \Hyde\Routing\Route The page's route.
*/
public function getRoute(): Route
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Hyde\Hyde;
use Hyde\Pages\DocumentationPage;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Hyde\Framework\Concerns\HydePage;
use Hyde\Hyde;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;
use Illuminate\Support\Str;
use Stringable;

Expand All @@ -30,7 +30,7 @@ class NavItem implements Stringable
/**
* Create a new navigation menu item.
*
* @param \Hyde\Support\Models\Route|null $route
* @param \Hyde\Routing\Route|null $route
* @param string $label
* @param int $priority
* @param bool $hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Hyde\Hyde;
use Hyde\Pages\DocumentationPage;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;
use Illuminate\Support\Collection;

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Framework/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Hyde;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;
use Hyde\Support\Models\Site;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;
Expand Down Expand Up @@ -96,7 +96,7 @@ protected function compilePagesForClass(string $pageClass): void
$this->newLine(2);
}

/** @psalm-return \Closure(\Hyde\Support\Models\Route):string */
/** @psalm-return \Closure(\Hyde\Routing\Route):string */
protected function compileRoute(): Closure
{
return function (Route $route) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Hyde\Pages\DocumentationPage;
use Hyde\Pages\MarkdownPage;
use Hyde\Pages\MarkdownPost;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;
use SimpleXMLElement;

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Hyde\Foundation\RouteCollection;
use Hyde\Framework\Concerns\HydePage;
use Hyde\Helpers\Features;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;
use Illuminate\Support\Facades\Facade;

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/src/Pages/DocumentationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Hyde\Framework\Actions\GeneratesSidebarTableOfContents;
use Hyde\Framework\Concerns\BaseMarkdownPage;
use Hyde\Markdown\Contracts\FrontMatter\DocumentationPageSchema;
use Hyde\Support\Models\Route;
use Hyde\Routing\Route;

/**
* Page class for documentation pages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Hyde\Helpers;
namespace Hyde\Routing;

use Hyde\Hyde;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

declare(strict_types=1);

namespace Hyde\Support\Models;
namespace Hyde\Routing;

use Hyde\Foundation\RouteCollection;
use Hyde\Framework\Concerns\HydePage;
use Hyde\Framework\Exceptions\RouteNotFoundException;
use Hyde\Hyde;
use Hyde\Support\Concerns\JsonSerializesArrayable;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -156,7 +154,7 @@ public function getQualifiedUrl(): string
/**
* Determine if the route instance matches another route or route key.
*
* @param \Hyde\Framework\Models\Support\Route|string $route A route instance or route key string
* @param \Hyde\Routing\Route|string $route A route instance or route key string
* @return bool
*/
public function is(Route|string $route): bool
Expand All @@ -169,94 +167,10 @@ public function is(Route|string $route): bool
}

/**
* Get a route from the route index for the specified route key.
*
* Alias for static::getFromKey().
*
* @param string $routeKey Example: posts/foo.md
* @return \Hyde\Framework\Models\Support\Route
*
* @throws \Hyde\Framework\Exceptions\RouteNotFoundException
*/
public static function get(string $routeKey): static
{
return static::getFromKey($routeKey);
}

/**
* Get a route from the route index for the specified route key.
*
* @param string $routeKey Example: posts/foo, posts.foo
* @return \Hyde\Framework\Models\Support\Route
*
* @throws \Hyde\Framework\Exceptions\RouteNotFoundException
*/
public static function getFromKey(string $routeKey): static
{
return Hyde::routes()->get(str_replace('.', '/', $routeKey))
?? throw new RouteNotFoundException($routeKey);
}

/**
* Get a route from the route index for the specified source file path.
*
* @param string $sourceFilePath Example: _posts/foo.md
* @return \Hyde\Framework\Models\Support\Route
*
* @throws \Hyde\Framework\Exceptions\RouteNotFoundException
*/
public static function getFromSource(string $sourceFilePath): static
{
return Hyde::routes()->first(function (Route $route) use ($sourceFilePath) {
return $route->getSourcePath() === $sourceFilePath;
}) ?? throw new RouteNotFoundException($sourceFilePath);
}

/**
* Get a route from the route index for the supplied page model.
*
* @param \Hyde\Framework\Concerns\HydePage $page
* @return \Hyde\Framework\Models\Support\Route
*/
public static function getFromModel(HydePage $page): Route
{
return $page->getRoute();
}

/**
* Get all routes from the route index.
*
* @return \Hyde\Foundation\RouteCollection<\Hyde\Framework\Models\Support\Route>
*/
public static function all(): RouteCollection
{
return Hyde::routes();
}

/**
* Get the current route for the page being rendered.
*/
public static function current(): Route
{
return Hyde::currentRoute() ?? throw new RouteNotFoundException('current');
}

/**
* Get the home route, usually the index page route.
*/
public static function home(): Route
{
return static::getFromKey('index');
}

/**
* Determine if the supplied route key exists in the route index.
*
* @param string $routeKey
* @return bool
* @inheritDoc
*/
public static function exists(string $routeKey): bool
public static function __callStatic(string $name, array $arguments)
{
return Hyde::routes()->has($routeKey);
return Router::$name(...$arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Hyde\Support\Models;
namespace Hyde\Routing;

use Stringable;
use function unslash;
Expand Down
Loading

0 comments on commit 4d033c9

Please sign in to comment.