Merge pull request #1608 from flarum/fl/1602-extend-frontend-document

New extender for adding variables to HtmlDocument payload
This commit is contained in:
Franz Liedke 2018-10-21 22:34:22 +02:00 committed by GitHub
commit 21b3737dc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 44 additions and 10 deletions

View File

@ -52,7 +52,7 @@ class AdminPayload implements ContentInterface
$this->events = $events;
}
public function populate(HtmlDocument $document, Request $request)
public function __invoke(HtmlDocument $document, Request $request)
{
$settings = $this->settings->all();

View File

@ -14,6 +14,7 @@ namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Flarum\Frontend\Asset\ExtensionAssets;
use Flarum\Frontend\CompilerFactory;
use Flarum\Frontend\HtmlDocumentFactory;
use Flarum\Http\RouteHandlerFactory;
use Illuminate\Contracts\Container\Container;
@ -24,6 +25,7 @@ class Frontend implements ExtenderInterface
protected $css = [];
protected $js;
protected $routes = [];
protected $content = [];
public function __construct($frontend)
{
@ -51,10 +53,22 @@ class Frontend implements ExtenderInterface
return $this;
}
/**
* @param callable|string $callback
* @return $this
*/
public function content($callback)
{
$this->content[] = $callback;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$this->registerAssets($container, $this->getModuleName($extension));
$this->registerRoutes($container);
$this->registerContent($container);
}
private function registerAssets(Container $container, string $moduleName)
@ -92,6 +106,26 @@ class Frontend implements ExtenderInterface
}
}
private function registerContent(Container $container)
{
if (empty($this->content)) {
return;
}
$container->resolving(
"flarum.$this->frontend.frontend",
function (HtmlDocumentFactory $view, Container $container) {
foreach ($this->content as $content) {
if (is_string($content)) {
$content = $container->make($content);
}
$view->add($content);
}
}
);
}
private function getModuleName(?Extension $extension): string
{
return $extension ? $extension->getId() : 'site-custom';

View File

@ -20,7 +20,7 @@ class AssertRegistered implements ContentInterface
{
use AssertPermissionTrait;
public function populate(HtmlDocument $document, Request $request)
public function __invoke(HtmlDocument $document, Request $request)
{
$this->assertRegistered($request->getAttribute('actor'));
}

View File

@ -49,7 +49,7 @@ class Discussion implements ContentInterface
$this->view = $view;
}
public function populate(HtmlDocument $document, Request $request)
public function __invoke(HtmlDocument $document, Request $request)
{
$queryParams = $request->getQueryParams();
$page = max(1, array_get($queryParams, 'page'));

View File

@ -44,7 +44,7 @@ class Index implements ContentInterface
/**
* {@inheritdoc}
*/
public function populate(HtmlDocument $document, Request $request)
public function __invoke(HtmlDocument $document, Request $request)
{
$queryParams = $request->getQueryParams();

View File

@ -20,5 +20,5 @@ interface ContentInterface
* @param HtmlDocument $document
* @param Request $request
*/
public function populate(HtmlDocument $document, Request $request);
public function __invoke(HtmlDocument $document, Request $request);
}

View File

@ -41,7 +41,7 @@ class CorePayload implements ContentInterface
$this->api = $api;
}
public function populate(HtmlDocument $document, Request $request)
public function __invoke(HtmlDocument $document, Request $request)
{
$document->payload = array_merge(
$document->payload,

View File

@ -29,7 +29,7 @@ class Layout implements ContentInterface
$this->layoutView = $layoutView;
}
public function populate(HtmlDocument $document, Request $request)
public function __invoke(HtmlDocument $document, Request $request)
{
$document->layoutView = $this->layoutView;
}

View File

@ -16,7 +16,7 @@ use Psr\Http\Message\ServerRequestInterface as Request;
class Meta implements ContentInterface
{
public function populate(HtmlDocument $document, Request $request)
public function __invoke(HtmlDocument $document, Request $request)
{
$document->meta = array_merge($document->meta, $this->buildMeta($document));
$document->head = array_merge($document->head, $this->buildHead($document));

View File

@ -61,7 +61,7 @@ class HtmlDocumentFactory
}
/**
* @param ContentInterface $content
* @param ContentInterface|callable $content
*/
public function add($content)
{
@ -116,7 +116,7 @@ class HtmlDocumentFactory
protected function populate(HtmlDocument $view, Request $request)
{
foreach ($this->content as $content) {
$content->populate($view, $request);
$content($view, $request);
}
}