Skip to content

Commit

Permalink
New extender for adding variables to HtmlDocument payload
Browse files Browse the repository at this point in the history
Fixes #1602.
  • Loading branch information
franzliedke committed Oct 20, 2018
1 parent c23af95 commit f018574
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/Extend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Flarum\Extend;

use Closure;
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;

Expand All @@ -24,6 +26,7 @@ class Frontend implements ExtenderInterface
protected $css = [];
protected $js;
protected $routes = [];
protected $documentCallback;

public function __construct($frontend)
{
Expand Down Expand Up @@ -51,10 +54,22 @@ public function route($path, $name, $content = null)
return $this;
}

/**
* @param callable|string $callback
* @return $this
*/
public function document($callback)
{
$this->documentCallback = $callback;

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
$this->registerAssets($container, $this->getModuleName($extension));
$this->registerRoutes($container);
$this->registerDocumentCallback($container);
}

private function registerAssets(Container $container, string $moduleName)
Expand Down Expand Up @@ -92,6 +107,26 @@ private function registerRoutes(Container $container)
}
}

private function registerDocumentCallback(Container $container)
{
if (! isset($this->documentCallback)) {
return;
}

$container->resolving(
"flarum.$this->frontend.frontend",
function (HtmlDocumentFactory $view, Container $container) {
if ($this->documentCallback instanceof Closure) {
$callback = $this->documentCallback;
} else {
$callback = $container->make($this->documentCallback);
}

$view->addCallback($callback);
}
);
}

private function getModuleName(?Extension $extension): string
{
return $extension ? $extension->getId() : 'site-custom';
Expand Down
24 changes: 24 additions & 0 deletions src/Frontend/HtmlDocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class HtmlDocumentFactory
*/
protected $content = [];

/**
* @var callable[]
*/
protected $callbacks = [];

/**
* @param Factory $view
* @param Client $api
Expand All @@ -68,6 +73,14 @@ public function add($content)
$this->content[] = $content;
}

/**
* @param callable $callback
*/
public function addCallback(callable $callback)
{
$this->callbacks[] = $callback;
}

/**
* @param Request $request
* @return HtmlDocument
Expand All @@ -89,6 +102,7 @@ public function make(Request $request): HtmlDocument
$view->css = array_merge($view->css, $this->getUrls($css));

$this->populate($view, $request);
$this->runCallbacks($view);

return $view;
}
Expand Down Expand Up @@ -120,6 +134,16 @@ protected function populate(HtmlDocument $view, Request $request)
}
}

/**
* @param HtmlDocument $view
*/
protected function runCallbacks(HtmlDocument $view)
{
foreach ($this->callbacks as $callback) {
$callback($view);
}
}

/**
* @param Request $request
* @return array
Expand Down

0 comments on commit f018574

Please sign in to comment.