blessing-skin-server/app/Services/Hook.php

109 lines
3.0 KiB
PHP
Raw Normal View History

2016-10-29 18:54:10 +08:00
<?php
namespace App\Services;
use Event;
use Closure;
2017-01-02 14:37:16 +08:00
use App\Events;
2016-10-29 18:54:10 +08:00
class Hook
{
/**
* Add an item to menu.
*
* @param string $category 'user' or 'admin'
* @param int $position Where to insert the given item, start from 0.
* @param array $menu e.g.
* [
* 'title' => 'Title', # will be translated by translator
* 'link' => 'user/config', # route link
* 'icon' => 'fa-book' # font-awesome icon
* ]
* @return void
*/
public static function addMenuItem($category, $position, array $menu)
{
2017-01-02 14:37:16 +08:00
$class = $category == "user" ? Events\ConfigureUserMenu::class : Events\ConfigureAdminMenu::class;
2016-11-17 17:32:12 +08:00
Event::listen($class, function ($event) use ($menu, $position, $category)
2016-10-29 18:54:10 +08:00
{
$new = [];
$offset = 0;
2016-11-17 17:32:12 +08:00
foreach ($event->menu[$category] as $item) {
2016-10-29 18:54:10 +08:00
// push new menu items at the given position
if ($offset == $position) {
$new[] = $menu;
}
$new[] = $item;
$offset++;
}
2016-11-17 17:32:12 +08:00
$event->menu[$category] = $new;
2016-10-29 18:54:10 +08:00
});
}
/**
* Add a route. A router instance will be passed to the given callback.
*
* @param Closure $callback
*/
public static function addRoute(Closure $callback)
{
2017-01-02 14:37:16 +08:00
Event::listen(Events\ConfigureRoutes::class, function($event) use ($callback)
2016-10-29 18:54:10 +08:00
{
return call_user_func($callback, $event->router);
});
}
2017-01-02 14:37:16 +08:00
public static function registerPluginTransScripts($id)
{
Event::listen(Events\RenderingFooter::class, function($event) use ($id)
{
$path = app('plugins')->getPlugin($id)->getPath().'/';
$script = 'lang/'.config('app.locale').'/locale.js';
if (file_exists($path.$script)) {
$event->addContent('<script src="'.plugin_assets($id, $script).'"></script>');
}
}, 999);
}
2017-01-08 16:05:54 +08:00
public static function addStyleFileToPage($urls, $pages = ['*'], $priority = 1)
2017-01-02 14:37:16 +08:00
{
2017-01-08 16:05:54 +08:00
Event::listen(Events\RenderingHeader::class, function($event) use ($urls, $pages)
2017-01-02 14:37:16 +08:00
{
2017-01-08 16:05:54 +08:00
foreach ($pages as $pattern) {
if (!app('request')->is($pattern))
continue;
foreach ((array) $urls as $url) {
$event->addContent("<link rel=\"stylesheet\" href=\"$url\">");
}
return;
2017-01-02 14:37:16 +08:00
}
2017-01-08 16:05:54 +08:00
2017-01-02 14:37:16 +08:00
}, $priority);
}
2017-01-08 16:05:54 +08:00
public static function addScriptFileToPage($urls, $pages = ['*'], $priority = 1)
2017-01-02 14:37:16 +08:00
{
2017-01-08 16:05:54 +08:00
Event::listen(Events\RenderingFooter::class, function($event) use ($urls, $pages)
2017-01-02 14:37:16 +08:00
{
2017-01-08 16:05:54 +08:00
foreach ($pages as $pattern) {
if (!app('request')->is($pattern))
continue;
foreach ((array) $urls as $url) {
$event->addContent("<script src=\"$url\"></script>");
}
return;
2017-01-02 14:37:16 +08:00
}
2017-01-08 16:05:54 +08:00
2017-01-02 14:37:16 +08:00
}, $priority);
}
2016-10-29 18:54:10 +08:00
}