From cbe52ff846d70b97240043f14ef1392bb1d6723d Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sun, 21 Oct 2018 00:53:52 +0200 Subject: [PATCH] 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';