From a0741f801b9bddeb4272ebc11f06393eebe5220c Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Mon, 23 Jul 2018 16:24:00 +0200 Subject: [PATCH] Replace Assets with Frontend extender This extender allows registering both assets and simple GET routes with frontend instances. See #851. --- framework/core/src/Extend/Assets.php | 64 ----------------- framework/core/src/Extend/Frontend.php | 95 ++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 64 deletions(-) delete mode 100644 framework/core/src/Extend/Assets.php create mode 100644 framework/core/src/Extend/Frontend.php diff --git a/framework/core/src/Extend/Assets.php b/framework/core/src/Extend/Assets.php deleted file mode 100644 index 1e37665fe..000000000 --- a/framework/core/src/Extend/Assets.php +++ /dev/null @@ -1,64 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Flarum\Extend; - -use Flarum\Extension\Extension; -use Flarum\Frontend\Asset\ExtensionAssets; -use Flarum\Frontend\CompilerFactory; -use Illuminate\Contracts\Container\Container; - -class Assets implements ExtenderInterface -{ - protected $frontend; - - protected $css = []; - protected $js; - - public function __construct($frontend) - { - $this->frontend = $frontend; - } - - public function css($path) - { - $this->css[] = $path; - - return $this; - } - - /** - * @deprecated - */ - public function asset($path) - { - return $this->css($path); - } - - public function js($path) - { - $this->js = $path; - - return $this; - } - - public function __invoke(Container $container, Extension $extension = null) - { - $container->resolving( - "flarum.$this->frontend.assets", - function (CompilerFactory $assets) use ($extension) { - $assets->add(function () use ($extension) { - return new ExtensionAssets($extension, $this->css, $this->js); - }); - } - ); - } -} diff --git a/framework/core/src/Extend/Frontend.php b/framework/core/src/Extend/Frontend.php new file mode 100644 index 000000000..bd3b13128 --- /dev/null +++ b/framework/core/src/Extend/Frontend.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Flarum\Extend; + +use Flarum\Extension\Extension; +use Flarum\Frontend\Asset\ExtensionAssets; +use Flarum\Frontend\CompilerFactory; +use Flarum\Http\RouteCollection; +use Flarum\Http\RouteHandlerFactory; +use Illuminate\Contracts\Container\Container; + +class Frontend implements ExtenderInterface +{ + protected $frontend; + + protected $css = []; + protected $js; + protected $routes = []; + + public function __construct($frontend) + { + $this->frontend = $frontend; + } + + public function css($path) + { + $this->css[] = $path; + + return $this; + } + + public function js($path) + { + $this->js = $path; + + return $this; + } + + public function route($path, $name, $content = null) + { + $this->routes[] = compact('path', 'name', 'content'); + + return $this; + } + + public function __invoke(Container $container, Extension $extension = null) + { + $this->registerAssets($container, $extension); + $this->registerRoutes($container); + } + + private function registerAssets(Container $container, Extension $extension) + { + if (empty($this->css) && empty($this->js)) { + return; + } + + $container->resolving( + "flarum.$this->frontend.assets", + function (CompilerFactory $assets) use ($extension) { + $assets->add(function () use ($extension) { + return new ExtensionAssets( + $extension, $this->css, $this->js + ); + }); + } + ); + } + + private function registerRoutes(Container $container) + { + if (empty($this->routes)) { + return; + } + + $routes = $container->make("flarum.$this->frontend.routes"); + $factory = $container->make(RouteHandlerFactory::class); + + foreach ($this->routes as $route) { + $routes->get( + $route['path'], $route['name'], + $factory->toForum($route['content']) + ); + } + } +}