mirror of
https://github.com/flarum/framework.git
synced 2024-12-02 23:23:52 +08:00
Replace Assets with Frontend extender
This extender allows registering both assets and simple GET routes with frontend instances. See #851.
This commit is contained in:
parent
73da82ebc0
commit
a0741f801b
|
@ -1,64 +0,0 @@
|
|||
<?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 Assets implements ExtenderInterface
|
||||
{
|
||||
protected $frontend;
|
||||
|
||||
protected $css = [];
|
||||
protected $js;
|
||||
|
||||
public function __construct($frontend)
|
||||
{
|
||||
$this->frontend = $frontend;
|
||||
}
|
||||
|
||||
public function css($path)
|
||||
{
|
||||
$this->css[] = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
public function asset($path)
|
||||
{
|
||||
return $this->css($path);
|
||||
}
|
||||
|
||||
public function js($path)
|
||||
{
|
||||
$this->js = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __invoke(Container $container, Extension $extension = null)
|
||||
{
|
||||
$container->resolving(
|
||||
"flarum.$this->frontend.assets",
|
||||
function (CompilerFactory $assets) use ($extension) {
|
||||
$assets->add(function () use ($extension) {
|
||||
return new ExtensionAssets($extension, $this->css, $this->js);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
95
framework/core/src/Extend/Frontend.php
Normal file
95
framework/core/src/Extend/Frontend.php
Normal file
|
@ -0,0 +1,95 @@
|
|||
<?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 Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\RouteHandlerFactory;
|
||||
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;
|
||||
}
|
||||
|
||||
public function route($path, $name, $content = null)
|
||||
{
|
||||
$this->routes[] = compact('path', 'name', 'content');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __invoke(Container $container, Extension $extension = null)
|
||||
{
|
||||
$this->registerAssets($container, $extension);
|
||||
$this->registerRoutes($container);
|
||||
}
|
||||
|
||||
private function registerAssets(Container $container, Extension $extension)
|
||||
{
|
||||
if (empty($this->css) && empty($this->js)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$container->resolving(
|
||||
"flarum.$this->frontend.assets",
|
||||
function (CompilerFactory $assets) use ($extension) {
|
||||
$assets->add(function () use ($extension) {
|
||||
return new ExtensionAssets(
|
||||
$extension, $this->css, $this->js
|
||||
);
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
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->toForum($route['content'])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user