mirror of
https://github.com/flarum/framework.git
synced 2024-12-01 05:53:45 +08:00
Merge pull request #1608 from flarum/fl/1602-extend-frontend-document
New extender for adding variables to HtmlDocument payload
This commit is contained in:
commit
21b3737dc2
|
@ -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();
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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'));
|
||||
}
|
||||
|
|
|
@ -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'));
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user