mirror of
https://github.com/flarum/core.git
synced 2025-10-13 07:54:25 +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:
84
src/Frontend/Frontend.php
Normal file
84
src/Frontend/Frontend.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?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;
|
||||
|
||||
use Flarum\Api\Client;
|
||||
use Flarum\Api\Controller\ShowForumController;
|
||||
use Illuminate\Contracts\View\Factory;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
class Frontend
|
||||
{
|
||||
/**
|
||||
* @var Factory
|
||||
*/
|
||||
protected $view;
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
protected $api;
|
||||
|
||||
/**
|
||||
* @var callable[]
|
||||
*/
|
||||
protected $content = [];
|
||||
|
||||
public function __construct(Factory $view, Client $api)
|
||||
{
|
||||
$this->view = $view;
|
||||
$this->api = $api;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable $content
|
||||
*/
|
||||
public function content(callable $content)
|
||||
{
|
||||
$this->content[] = $content;
|
||||
}
|
||||
|
||||
public function document(Request $request): Document
|
||||
{
|
||||
$forumDocument = $this->getForumDocument($request);
|
||||
|
||||
$document = new Document($this->view, $forumDocument);
|
||||
|
||||
$this->populate($document, $request);
|
||||
|
||||
return $document;
|
||||
}
|
||||
|
||||
protected function populate(Document $document, Request $request)
|
||||
{
|
||||
foreach ($this->content as $content) {
|
||||
$content($document, $request);
|
||||
}
|
||||
}
|
||||
|
||||
private function getForumDocument(Request $request): array
|
||||
{
|
||||
$actor = $request->getAttribute('actor');
|
||||
|
||||
$this->api->setErrorHandler(null);
|
||||
|
||||
return $this->getResponseBody(
|
||||
$this->api->send(ShowForumController::class, $actor)
|
||||
);
|
||||
}
|
||||
|
||||
private function getResponseBody(Response $response): array
|
||||
{
|
||||
return json_decode($response->getBody(), true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user