1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 07:27:39 +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:
Toby Zerner
2018-06-30 12:31:12 +09:30
committed by GitHub
parent 0f5ddc1c43
commit 0e73785498
73 changed files with 2846 additions and 2176 deletions

View File

@@ -0,0 +1,24 @@
<?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 populate(HtmlDocument $document, Request $request);
}

View File

@@ -0,0 +1,99 @@
<?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\Api\Client;
use Flarum\Api\Controller\ShowUserController;
use Flarum\Frontend\HtmlDocument;
use Flarum\Locale\LocaleManager;
use Flarum\User\User;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
class CorePayload implements ContentInterface
{
/**
* @var LocaleManager
*/
private $locales;
/**
* @var Client
*/
private $api;
/**
* @param LocaleManager $locales
* @param Client $api
*/
public function __construct(LocaleManager $locales, Client $api)
{
$this->locales = $locales;
$this->api = $api;
}
public function populate(HtmlDocument $document, Request $request)
{
$document->payload = array_merge(
$document->payload,
$this->buildPayload($document, $request)
);
}
private function buildPayload(HtmlDocument $document, Request $request)
{
$data = $this->getDataFromApiDocument($document->getForumApiDocument());
$actor = $request->getAttribute('actor');
if ($actor->exists) {
$user = $this->getUserApiDocument($actor);
$data = array_merge($data, $this->getDataFromApiDocument($user));
}
return [
'resources' => $data,
'session' => [
'userId' => $actor->id,
'csrfToken' => $request->getAttribute('session')->token()
],
'locales' => $this->locales->getLocales(),
'locale' => $request->getAttribute('locale')
];
}
private function getDataFromApiDocument(array $apiDocument): array
{
$data[] = $apiDocument['data'];
if (isset($apiDocument['included'])) {
$data = array_merge($data, $apiDocument['included']);
}
return $data;
}
private function getUserApiDocument(User $user): array
{
// TODO: to avoid an extra query, something like
// $controller = new ShowUserController(new PreloadedUserRepository($user));
return $this->getResponseBody(
$this->api->send(ShowUserController::class, $user, ['id' => $user->id])
);
}
private function getResponseBody(ResponseInterface $response)
{
return json_decode($response->getBody(), true);
}
}

View File

@@ -0,0 +1,36 @@
<?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 populate(HtmlDocument $document, Request $request)
{
$document->layoutView = $this->layoutView;
}
}

View File

@@ -0,0 +1,50 @@
<?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 Meta implements ContentInterface
{
public function populate(HtmlDocument $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)
{
$forumApiDocument = $document->getForumApiDocument();
$meta = [
'viewport' => 'width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1',
'description' => array_get($forumApiDocument, 'data.attributes.forumDescription'),
'theme-color' => array_get($forumApiDocument, 'data.attributes.themePrimaryColor')
];
return $meta;
}
private function buildHead(HtmlDocument $document)
{
$head = [
'font' => '<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700,600">'
];
if ($faviconUrl = array_get($document->getForumApiDocument(), 'data.attributes.faviconUrl')) {
$head['favicon'] = '<link rel="shortcut icon" href="'.e($faviconUrl).'">';
}
return $head;
}
}