From d335ce8eef30f27395f5cf25013d64400ef1c851 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 21 Oct 2018 20:41:10 +0200 Subject: [PATCH 1/2] Tweak ContentInterface so that callables can be used as well --- framework/core/src/Admin/Content/AdminPayload.php | 2 +- framework/core/src/Forum/Content/AssertRegistered.php | 2 +- framework/core/src/Forum/Content/Discussion.php | 2 +- framework/core/src/Forum/Content/Index.php | 2 +- framework/core/src/Frontend/Content/ContentInterface.php | 2 +- framework/core/src/Frontend/Content/CorePayload.php | 2 +- framework/core/src/Frontend/Content/Layout.php | 2 +- framework/core/src/Frontend/Content/Meta.php | 2 +- framework/core/src/Frontend/HtmlDocumentFactory.php | 4 ++-- 9 files changed, 10 insertions(+), 10 deletions(-) diff --git a/framework/core/src/Admin/Content/AdminPayload.php b/framework/core/src/Admin/Content/AdminPayload.php index 59fdf45ab..b5b7c3f15 100644 --- a/framework/core/src/Admin/Content/AdminPayload.php +++ b/framework/core/src/Admin/Content/AdminPayload.php @@ -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(); diff --git a/framework/core/src/Forum/Content/AssertRegistered.php b/framework/core/src/Forum/Content/AssertRegistered.php index 8bceb4761..fba9c3639 100644 --- a/framework/core/src/Forum/Content/AssertRegistered.php +++ b/framework/core/src/Forum/Content/AssertRegistered.php @@ -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')); } diff --git a/framework/core/src/Forum/Content/Discussion.php b/framework/core/src/Forum/Content/Discussion.php index 15e8d9bd4..78dc042c2 100644 --- a/framework/core/src/Forum/Content/Discussion.php +++ b/framework/core/src/Forum/Content/Discussion.php @@ -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')); diff --git a/framework/core/src/Forum/Content/Index.php b/framework/core/src/Forum/Content/Index.php index d83de36e6..21db56c60 100644 --- a/framework/core/src/Forum/Content/Index.php +++ b/framework/core/src/Forum/Content/Index.php @@ -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(); diff --git a/framework/core/src/Frontend/Content/ContentInterface.php b/framework/core/src/Frontend/Content/ContentInterface.php index cbf167272..4a4a2f614 100644 --- a/framework/core/src/Frontend/Content/ContentInterface.php +++ b/framework/core/src/Frontend/Content/ContentInterface.php @@ -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); } diff --git a/framework/core/src/Frontend/Content/CorePayload.php b/framework/core/src/Frontend/Content/CorePayload.php index 87a390f6d..51f5b1524 100644 --- a/framework/core/src/Frontend/Content/CorePayload.php +++ b/framework/core/src/Frontend/Content/CorePayload.php @@ -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, diff --git a/framework/core/src/Frontend/Content/Layout.php b/framework/core/src/Frontend/Content/Layout.php index 07896215d..08105ed0f 100644 --- a/framework/core/src/Frontend/Content/Layout.php +++ b/framework/core/src/Frontend/Content/Layout.php @@ -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; } diff --git a/framework/core/src/Frontend/Content/Meta.php b/framework/core/src/Frontend/Content/Meta.php index 6d037c3ae..772ffb191 100644 --- a/framework/core/src/Frontend/Content/Meta.php +++ b/framework/core/src/Frontend/Content/Meta.php @@ -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)); diff --git a/framework/core/src/Frontend/HtmlDocumentFactory.php b/framework/core/src/Frontend/HtmlDocumentFactory.php index a588e1138..846e9e345 100644 --- a/framework/core/src/Frontend/HtmlDocumentFactory.php +++ b/framework/core/src/Frontend/HtmlDocumentFactory.php @@ -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); } } From cbe52ff846d70b97240043f14ef1392bb1d6723d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 21 Oct 2018 00:53:52 +0200 Subject: [PATCH 2/2] New extender for adding variables to HtmlDocument payload Fixes #1602. --- framework/core/src/Extend/Frontend.php | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/framework/core/src/Extend/Frontend.php b/framework/core/src/Extend/Frontend.php index 1b77d541b..3832c71b8 100644 --- a/framework/core/src/Extend/Frontend.php +++ b/framework/core/src/Extend/Frontend.php @@ -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';