2018-07-23 16:24:00 +02:00
|
|
|
<?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\Extension\Extension;
|
|
|
|
use Flarum\Frontend\Asset\ExtensionAssets;
|
|
|
|
use Flarum\Frontend\CompilerFactory;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-23 16:25:12 +02:00
|
|
|
public function __invoke(Container $container, Extension $extension = null)
|
|
|
|
{
|
2018-09-01 16:15:02 +02:00
|
|
|
$this->registerAssets($container, $this->getModuleName($extension));
|
2018-07-23 16:25:12 +02:00
|
|
|
}
|
2018-07-23 16:24:00 +02:00
|
|
|
|
2018-09-01 16:15:02 +02:00
|
|
|
private function registerAssets(Container $container, string $moduleName)
|
2018-07-23 16:25:12 +02:00
|
|
|
{
|
|
|
|
if (empty($this->css) && empty($this->js)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$container->resolving(
|
|
|
|
"flarum.$this->frontend.assets",
|
2018-09-01 16:15:02 +02:00
|
|
|
function (CompilerFactory $assets) use ($moduleName) {
|
|
|
|
$assets->add(function () use ($moduleName) {
|
2018-07-23 16:25:12 +02:00
|
|
|
return new ExtensionAssets(
|
2018-09-01 16:15:02 +02:00
|
|
|
$moduleName, $this->css, $this->js
|
2018-07-23 16:25:12 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-01 16:15:02 +02:00
|
|
|
private function getModuleName(?Extension $extension): string
|
|
|
|
{
|
|
|
|
return $extension ? $extension->getId() : 'site-custom';
|
|
|
|
}
|
2018-07-23 16:24:00 +02:00
|
|
|
}
|