Frontend extender: Work without extension, too

This commit is contained in:
Franz Liedke 2018-09-01 16:15:02 +02:00
parent a49f2204ab
commit e8c779fcf4
2 changed files with 16 additions and 14 deletions

View File

@ -53,11 +53,11 @@ class Frontend implements ExtenderInterface
public function __invoke(Container $container, Extension $extension = null) public function __invoke(Container $container, Extension $extension = null)
{ {
$this->registerAssets($container, $extension); $this->registerAssets($container, $this->getModuleName($extension));
$this->registerRoutes($container); $this->registerRoutes($container);
} }
private function registerAssets(Container $container, Extension $extension) private function registerAssets(Container $container, string $moduleName)
{ {
if (empty($this->css) && empty($this->js)) { if (empty($this->css) && empty($this->js)) {
return; return;
@ -65,10 +65,10 @@ class Frontend implements ExtenderInterface
$container->resolving( $container->resolving(
"flarum.$this->frontend.assets", "flarum.$this->frontend.assets",
function (CompilerFactory $assets) use ($extension) { function (CompilerFactory $assets) use ($moduleName) {
$assets->add(function () use ($extension) { $assets->add(function () use ($moduleName) {
return new ExtensionAssets( return new ExtensionAssets(
$extension, $this->css, $this->js $moduleName, $this->css, $this->js
); );
}); });
} }
@ -91,4 +91,9 @@ class Frontend implements ExtenderInterface
); );
} }
} }
private function getModuleName(?Extension $extension): string
{
return $extension ? $extension->getId() : 'site-custom';
}
} }

View File

@ -11,15 +11,14 @@
namespace Flarum\Frontend\Asset; namespace Flarum\Frontend\Asset;
use Flarum\Extension\Extension;
use Flarum\Frontend\Compiler\Source\SourceCollector; use Flarum\Frontend\Compiler\Source\SourceCollector;
class ExtensionAssets implements AssetInterface class ExtensionAssets implements AssetInterface
{ {
/** /**
* @var Extension * @var string
*/ */
protected $extension; protected $moduleName;
/** /**
* @var array * @var array
@ -32,13 +31,13 @@ class ExtensionAssets implements AssetInterface
protected $js; protected $js;
/** /**
* @param Extension $extension * @param string $moduleName
* @param array $css * @param array $css
* @param string|callable|null $js * @param string|callable|null $js
*/ */
public function __construct(Extension $extension, array $css, $js = null) public function __construct(string $moduleName, array $css, $js = null)
{ {
$this->extension = $extension; $this->moduleName = $moduleName;
$this->css = $css; $this->css = $css;
$this->js = $js; $this->js = $js;
} }
@ -57,9 +56,7 @@ class ExtensionAssets implements AssetInterface
} }
$sources->addString(function () { $sources->addString(function () {
$name = $this->extension->getId(); return "flarum.extensions['$this->moduleName']=module.exports";
return "flarum.extensions['$name']=module.exports";
}); });
} }
} }