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

Rename Router class to Services\RoutingService #234

Merged
merged 3 commits into from
Jul 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Support\Collection;

interface RouterContract
interface RoutingServiceContract
{
/**
* Construct a new Router instance and discover all routes.
Expand All @@ -14,9 +14,9 @@ public function __construct();
/**
* Get the Singleton instance of the Router.
*
* @return \Hyde\Framework\Modules\Routing\RouterContract
* @return \Hyde\Framework\Contracts\RoutingServiceContract
*/
public static function getInstance(): RouterContract;
public static function getInstance(): RoutingServiceContract;

/**
* Get the routes discovered by the router.
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Models/NavigationMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Hyde\Framework\Contracts\RouteContract;
use Hyde\Framework\Hyde;
use Hyde\Framework\Router;
use Hyde\Framework\Services\RoutingService;
use Illuminate\Support\Collection;

/**
Expand Down Expand Up @@ -38,7 +38,7 @@ public function setCurrentRoute(RouteContract $currentRoute): self

public function generate(): self
{
Router::getInstance()->getRoutes()->each(function (Route $route) {
RoutingService::getInstance()->getRoutes()->each(function (Route $route) {
$this->items->push(NavItem::fromRoute($route));
});

Expand Down
8 changes: 4 additions & 4 deletions packages/framework/src/Models/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Hyde\Framework\Contracts\RouteFacadeContract;
use Hyde\Framework\Exceptions\RouteNotFoundException;
use Hyde\Framework\Hyde;
use Hyde\Framework\Router;
use Hyde\Framework\Services\RoutingService;
use Illuminate\Support\Collection;

/**
Expand Down Expand Up @@ -92,13 +92,13 @@ public static function get(string $routeKey): static
/** @inheritDoc */
public static function getFromKey(string $routeKey): static
{
return Router::getInstance()->getRoutes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
return RoutingService::getInstance()->getRoutes()->get($routeKey) ?? throw new RouteNotFoundException($routeKey);
}

/** @inheritDoc */
public static function getFromSource(string $sourceFilePath): static
{
return Router::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
return RoutingService::getInstance()->getRoutes()->first(function (RouteContract $route) use ($sourceFilePath) {
return $route->getSourceFilePath() === $sourceFilePath;
}) ?? throw new RouteNotFoundException($sourceFilePath);
}
Expand All @@ -112,7 +112,7 @@ public static function getFromModel(PageContract $page): RouteContract
/** @inheritDoc */
public static function all(): Collection
{
return Router::getInstance()->getRoutes();
return RoutingService::getInstance()->getRoutes();
}

/** @inheritDoc */
Expand Down
5 changes: 2 additions & 3 deletions packages/framework/src/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Framework\Contracts\RouteContract as Route;
use Hyde\Framework\Hyde;
use Hyde\Framework\Router;
use Hyde\Framework\StaticPageBuilder;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;
Expand All @@ -24,13 +23,13 @@ class BuildService
use InteractsWithIO;
use InteractsWithDirectories;

protected Router $router;
protected RoutingService $router;

public function __construct(OutputStyle $output)
{
$this->output = $output;

$this->router = Router::getInstance();
$this->router = RoutingService::getInstance();
}

public function compileStaticPages(): void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Hyde\Framework;
namespace Hyde\Framework\Services;

use Hyde\Framework\Contracts\PageContract;
use Hyde\Framework\Contracts\RouteContract;
use Hyde\Framework\Contracts\RouterContract;
use Hyde\Framework\Contracts\RoutingServiceContract;
use Hyde\Framework\Helpers\Features;
use Hyde\Framework\Models\Pages\BladePage;
use Hyde\Framework\Models\Pages\DocumentationPage;
Expand Down Expand Up @@ -32,9 +32,9 @@
* determine where a source file will be compiled to, and where a compiled
* file was generated from.
*
* @see \Hyde\Framework\Testing\Feature\RouterTest
* @see \Hyde\Framework\Testing\Feature\RoutingServiceTest
*/
class Router implements RouterContract
class RoutingService implements RoutingServiceContract
{
/**
* The routes discovered by the router.
Expand All @@ -44,9 +44,9 @@ class Router implements RouterContract
protected Collection $routes;

/**
* @var \Hyde\Framework\Router|null The singleton instance of the router.
* @var \Hyde\Framework\Services\RoutingService|null The singleton instance of the router.
*/
protected static Router|null $instance = null;
protected static RoutingService|null $instance = null;

/** @inheritDoc */
public function __construct()
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/tests/Feature/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Models\Route;
use Hyde\Framework\Router;
use Hyde\Framework\Services\RoutingService;
use Hyde\Testing\TestCase;

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ public function test_get_from_model_returns_the_models_route()

public function test_route_facade_all_method_returns_all_routes()
{
$this->assertEquals(Router::getInstance()->getRoutes(), Route::all());
$this->assertEquals(RoutingService::getInstance()->getRoutes(), Route::all());
}

public function test_get_link_returns_correct_path_for_root_pages()
Expand Down
30 changes: 15 additions & 15 deletions packages/framework/tests/Feature/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,33 @@
use Hyde\Framework\Models\Pages\MarkdownPage;
use Hyde\Framework\Models\Pages\MarkdownPost;
use Hyde\Framework\Models\Route;
use Hyde\Framework\Router;
use Hyde\Framework\Services\RoutingService;
use Hyde\Testing\TestCase;
use Illuminate\Support\Collection;

/**
* @covers \Hyde\Framework\Router
* @covers \Hyde\Framework\Services\RoutingService
*/
class RouterTest extends TestCase
{
/**
* @covers \Hyde\Framework\Router::getInstance
* @covers \Hyde\Framework\Services\RoutingService::getInstance
*/
public function test_get_instance_returns_the_router_instance()
{
$this->assertInstanceOf(Router::class, Router::getInstance());
$this->assertEquals(Router::getInstance(), Router::getInstance());
$this->assertInstanceOf(RoutingService::class, RoutingService::getInstance());
$this->assertEquals(RoutingService::getInstance(), RoutingService::getInstance());
}

/**
* Test route autodiscovery.
*
* @covers \Hyde\Framework\Router::__construct
* @covers \Hyde\Framework\Router::getRoutes
* @covers \Hyde\Framework\Services\RoutingService::__construct
* @covers \Hyde\Framework\Services\RoutingService::getRoutes
*/
public function test_get_routes_returns_discovered_routes()
{
$routes = (new Router())->getRoutes();
$routes = (new RoutingService())->getRoutes();

$this->assertContainsOnlyInstancesOf(RouteContract::class, $routes);

Expand All @@ -47,13 +47,13 @@ public function test_get_routes_returns_discovered_routes()
}

/**
* @covers \Hyde\Framework\Router::getRoutesForModel
* @covers \Hyde\Framework\Services\RoutingService::getRoutesForModel
*/
public function test_get_routes_for_model_returns_only_routes_for_the_given_model()
{
Hyde::touch(('_pages/foo.md'));

$routes = (new Router())->getRoutesForModel(MarkdownPage::class);
$routes = (new RoutingService())->getRoutesForModel(MarkdownPage::class);

$this->assertEquals(collect([
'foo' => new Route(MarkdownPage::parse('foo')),
Expand All @@ -65,9 +65,9 @@ public function test_get_routes_for_model_returns_only_routes_for_the_given_mode
/**
* Test route autodiscovery.
*
* @covers \Hyde\Framework\Router::discover
* @covers \Hyde\Framework\Router::discoverRoutes
* @covers \Hyde\Framework\Router::discoverPageRoutes
* @covers \Hyde\Framework\Services\RoutingService::discover
* @covers \Hyde\Framework\Services\RoutingService::discoverRoutes
* @covers \Hyde\Framework\Services\RoutingService::discoverPageRoutes
*/
public function test_discover_routes_finds_and_adds_all_pages_to_route_collection()
{
Expand All @@ -94,7 +94,7 @@ public function test_routes_are_not_discovered_for_disabled_features()
touch('_posts/post.md');
touch('_docs/doc.md');

$this->assertEmpty((new Router())->getRoutes());
$this->assertEmpty((new RoutingService())->getRoutes());

unlink('_pages/blade.blade.php');
unlink('_pages/markdown.md');
Expand Down Expand Up @@ -129,7 +129,7 @@ protected function testRouteModelDiscoveryForPageModel(string $class)
$expectedKey => new Route($class::parse('foo')),
]);

$this->assertEquals($expected, (new Router())->getRoutes());
$this->assertEquals($expected, (new RoutingService())->getRoutes());
unlink(Hyde::path($class::qualifyBasename('foo')));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/realtime-compiler/src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function handle(): Response
}

// Temporary backwards compatibility for versions less than Hyde/Framework v0.48.0-beta
if (! class_exists('\Hyde\Framework\Router')) {
if (! class_exists('\Hyde\Framework\Services\RoutingService')) {
return LegacyPageRouter::handle($this->request);
}

Expand Down