1
0
mirror of https://github.com/flarum/core.git synced 2025-10-19 02:36:08 +02:00

Refactor the web app bootstrapping code

- All custom JS variables are now preloaded into the `app.data` object, rather than directly on the `app` object. This means that admin settings are available in `app.data.settings` rather than `app.settings`, etc.
- Cleaner route handler generation
- Renamed ConfigureClientView to ConfigureWebApp, though the former still exists and is deprecated
- Partial fix for #881 (strips ?nojs=1 from URL if possible, so that refreshing will attempt to load JS version again)
This commit is contained in:
Toby Zerner
2016-05-26 19:04:24 +09:30
parent 2525e3e7ad
commit 9bfb797fdc
49 changed files with 1575 additions and 1254 deletions

View File

@@ -14,13 +14,11 @@ use Flarum\Event\ExtensionWasDisabled;
use Flarum\Event\ExtensionWasEnabled;
use Flarum\Event\SettingWasSet;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Http\GenerateRouteHandlerTrait;
use Flarum\Http\Handler\RouteHandlerFactory;
use Flarum\Http\RouteCollection;
class AdminServiceProvider extends AbstractServiceProvider
{
use GenerateRouteHandlerTrait;
/**
* {@inheritdoc}
*/
@@ -44,9 +42,9 @@ class AdminServiceProvider extends AbstractServiceProvider
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin');
$this->flushAssetsWhenThemeChanged();
$this->flushWebAppAssetsWhenThemeChanged();
$this->flushAssetsWhenExtensionsChanged();
$this->flushWebAppAssetsWhenExtensionsChanged();
}
/**
@@ -56,42 +54,42 @@ class AdminServiceProvider extends AbstractServiceProvider
*/
protected function populateRoutes(RouteCollection $routes)
{
$toController = $this->getHandlerGenerator($this->app);
$route = $this->app->make(RouteHandlerFactory::class);
$routes->get(
'/',
'index',
$toController('Flarum\Admin\Controller\ClientController')
$route->toController(Controller\WebAppController::class)
);
}
protected function flushAssetsWhenThemeChanged()
protected function flushWebAppAssetsWhenThemeChanged()
{
$this->app->make('events')->listen(SettingWasSet::class, function (SettingWasSet $event) {
if (preg_match('/^theme_|^custom_less$/i', $event->key)) {
$this->getClientController()->flushCss();
$this->getWebAppAssets()->flushCss();
}
});
}
protected function flushAssetsWhenExtensionsChanged()
protected function flushWebAppAssetsWhenExtensionsChanged()
{
$events = $this->app->make('events');
$events->listen(ExtensionWasEnabled::class, [$this, 'flushAssets']);
$events->listen(ExtensionWasDisabled::class, [$this, 'flushAssets']);
$events->listen(ExtensionWasEnabled::class, [$this, 'flushWebAppAssets']);
$events->listen(ExtensionWasDisabled::class, [$this, 'flushWebAppAssets']);
}
public function flushAssets()
public function flushWebAppAssets()
{
$this->getClientController()->flushAssets();
$this->getWebAppAssets()->flush();
}
/**
* @return \Flarum\Admin\Controller\ClientController
* @return \Flarum\Http\WebApp\WebAppAssets
*/
protected function getClientController()
protected function getWebAppAssets()
{
return $this->app->make('Flarum\Admin\Controller\ClientController');
return $this->app->make(WebApp::class)->getAssets();
}
}

View File

@@ -10,29 +10,23 @@
namespace Flarum\Admin\Controller;
use Flarum\Api\Client;
use Flarum\Admin\WebApp;
use Flarum\Core\Permission;
use Flarum\Event\PrepareUnserializedSettings;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\Application;
use Flarum\Http\Controller\AbstractClientController as BaseClientController;
use Flarum\Http\Controller\AbstractWebAppController;
use Flarum\Locale\LocaleManager;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Events\Dispatcher;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ServerRequestInterface;
class ClientController extends BaseClientController
class WebAppController extends AbstractWebAppController
{
/**
* {@inheritdoc}
* @var SettingsRepositoryInterface
*/
protected $clientName = 'admin';
/**
* {@inheritdoc}
*/
protected $translations = '/^[^\.]+\.(?:admin|lib)\./';
protected $settings;
/**
* @var ExtensionManager
@@ -40,29 +34,25 @@ class ClientController extends BaseClientController
protected $extensions;
/**
* {@inheritdoc}
* @param WebApp $webApp
* @param Dispatcher $events
* @param SettingsRepositoryInterface $settings
* @param ExtensionManager $extensions
*/
public function __construct(
Application $app,
Client $apiClient,
LocaleManager $locales,
SettingsRepositoryInterface $settings,
Dispatcher $events,
Repository $cache,
ExtensionManager $extensions
) {
BaseClientController::__construct($app, $apiClient, $locales, $settings, $events, $cache);
$this->layout = __DIR__.'/../../../views/admin.blade.php';
public function __construct(WebApp $webApp, Dispatcher $events, SettingsRepositoryInterface $settings, ExtensionManager $extensions)
{
$this->webApp = $webApp;
$this->events = $events;
$this->settings = $settings;
$this->extensions = $extensions;
}
/**
* {@inheritdoc}
*/
public function render(Request $request)
protected function getView(ServerRequestInterface $request)
{
$view = BaseClientController::render($request);
$view = parent::getView($request);
$settings = $this->settings->all();

24
src/Admin/WebApp.php Normal file
View File

@@ -0,0 +1,24 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Admin;
use Flarum\Http\WebApp\AbstractWebApp;
class WebApp extends AbstractWebApp
{
/**
* {@inheritdoc}
*/
protected function getName()
{
return 'admin';
}
}