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;
|
2018-10-21 00:53:52 +02:00
|
|
|
use Flarum\Frontend\HtmlDocumentFactory;
|
2018-09-21 09:05:45 +09:30
|
|
|
use Flarum\Http\RouteHandlerFactory;
|
2018-07-23 16:24:00 +02:00
|
|
|
use Illuminate\Contracts\Container\Container;
|
|
|
|
|
|
|
|
class Frontend implements ExtenderInterface
|
|
|
|
{
|
|
|
|
protected $frontend;
|
|
|
|
|
|
|
|
protected $css = [];
|
|
|
|
protected $js;
|
|
|
|
protected $routes = [];
|
2018-10-21 00:53:52 +02:00
|
|
|
protected $content = [];
|
2018-07-23 16:24:00 +02:00
|
|
|
|
|
|
|
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-09-21 09:05:45 +09:30
|
|
|
public function route($path, $name, $content = null)
|
|
|
|
{
|
|
|
|
$this->routes[] = compact('path', 'name', 'content');
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-10-21 00:53:52 +02:00
|
|
|
/**
|
|
|
|
* @param callable|string $callback
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function content($callback)
|
|
|
|
{
|
|
|
|
$this->content[] = $callback;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-09-26 22:36:36 +02:00
|
|
|
public function extend(Container $container, Extension $extension = null)
|
2018-07-23 16:25:12 +02:00
|
|
|
{
|
2018-09-01 16:15:02 +02:00
|
|
|
$this->registerAssets($container, $this->getModuleName($extension));
|
2018-09-21 09:05:45 +09:30
|
|
|
$this->registerRoutes($container);
|
2018-10-21 00:53:52 +02:00
|
|
|
$this->registerContent($container);
|
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-21 09:05:45 +09:30
|
|
|
private function registerRoutes(Container $container)
|
|
|
|
{
|
|
|
|
if (empty($this->routes)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$routes = $container->make("flarum.$this->frontend.routes");
|
|
|
|
$factory = $container->make(RouteHandlerFactory::class);
|
|
|
|
|
|
|
|
foreach ($this->routes as $route) {
|
|
|
|
$routes->get(
|
|
|
|
$route['path'], $route['name'],
|
|
|
|
$factory->toFrontend($this->frontend, $route['content'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 00:53:52 +02:00
|
|
|
private function registerContent(Container $container)
|
|
|
|
{
|
|
|
|
if (empty($this->content)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$container->resolving(
|
|
|
|
"flarum.$this->frontend.frontend",
|
|
|
|
function (HtmlDocumentFactory $view, Container $container) {
|
|
|
|
foreach ($this->content as $content) {
|
|
|
|
if (is_string($content)) {
|
|
|
|
$content = $container->make($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
$view->add($content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|