mirror of
https://github.com/flarum/core.git
synced 2025-10-15 00:44:40 +02:00
Refactor frontend code to allow for extension of assets
- Simpler class naming: Frontend\CompilerFactory → Frontend\Assets Frontend\HtmlDocumentFactory → Frontend\Frontend Frontend\HtmlDocument → Frontend\Document - Remove AssetInterface and simply collect callbacks in Frontend\Assets instead - Remove ContentInterface because it serves no purpose (never type- hinted or type-checked) - Commit and add asset URLs to the Document via a content callback instead of in the Document factory class itself - Add translations and locale assets to Assets separate to the assets factory, as non-forum/admin asset bundles probably won't want them - Update Frontend Extender to allow the creation of new asset bundles - Make custom LESS validation listener a standalone class instead of extending RecompileFrontendAssets
This commit is contained in:
@@ -12,21 +12,18 @@
|
||||
namespace Flarum\Frontend;
|
||||
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Frontend\Compiler\Source\SourceCollector;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\View\Factory as ViewFactory;
|
||||
|
||||
class FrontendServiceProvider extends AbstractServiceProvider
|
||||
{
|
||||
public function register()
|
||||
{
|
||||
// Yo dawg, I heard you like factories, so I made you a factory to
|
||||
// create your factory. We expose a couple of factory functions that
|
||||
// will create frontend factories and configure them with some default
|
||||
// settings common to both the forum and admin frontends.
|
||||
|
||||
$this->app->singleton('flarum.frontend.assets.defaults', function () {
|
||||
$this->app->singleton('flarum.assets.factory', function () {
|
||||
return function (string $name) {
|
||||
$assets = new CompilerFactory(
|
||||
$assets = new Assets(
|
||||
$name,
|
||||
$this->app->make('filesystem')->disk('flarum-assets'),
|
||||
$this->app->storagePath()
|
||||
@@ -36,35 +33,26 @@ class FrontendServiceProvider extends AbstractServiceProvider
|
||||
$this->app->basePath().'/vendor/components/font-awesome/less' => ''
|
||||
]);
|
||||
|
||||
$assets->add(function () use ($name) {
|
||||
$translations = $this->app->make(Asset\Translations::class);
|
||||
$translations->setFilter(function (string $id) use ($name) {
|
||||
return preg_match('/^.+(?:\.|::)(?:'.$name.'|lib)\./', $id);
|
||||
});
|
||||
|
||||
return [
|
||||
new Asset\CoreAssets($name),
|
||||
$this->app->make(Asset\LessVariables::class),
|
||||
$translations,
|
||||
$this->app->make(Asset\LocaleAssets::class)
|
||||
];
|
||||
});
|
||||
$assets->css([$this, 'addLessVariables']);
|
||||
$assets->localeCss([$this, 'addLessVariables']);
|
||||
|
||||
return $assets;
|
||||
};
|
||||
});
|
||||
|
||||
$this->app->singleton('flarum.frontend.view.defaults', function () {
|
||||
$this->app->singleton('flarum.frontend.factory', function () {
|
||||
return function (string $name) {
|
||||
$view = $this->app->make(HtmlDocumentFactory::class);
|
||||
$frontend = $this->app->make(Frontend::class);
|
||||
|
||||
$view->setCommitAssets($this->app->inDebugMode());
|
||||
$frontend->content(function (Document $document) use ($name) {
|
||||
$document->layoutView = 'flarum::frontend.'.$name;
|
||||
});
|
||||
|
||||
$view->add(new Content\Layout('flarum::frontend.'.$name));
|
||||
$view->add($this->app->make(Content\CorePayload::class));
|
||||
$view->add($this->app->make(Content\Meta::class));
|
||||
$frontend->content($this->app->make(Content\Assets::class)->forFrontend($name));
|
||||
$frontend->content($this->app->make(Content\CorePayload::class));
|
||||
$frontend->content($this->app->make(Content\Meta::class));
|
||||
|
||||
return $view;
|
||||
return $frontend;
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -81,4 +69,22 @@ class FrontendServiceProvider extends AbstractServiceProvider
|
||||
'url' => $this->app->make(UrlGenerator::class)
|
||||
]);
|
||||
}
|
||||
|
||||
public function addLessVariables(SourceCollector $sources)
|
||||
{
|
||||
$sources->addString(function () {
|
||||
$settings = $this->app->make(SettingsRepositoryInterface::class);
|
||||
|
||||
$vars = [
|
||||
'config-primary-color' => $settings->get('theme_primary_color', '#000'),
|
||||
'config-secondary-color' => $settings->get('theme_secondary_color', '#000'),
|
||||
'config-dark-mode' => $settings->get('theme_dark_mode') ? 'true' : 'false',
|
||||
'config-colored-header' => $settings->get('theme_colored_header') ? 'true' : 'false'
|
||||
];
|
||||
|
||||
return array_reduce(array_keys($vars), function ($string, $name) use ($vars) {
|
||||
return $string."@$name: {$vars[$name]};";
|
||||
}, '');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user