1
0
mirror of https://github.com/flarum/core.git synced 2025-07-31 13:40:20 +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 9e63f32105
commit edaca3160e
34 changed files with 519 additions and 946 deletions

View File

@@ -0,0 +1,74 @@
<?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\Frontend\Content;
use Flarum\Foundation\Application;
use Flarum\Frontend\Compiler\CompilerInterface;
use Flarum\Frontend\Document;
use Psr\Http\Message\ServerRequestInterface as Request;
class Assets
{
protected $app;
/**
* @var \Flarum\Frontend\Assets
*/
protected $assets;
public function __construct(Application $app)
{
$this->app = $app;
}
public function forFrontend(string $name)
{
$this->assets = $this->app->make('flarum.assets.'.$name);
return $this;
}
public function __invoke(Document $document, Request $request)
{
$locale = $request->getAttribute('locale');
$compilers = [
'js' => [$this->assets->makeJs(), $this->assets->makeLocaleJs($locale)],
'css' => [$this->assets->makeCss(), $this->assets->makeLocaleCss($locale)]
];
if ($this->app->inDebugMode()) {
$this->commit(array_flatten($compilers));
}
$document->js = array_merge($document->js, $this->getUrls($compilers['js']));
$document->css = array_merge($document->css, $this->getUrls($compilers['css']));
}
private function commit(array $compilers)
{
foreach ($compilers as $compiler) {
$compiler->commit();
}
}
/**
* @param CompilerInterface[] $compilers
* @return string[]
*/
private function getUrls(array $compilers)
{
return array_filter(array_map(function (CompilerInterface $compiler) {
return $compiler->getUrl();
}, $compilers));
}
}

View File

@@ -1,24 +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\Frontend\Content;
use Flarum\Frontend\HtmlDocument;
use Psr\Http\Message\ServerRequestInterface as Request;
interface ContentInterface
{
/**
* @param HtmlDocument $document
* @param Request $request
*/
public function __invoke(HtmlDocument $document, Request $request);
}

View File

@@ -13,13 +13,13 @@ namespace Flarum\Frontend\Content;
use Flarum\Api\Client;
use Flarum\Api\Controller\ShowUserController;
use Flarum\Frontend\HtmlDocument;
use Flarum\Frontend\Document;
use Flarum\Locale\LocaleManager;
use Flarum\User\User;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
class CorePayload implements ContentInterface
class CorePayload
{
/**
* @var LocaleManager
@@ -41,7 +41,7 @@ class CorePayload implements ContentInterface
$this->api = $api;
}
public function __invoke(HtmlDocument $document, Request $request)
public function __invoke(Document $document, Request $request)
{
$document->payload = array_merge(
$document->payload,
@@ -49,7 +49,7 @@ class CorePayload implements ContentInterface
);
}
private function buildPayload(HtmlDocument $document, Request $request)
private function buildPayload(Document $document, Request $request)
{
$data = $this->getDataFromApiDocument($document->getForumApiDocument());

View File

@@ -1,36 +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\Frontend\Content;
use Flarum\Frontend\HtmlDocument;
use Psr\Http\Message\ServerRequestInterface as Request;
class Layout implements ContentInterface
{
/**
* @var string
*/
protected $layoutView;
/**
* @param string $layoutView
*/
public function __construct(string $layoutView)
{
$this->layoutView = $layoutView;
}
public function __invoke(HtmlDocument $document, Request $request)
{
$document->layoutView = $this->layoutView;
}
}

View File

@@ -11,18 +11,18 @@
namespace Flarum\Frontend\Content;
use Flarum\Frontend\HtmlDocument;
use Flarum\Frontend\Document;
use Psr\Http\Message\ServerRequestInterface as Request;
class Meta implements ContentInterface
class Meta
{
public function __invoke(HtmlDocument $document, Request $request)
public function __invoke(Document $document, Request $request)
{
$document->meta = array_merge($document->meta, $this->buildMeta($document));
$document->head = array_merge($document->head, $this->buildHead($document));
}
private function buildMeta(HtmlDocument $document)
private function buildMeta(Document $document)
{
$forumApiDocument = $document->getForumApiDocument();
@@ -35,7 +35,7 @@ class Meta implements ContentInterface
return $meta;
}
private function buildHead(HtmlDocument $document)
private function buildHead(Document $document)
{
$head = [];