mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2024-12-21 06:19:38 +08:00
61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Event;
|
|
use Closure;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
use App\Events\ConfigureRoutes;
|
|
use App\Events\ConfigureUserMenu;
|
|
|
|
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)
|
|
{
|
|
Event::listen(ConfigureUserMenu::class, function ($event) use ($menu, $position)
|
|
{
|
|
$new = [];
|
|
|
|
$offset = 0;
|
|
foreach ($event->menu['user'] as $item) {
|
|
// push new menu items at the given position
|
|
if ($offset == $position) {
|
|
$new[] = $menu;
|
|
}
|
|
|
|
$new[] = $item;
|
|
$offset++;
|
|
}
|
|
|
|
$event->menu['user'] = $new;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Add a route. A router instance will be passed to the given callback.
|
|
*
|
|
* @param Closure $callback
|
|
*/
|
|
public static function addRoute(Closure $callback)
|
|
{
|
|
Event::listen(ConfigureRoutes::class, function($event) use ($callback)
|
|
{
|
|
return call_user_func($callback, $event->router);
|
|
});
|
|
}
|
|
}
|