diff --git a/framework/core/src/Extend/Assets.php b/framework/core/src/Extend/Assets.php new file mode 100644 index 000000000..6fb52dbd7 --- /dev/null +++ b/framework/core/src/Extend/Assets.php @@ -0,0 +1,81 @@ + + * + * 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; + } + } +}