LogicalTheme: Added events for registering web routes

Added to allow easier registration of routes.
Added for normal web and authed routes.
Included testing to cover.
This commit is contained in:
Dan Brown 2023-11-17 13:45:57 +00:00
parent 37a17e858a
commit 22a9cf1e48
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 65 additions and 1 deletions

View File

@ -2,9 +2,12 @@
namespace BookStack\App\Providers;
use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
@ -46,8 +49,15 @@ class RouteServiceProvider extends ServiceProvider
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
], function (Router $router) {
require base_path('routes/web.php');
Theme::dispatch(ThemeEvents::ROUTES_REGISTER_WEB, $router);
});
Route::group([
'middleware' => ['web', 'auth'],
], function (Router $router) {
Theme::dispatch(ThemeEvents::ROUTES_REGISTER_WEB_AUTH, $router);
});
}

View File

@ -4,6 +4,7 @@ namespace BookStack\App\Providers;
use BookStack\Theming\ThemeEvents;
use BookStack\Theming\ThemeService;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class ThemeServiceProvider extends ServiceProvider

View File

@ -98,6 +98,25 @@ class ThemeEvents
*/
const PAGE_INCLUDE_PARSE = 'page_include_parse';
/**
* Routes register web event.
* Called when standard web (browser/non-api) app routes are registered.
* Provides an app router, so you can register your own web routes.
*
* @param \Illuminate\Routing\Router
*/
const ROUTES_REGISTER_WEB = 'routes_register_web';
/**
* Routes register web auth event.
* Called when auth-required web (browser/non-api) app routes can be registered.
* These are routes that typically require login to access (unless the instance is made public).
* Provides an app router, so you can register your own web routes.
*
* @param \Illuminate\Routing\Router
*/
const ROUTES_REGISTER_WEB_AUTH = 'routes_register_web_auth';
/**
* Web before middleware action.
* Runs before the request is handled but after all other middleware apart from those

View File

@ -256,6 +256,40 @@ class ThemeTest extends TestCase
$this->assertEquals($otherPage->id, $args[3]->id);
}
public function test_event_routes_register_web_and_web_auth()
{
$functionsContent = <<<'END'
<?php
use BookStack\Theming\ThemeEvents;
use BookStack\Facades\Theme;
use Illuminate\Routing\Router;
Theme::listen(ThemeEvents::ROUTES_REGISTER_WEB, function (Router $router) {
$router->get('/cat', fn () => 'cat')->name('say.cat');
});
Theme::listen(ThemeEvents::ROUTES_REGISTER_WEB_AUTH, function (Router $router) {
$router->get('/dog', fn () => 'dog')->name('say.dog');
});
END;
$this->usingThemeFolder(function () use ($functionsContent) {
$functionsFile = theme_path('functions.php');
file_put_contents($functionsFile, $functionsContent);
$app = $this->createApplication();
/** @var \Illuminate\Routing\Router $router */
$router = $app->get('router');
/** @var \Illuminate\Routing\Route $catRoute */
$catRoute = $router->getRoutes()->getRoutesByName()['say.cat'];
$this->assertEquals(['web'], $catRoute->middleware());
/** @var \Illuminate\Routing\Route $dogRoute */
$dogRoute = $router->getRoutes()->getRoutesByName()['say.dog'];
$this->assertEquals(['web', 'auth'], $dogRoute->middleware());
});
}
public function test_add_social_driver()
{
Theme::addSocialDriver('catnet', [