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:
@@ -13,8 +13,6 @@ namespace Flarum\Forum;
|
||||
|
||||
use Flarum\Event\ConfigureForumRoutes;
|
||||
use Flarum\Event\ConfigureMiddleware;
|
||||
use Flarum\Extension\Event\Disabled;
|
||||
use Flarum\Extension\Event\Enabled;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Http\Middleware\AuthenticateWithSession;
|
||||
use Flarum\Http\Middleware\CollectGarbage;
|
||||
@@ -28,7 +26,6 @@ use Flarum\Http\Middleware\StartSession;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\RouteHandlerFactory;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\Event\Saved;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Zend\Stratigility\MiddlewarePipe;
|
||||
@@ -69,6 +66,27 @@ class ForumServiceProvider extends AbstractServiceProvider
|
||||
|
||||
return $pipe;
|
||||
});
|
||||
|
||||
$this->app->bind('flarum.forum.assets', function () {
|
||||
$assets = $this->app->make('flarum.frontend.assets.defaults')('forum');
|
||||
|
||||
$assets->add(function () {
|
||||
return [
|
||||
$this->app->make(Asset\FormatterJs::class),
|
||||
$this->app->make(Asset\CustomCss::class)
|
||||
];
|
||||
});
|
||||
|
||||
return $assets;
|
||||
});
|
||||
|
||||
$this->app->bind('flarum.forum.frontend', function () {
|
||||
$view = $this->app->make('flarum.frontend.view.defaults')('forum');
|
||||
|
||||
$view->setAssets($this->app->make('flarum.forum.assets'));
|
||||
|
||||
return $view;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,9 +103,13 @@ class ForumServiceProvider extends AbstractServiceProvider
|
||||
'settings' => $this->app->make(SettingsRepositoryInterface::class)
|
||||
]);
|
||||
|
||||
$this->flushWebAppAssetsWhenThemeChanged();
|
||||
|
||||
$this->flushWebAppAssetsWhenExtensionsChanged();
|
||||
$this->app->make('events')->subscribe(
|
||||
new RecompileFrontendAssets(
|
||||
$this->app->make('flarum.forum.assets'),
|
||||
$this->app->make('flarum.locales'),
|
||||
$this->app
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -111,7 +133,7 @@ class ForumServiceProvider extends AbstractServiceProvider
|
||||
if (isset($routes->getRouteData()[0]['GET'][$defaultRoute])) {
|
||||
$toDefaultController = $routes->getRouteData()[0]['GET'][$defaultRoute];
|
||||
} else {
|
||||
$toDefaultController = $factory->toController(Controller\IndexController::class);
|
||||
$toDefaultController = $factory->toForum(Content\Index::class);
|
||||
}
|
||||
|
||||
$routes->get(
|
||||
@@ -120,34 +142,4 @@ class ForumServiceProvider extends AbstractServiceProvider
|
||||
$toDefaultController
|
||||
);
|
||||
}
|
||||
|
||||
protected function flushWebAppAssetsWhenThemeChanged()
|
||||
{
|
||||
$this->app->make('events')->listen(Saved::class, function (Saved $event) {
|
||||
if (preg_match('/^theme_|^custom_less$/i', $event->key)) {
|
||||
$this->getWebAppAssets()->flushCss();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
protected function flushWebAppAssetsWhenExtensionsChanged()
|
||||
{
|
||||
$events = $this->app->make('events');
|
||||
|
||||
$events->listen(Enabled::class, [$this, 'flushWebAppAssets']);
|
||||
$events->listen(Disabled::class, [$this, 'flushWebAppAssets']);
|
||||
}
|
||||
|
||||
public function flushWebAppAssets()
|
||||
{
|
||||
$this->getWebAppAssets()->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Flarum\Frontend\FrontendAssets
|
||||
*/
|
||||
protected function getWebAppAssets()
|
||||
{
|
||||
return $this->app->make(Frontend::class)->getAssets();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user