]> BookStack Code Mirror - bookstack/commitdiff
LogicalTheme: Added events for registering web routes
authorDan Brown <redacted>
Fri, 17 Nov 2023 13:45:57 +0000 (13:45 +0000)
committerDan Brown <redacted>
Fri, 17 Nov 2023 13:45:57 +0000 (13:45 +0000)
Added to allow easier registration of routes.
Added for normal web and authed routes.
Included testing to cover.

app/App/Providers/RouteServiceProvider.php
app/App/Providers/ThemeServiceProvider.php
app/Theming/ThemeEvents.php
tests/ThemeTest.php

index 913dfa4353f62739e60691587fb78a557f917051..abd5562449b775e16f2574f21e42852bc426f06b 100644 (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);
         });
     }
 
index 4c657d9123208dca5f7aaae592e3f1cfc152fb32..c15b43a6b4cdfe11002450717da9ef3b64a25fa5 100644 (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
index 4b56b2f56c119c596e2d8684a1e2569d65428bb7..9e14707dea055dc73014f2f0fd0a0853b259649f 100644 (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
index 53361e35194dad7b8fdec639373cc1a673bdfdd4..73b90143949a12b3b127761d94739e5ec81cbe42 100644 (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', [
Morty Proxy This is a proxified and sanitized view of the page, visit original site.