mirror of
https://github.com/flarum/core.git
synced 2025-08-07 08:56:38 +02:00
WIP: frontend extender
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Flarum\Extend;
|
namespace Flarum\Extend;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Flarum\Extension\Extension;
|
use Flarum\Extension\Extension;
|
||||||
use Flarum\Frontend\Asset\ExtensionAssets;
|
use Flarum\Frontend\Asset\ExtensionAssets;
|
||||||
use Flarum\Frontend\CompilerFactory;
|
use Flarum\Frontend\CompilerFactory;
|
||||||
@@ -21,6 +22,7 @@ use Illuminate\Contracts\Container\Container;
|
|||||||
class Frontend implements ExtenderInterface
|
class Frontend implements ExtenderInterface
|
||||||
{
|
{
|
||||||
protected $frontend;
|
protected $frontend;
|
||||||
|
protected $inheritFrom;
|
||||||
|
|
||||||
protected $css = [];
|
protected $css = [];
|
||||||
protected $js;
|
protected $js;
|
||||||
@@ -32,6 +34,13 @@ class Frontend implements ExtenderInterface
|
|||||||
$this->frontend = $frontend;
|
$this->frontend = $frontend;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function inherits($from)
|
||||||
|
{
|
||||||
|
$this->inheritFrom = $from;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function css($path)
|
public function css($path)
|
||||||
{
|
{
|
||||||
$this->css[] = $path;
|
$this->css[] = $path;
|
||||||
@@ -48,11 +57,26 @@ class Frontend implements ExtenderInterface
|
|||||||
|
|
||||||
public function route($path, $name, $content = null)
|
public function route($path, $name, $content = null)
|
||||||
{
|
{
|
||||||
|
$this->ensureCanRegisterRoutes();
|
||||||
|
|
||||||
$this->routes[] = compact('path', 'name', 'content');
|
$this->routes[] = compact('path', 'name', 'content');
|
||||||
|
|
||||||
return $this;
|
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
|
* @param callable|string $callback
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -66,11 +90,40 @@ class Frontend implements ExtenderInterface
|
|||||||
|
|
||||||
public function extend(Container $container, Extension $extension = null)
|
public function extend(Container $container, Extension $extension = null)
|
||||||
{
|
{
|
||||||
|
$this->registerFrontend($container);
|
||||||
$this->registerAssets($container, $this->getModuleName($extension));
|
$this->registerAssets($container, $this->getModuleName($extension));
|
||||||
$this->registerRoutes($container);
|
$this->registerRoutes($container);
|
||||||
$this->registerContent($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)
|
private function registerAssets(Container $container, string $moduleName)
|
||||||
{
|
{
|
||||||
if (empty($this->css) && empty($this->js)) {
|
if (empty($this->css) && empty($this->js)) {
|
||||||
|
@@ -67,6 +67,14 @@ class CompilerFactory
|
|||||||
$this->lessImportDirs = $lessImportDirs;
|
$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
|
* @param callable $callback
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user