From dfbbd66a37e32db7a08928021fdf76719cfd4f9a Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 14 Nov 2018 08:16:58 +0100 Subject: [PATCH] WIP: frontend extender --- src/Extend/Frontend.php | 53 ++++++++++++++++++++++++++++++++ src/Frontend/CompilerFactory.php | 8 +++++ 2 files changed, 61 insertions(+) diff --git a/src/Extend/Frontend.php b/src/Extend/Frontend.php index 3832c71b8..d5414ba63 100644 --- a/src/Extend/Frontend.php +++ b/src/Extend/Frontend.php @@ -11,6 +11,7 @@ namespace Flarum\Extend; +use Exception; use Flarum\Extension\Extension; use Flarum\Frontend\Asset\ExtensionAssets; use Flarum\Frontend\CompilerFactory; @@ -21,6 +22,7 @@ use Illuminate\Contracts\Container\Container; class Frontend implements ExtenderInterface { protected $frontend; + protected $inheritFrom; protected $css = []; protected $js; @@ -32,6 +34,13 @@ class Frontend implements ExtenderInterface $this->frontend = $frontend; } + public function inherits($from) + { + $this->inheritFrom = $from; + + return $this; + } + public function css($path) { $this->css[] = $path; @@ -48,11 +57,26 @@ class Frontend implements ExtenderInterface public function route($path, $name, $content = null) { + $this->ensureCanRegisterRoutes(); + $this->routes[] = compact('path', 'name', 'content'); return $this; } + private function ensureCanRegisterRoutes() + { + if (in_array($this->frontend, ['forum', 'admin'])) { + return; + } + + throw new Exception( + 'The Frontend extender can only handle routes for the forum and '. + 'admin frontends. Other routes (e.g. for the API) need to be '. + 'registered through the Routes extender.' + ); + } + /** * @param callable|string $callback * @return $this @@ -66,11 +90,40 @@ class Frontend implements ExtenderInterface public function extend(Container $container, Extension $extension = null) { + $this->registerFrontend($container); $this->registerAssets($container, $this->getModuleName($extension)); $this->registerRoutes($container); $this->registerContent($container); } + private function registerFrontend(Container $container) + { + if ($container->bound("flarum.$this->frontend.frontend")) { + return; + } + + $container->bind( + "flarum.$this->frontend.frontend", + function ($c) { + $view = $c->make('flarum.frontend.view.defaults')($this->frontend); + + $view->setAssets($c->make("flarum.$this->frontend.assets")); + + return $view; + } + ); + + $container->bind("flarum.$this->frontend.assets", function ($c) { + if ($this->inheritFrom) { + // FIXME: will contain Assets\CoreAssets instance with wrong name + return $c->make("flarum.$this->inheritFrom.assets") + ->inherit($this->frontend); + } else { + return $c->make('flarum.frontend.assets.defaults')($this->frontend); + } + }); + } + private function registerAssets(Container $container, string $moduleName) { if (empty($this->css) && empty($this->js)) { diff --git a/src/Frontend/CompilerFactory.php b/src/Frontend/CompilerFactory.php index fc541e5a8..347810d21 100644 --- a/src/Frontend/CompilerFactory.php +++ b/src/Frontend/CompilerFactory.php @@ -67,6 +67,14 @@ class CompilerFactory $this->lessImportDirs = $lessImportDirs; } + public function inherit($childName): self + { + // TODO: Probably some more deep cloning necessary here + $child = clone $this; + $child->name = $childName; + return $child; + } + /** * @param callable $callback */