diff --git a/src/Extend/Assets.php b/src/Extend/Assets.php deleted file mode 100644 index 1e37665fe..000000000 --- a/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/src/Extend/Frontend.php b/src/Extend/Frontend.php new file mode 100644 index 000000000..bd3b13128 --- /dev/null +++ b/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']) + ); + } + } +}