1
0
mirror of https://github.com/flarum/core.git synced 2025-07-23 09:41:26 +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:
Toby Zerner
2018-11-16 13:54:13 +10:30
parent 59330d8fe6
commit 171f9184d9
34 changed files with 519 additions and 946 deletions

View File

@@ -12,9 +12,9 @@
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Frontend\Asset\ExtensionAssets;
use Flarum\Frontend\CompilerFactory;
use Flarum\Frontend\HtmlDocumentFactory;
use Flarum\Frontend\Assets;
use Flarum\Frontend\Compiler\Source\SourceCollector;
use Flarum\Frontend\RecompileFrontendAssets;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
@@ -77,16 +77,42 @@ class Frontend implements ExtenderInterface
return;
}
$container->resolving(
"flarum.$this->frontend.assets",
function (CompilerFactory $assets) use ($moduleName) {
$assets->add(function () use ($moduleName) {
return new ExtensionAssets(
$moduleName, $this->css, $this->js
);
$abstract = 'flarum.assets.'.$this->frontend;
$container->resolving($abstract, function (Assets $assets) use ($moduleName) {
if ($this->js) {
$assets->js(function (SourceCollector $sources) use ($moduleName) {
$sources->addString(function () {
return 'var module={}';
});
$sources->addFile($this->js);
$sources->addString(function () use ($moduleName) {
return "flarum.extensions['$moduleName']=module.exports";
});
});
}
);
if ($this->css) {
$assets->css(function (SourceCollector $sources) {
foreach ($this->css as $path) {
$sources->addFile($path);
}
});
}
});
if (! $container->bound($abstract)) {
$container->bind($abstract, function (Container $container) {
return $container->make('flarum.assets.factory')($this->frontend);
});
$container->make('events')->subscribe(
new RecompileFrontendAssets(
$container->make($abstract),
$container->make('flarum.locales')
)
);
}
}
private function registerRoutes(Container $container)
@@ -114,13 +140,13 @@ class Frontend implements ExtenderInterface
$container->resolving(
"flarum.$this->frontend.frontend",
function (HtmlDocumentFactory $view, Container $container) {
function (Frontend $frontend, Container $container) {
foreach ($this->content as $content) {
if (is_string($content)) {
$content = $container->make($content);
}
$view->add($content);
$frontend->content($content);
}
}
);