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

64 lines
1.6 KiB
PHP
Raw Normal View History

2016-10-29 18:54:10 +08:00
<?php
namespace App\Services;
use Event;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use App\Events\ConfigureRoutes;
use App\Events\ConfigureUserMenu;
2016-11-17 17:32:12 +08:00
use App\Events\ConfigureAdminMenu;
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)
{
2016-11-17 17:32:12 +08:00
$class = $category == "user" ? ConfigureUserMenu::class : ConfigureAdminMenu::class;
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)
{
Event::listen(ConfigureRoutes::class, function($event) use ($callback)
{
return call_user_func($callback, $event->router);
});
}
}