Add Assets extender for frontend extensions

This commit is contained in:
Franz Liedke 2018-01-02 22:50:39 +01:00
parent 5aead00730
commit 95b80aecfe

View File

@ -0,0 +1,81 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Frontend\Event\Rendering;
use Illuminate\Contracts\Container\Container;
use Illuminate\Events\Dispatcher;
class Assets implements Extender
{
protected $appName;
protected $assets = [];
protected $bootstrapper;
public function __construct($appName)
{
$this->appName = $appName;
}
public function defaultAssets($baseDir)
{
$this->asset("$baseDir/js/{$this->appName}/dist/extension.js");
$this->asset("$baseDir/less/{$this->appName}/extension.less");
return $this;
}
public function asset($path)
{
$this->assets[] = $path;
return $this;
}
public function bootstrapper($name)
{
$this->bootstrapper = $name;
return $this;
}
public function apply(Container $container)
{
$container->make(Dispatcher::class)->listen(
Rendering::class,
function (Rendering $event) {
if (! $this->matches($event)) {
return;
}
$event->addAssets($this->assets);
if ($this->bootstrapper) {
$event->addBootstrapper($this->bootstrapper);
}
}
);
}
private function matches(Rendering $event)
{
switch ($this->appName) {
case 'admin':
return $event->isAdmin();
case 'forum':
return $event->isForum();
default:
return false;
}
}
}