1
0
mirror of https://github.com/flarum/core.git synced 2025-02-26 12:15:32 +01:00

Ensure routes are only populated after extensions have registered listeners

Because extensions can have dependencies injected, a RouteCollection could potentially be instantiated, and thus the ConfigureRoutes event would be called before extensions have had a chance to subscribe to it. Instead, we instantiate the RouteCollection on demand, but only populate it when the application boots.
This commit is contained in:
Toby Zerner 2016-01-02 15:03:11 +10:30
parent e86cc39f5b
commit ff0ce09620
3 changed files with 18 additions and 24 deletions

View File

@ -33,7 +33,7 @@ class AdminServiceProvider extends AbstractServiceProvider
}); });
$this->app->singleton('flarum.admin.routes', function () { $this->app->singleton('flarum.admin.routes', function () {
return $this->getRoutes(); return new RouteCollection;
}); });
} }
@ -42,6 +42,8 @@ class AdminServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
$this->populateRoutes($this->app->make('flarum.admin.routes'));
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin'); $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin');
$this->flushAssetsWhenThemeChanged(); $this->flushAssetsWhenThemeChanged();
@ -50,14 +52,12 @@ class AdminServiceProvider extends AbstractServiceProvider
} }
/** /**
* Register the admin client routes. * Populate the forum client routes.
* *
* @return RouteCollection * @param RouteCollection $routes
*/ */
protected function getRoutes() protected function populateRoutes(RouteCollection $routes)
{ {
$routes = new RouteCollection;
$toController = $this->getHandlerGenerator($this->app); $toController = $this->getHandlerGenerator($this->app);
$routes->get( $routes->get(
@ -65,8 +65,6 @@ class AdminServiceProvider extends AbstractServiceProvider
'index', 'index',
$toController('Flarum\Admin\Controller\ClientController') $toController('Flarum\Admin\Controller\ClientController')
); );
return $routes;
} }
protected function flushAssetsWhenThemeChanged() protected function flushAssetsWhenThemeChanged()

View File

@ -36,7 +36,7 @@ class ApiServiceProvider extends AbstractServiceProvider
}); });
$this->app->singleton('flarum.api.routes', function () { $this->app->singleton('flarum.api.routes', function () {
return $this->getRoutes(); return new RouteCollection;
}); });
$this->app->singleton(ErrorHandler::class, function () { $this->app->singleton(ErrorHandler::class, function () {
@ -64,6 +64,8 @@ class ApiServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
$this->populateRoutes($this->app->make('flarum.api.routes'));
$this->registerNotificationSerializers(); $this->registerNotificationSerializers();
AbstractSerializeController::setContainer($this->app); AbstractSerializeController::setContainer($this->app);
@ -93,14 +95,12 @@ class ApiServiceProvider extends AbstractServiceProvider
} }
/** /**
* Get the API routes. * Populate the API routes.
* *
* @return RouteCollection * @param RouteCollection $routes
*/ */
protected function getRoutes() protected function populateRoutes(RouteCollection $routes)
{ {
$routes = new RouteCollection;
$toController = $this->getHandlerGenerator($this->app); $toController = $this->getHandlerGenerator($this->app);
// Get forum information // Get forum information
@ -366,7 +366,5 @@ class ApiServiceProvider extends AbstractServiceProvider
$this->app->make('events')->fire( $this->app->make('events')->fire(
new ConfigureApiRoutes($routes, $toController) new ConfigureApiRoutes($routes, $toController)
); );
return $routes;
} }
} }

View File

@ -32,7 +32,7 @@ class ForumServiceProvider extends AbstractServiceProvider
}); });
$this->app->singleton('flarum.forum.routes', function () { $this->app->singleton('flarum.forum.routes', function () {
return $this->getRoutes(); return new RouteCollection;
}); });
} }
@ -41,6 +41,8 @@ class ForumServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
$this->populateRoutes($this->app->make('flarum.forum.routes'));
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum'); $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum');
$this->flushAssetsWhenThemeChanged(); $this->flushAssetsWhenThemeChanged();
@ -49,14 +51,12 @@ class ForumServiceProvider extends AbstractServiceProvider
} }
/** /**
* Get the forum client routes. * Populate the forum client routes.
* *
* @return RouteCollection * @param RouteCollection $routes
*/ */
protected function getRoutes() protected function populateRoutes(RouteCollection $routes)
{ {
$routes = new RouteCollection;
$toController = $this->getHandlerGenerator($this->app); $toController = $this->getHandlerGenerator($this->app);
$routes->get( $routes->get(
@ -140,8 +140,6 @@ class ForumServiceProvider extends AbstractServiceProvider
'default', 'default',
$toDefaultController $toDefaultController
); );
return $routes;
} }
protected function flushAssetsWhenThemeChanged() protected function flushAssetsWhenThemeChanged()