1
0
mirror of https://github.com/flarum/core.git synced 2025-10-12 23:44:27 +02:00

Frontend refactor (#1471)

Refactor Frontend + Asset code

- Use Laravel's Filesystem component for asset IO, meaning theoretically
  assets should be storable on S3 etc.

- More reliable checking for asset recompilation when debug mode is on,
  so you don't have to constantly delete the compiled assets to force
  a recompile. Should also fix issues with locale JS files being
  recompiled with the same name and cached.

- Remove JavaScript minification, because it will be done by Webpack
  (exception is for the TextFormatter JS).

- Add support for JS sourcemaps.

- Separate frontend view and assets completely. This is an important
  distinction because frontend assets are compiled independent of a
  request, whereas putting together a view depends on a request.

- Bind frontend view/asset factory instances to the container (in
  service providers) rather than subclassing. Asset and content
  populators can be added to these factories – these are simply objects
  that populate the asset compilers or the view with information.

- Add RouteHandlerFactory functions that make it easy to hook up a
  frontend controller with a frontend instance ± some content.

- Remove the need for "nojs"

- Fix cache:clear command

- Recompile assets when settings/enabled extensions change
This commit is contained in:
Toby Zerner
2018-06-30 12:31:12 +09:30
committed by GitHub
parent 0f5ddc1c43
commit 0e73785498
73 changed files with 2846 additions and 2176 deletions

View File

@@ -13,9 +13,8 @@ namespace Flarum\Admin;
use Flarum\Admin\Middleware\RequireAdministrateAbility;
use Flarum\Event\ConfigureMiddleware;
use Flarum\Extension\Event\Disabled;
use Flarum\Extension\Event\Enabled;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Frontend\RecompileFrontendAssets;
use Flarum\Http\Middleware\AuthenticateWithSession;
use Flarum\Http\Middleware\DispatchRoute;
use Flarum\Http\Middleware\HandleErrors;
@@ -26,7 +25,6 @@ use Flarum\Http\Middleware\StartSession;
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\Event\Saved;
use Zend\Stratigility\MiddlewarePipe;
class AdminServiceProvider extends AbstractServiceProvider
@@ -64,6 +62,19 @@ class AdminServiceProvider extends AbstractServiceProvider
return $pipe;
});
$this->app->bind('flarum.admin.assets', function () {
return $this->app->make('flarum.frontend.assets.defaults')('admin');
});
$this->app->bind('flarum.admin.frontend', function () {
$view = $this->app->make('flarum.frontend.view.defaults')('admin');
$view->setAssets($this->app->make('flarum.admin.assets'));
$view->add($this->app->make(Content\AdminPayload::class));
return $view;
});
}
/**
@@ -75,12 +86,15 @@ class AdminServiceProvider extends AbstractServiceProvider
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin');
$this->registerListeners();
$this->app->make('events')->subscribe(
new RecompileFrontendAssets(
$this->app->make('flarum.admin.assets'),
$this->app->make('flarum.locales')
)
);
}
/**
* Populate the forum client routes.
*
* @param RouteCollection $routes
*/
protected function populateRoutes(RouteCollection $routes)
@@ -90,36 +104,4 @@ class AdminServiceProvider extends AbstractServiceProvider
$callback = include __DIR__.'/routes.php';
$callback($routes, $factory);
}
protected function registerListeners()
{
$dispatcher = $this->app->make('events');
// Flush web app assets when the theme is changed
$dispatcher->listen(Saved::class, function (Saved $event) {
if (preg_match('/^theme_|^custom_less$/i', $event->key)) {
$this->getWebAppAssets()->flushCss();
}
});
// Flush web app assets when extensions are changed
$dispatcher->listen(Enabled::class, [$this, 'flushWebAppAssets']);
$dispatcher->listen(Disabled::class, [$this, 'flushWebAppAssets']);
// Check the format of custom LESS code
$dispatcher->subscribe(CheckCustomLessFormat::class);
}
public function flushWebAppAssets()
{
$this->getWebAppAssets()->flush();
}
/**
* @return \Flarum\Frontend\FrontendAssets
*/
protected function getWebAppAssets()
{
return $this->app->make(Frontend::class)->getAssets();
}
}

View File

@@ -1,43 +0,0 @@
<?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\Foundation\ValidationException;
use Flarum\Settings\Event\Serializing;
use Illuminate\Contracts\Events\Dispatcher;
use Less_Exception_Parser;
use Less_Parser;
class CheckCustomLessFormat
{
public function subscribe(Dispatcher $events)
{
$events->listen(Serializing::class, [$this, 'check']);
}
public function check(Serializing $event)
{
if ($event->key === 'custom_less') {
$parser = new Less_Parser();
try {
// Check the custom less format before saving
// Variables names are not checked, we would have to set them and call getCss() to check them
$parser->parse($event->value);
} catch (Less_Exception_Parser $e) {
throw new ValidationException([
'custom_less' => $e->getMessage(),
]);
}
}
}
}

View File

@@ -0,0 +1,60 @@
<?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\Content;
use Flarum\Extension\ExtensionManager;
use Flarum\Frontend\Content\ContentInterface;
use Flarum\Frontend\HtmlDocument;
use Flarum\Group\Permission;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Database\ConnectionInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
class AdminPayload implements ContentInterface
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var ExtensionManager
*/
protected $extensions;
/**
* @var ConnectionInterface
*/
protected $db;
/**
* @param SettingsRepositoryInterface $settings
* @param ExtensionManager $extensions
* @param ConnectionInterface $db
*/
public function __construct(SettingsRepositoryInterface $settings, ExtensionManager $extensions, ConnectionInterface $db)
{
$this->settings = $settings;
$this->extensions = $extensions;
$this->db = $db;
}
public function populate(HtmlDocument $document, Request $request)
{
$document->payload['settings'] = $this->settings->all();
$document->payload['permissions'] = Permission::map();
$document->payload['extensions'] = $this->extensions->getExtensions()->toArray();
$document->payload['phpVersion'] = PHP_VERSION;
$document->payload['mysqlVersion'] = $this->db->selectOne('select version() as version')->version;
}
}

View File

@@ -1,79 +0,0 @@
<?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\Controller;
use Flarum\Admin\Frontend;
use Flarum\Extension\ExtensionManager;
use Flarum\Frontend\AbstractFrontendController;
use Flarum\Group\Permission;
use Flarum\Settings\Event\Deserializing;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\ConnectionInterface;
use Psr\Http\Message\ServerRequestInterface;
class FrontendController extends AbstractFrontendController
{
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/**
* @var ExtensionManager
*/
protected $extensions;
/**
* @var ConnectionInterface
*/
protected $db;
/**
* @param Frontend $webApp
* @param Dispatcher $events
* @param SettingsRepositoryInterface $settings
* @param ExtensionManager $extensions
* @param ConnectionInterface $db
*/
public function __construct(Frontend $webApp, Dispatcher $events, SettingsRepositoryInterface $settings, ExtensionManager $extensions, ConnectionInterface $db)
{
$this->webApp = $webApp;
$this->events = $events;
$this->settings = $settings;
$this->extensions = $extensions;
$this->db = $db;
}
/**
* {@inheritdoc}
*/
protected function getView(ServerRequestInterface $request)
{
$view = parent::getView($request);
$settings = $this->settings->all();
$this->events->dispatch(
new Deserializing($settings)
);
$view->setVariable('settings', $settings);
$view->setVariable('permissions', Permission::map());
$view->setVariable('extensions', $this->extensions->getExtensions()->toArray());
$view->setVariable('phpVersion', PHP_VERSION);
$view->setVariable('mysqlVersion', $this->db->selectOne('select version() as version')->version);
return $view;
}
}

View File

@@ -1,25 +0,0 @@
<?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\Frontend\AbstractFrontend;
class Frontend extends AbstractFrontend
{
/**
* {@inheritdoc}
*/
protected function getName()
{
return 'admin';
}
}

View File

@@ -9,7 +9,6 @@
* file that was distributed with this source code.
*/
use Flarum\Admin\Controller;
use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory;
@@ -17,6 +16,6 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
$map->get(
'/',
'index',
$route->toController(Controller\FrontendController::class)
$route->toAdmin()
);
};