Hook.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\common\services;
  3. use Event;
  4. use Closure;
  5. use app\common\events;
  6. use Illuminate\Support\Str;
  7. class Hook
  8. {
  9. /**
  10. * Add an item to menu.
  11. *
  12. * @param string $category 'member' or 'admin'
  13. * @param int $position Where to insert the given item, start from 0.
  14. * @param array $menu e.g.
  15. * [
  16. * 'title' => 'Title', # will be translated by translator
  17. * 'link' => 'user/config', # route link
  18. * 'icon' => 'fa-book' # font-awesome icon
  19. * ]
  20. * @return void
  21. */
  22. public static function addMenuItem($category, $position, array $menu)
  23. {
  24. $class = $category == "member" ? events\ConfigureMemberMenu::class : events\ConfigureAdminMenu::class;
  25. Event::listen($class, function ($event) use ($menu, $position, $category)
  26. {
  27. $new = [];
  28. $offset = 0;
  29. foreach ($event->menu[$category] as $item) {
  30. // push new menu items at the given position
  31. if ($offset == $position) {
  32. $new[] = $menu;
  33. }
  34. $new[] = $item;
  35. $offset++;
  36. }
  37. $event->menu[$category] = $new;
  38. });
  39. }
  40. /**
  41. * Add a route. A router instance will be passed to the given callback.
  42. *
  43. * @param Closure $callback
  44. */
  45. public static function addRoute(Closure $callback)
  46. {
  47. Event::listen(events\ConfigureRoutes::class, function($event) use ($callback)
  48. {
  49. return call_user_func($callback, $event->router);
  50. });
  51. }
  52. public static function registerPluginTransScripts($id)
  53. {
  54. Event::listen(events\RenderingFooter::class, function($event) use ($id)
  55. {
  56. $path = app('plugins')->getPlugin($id)->getPath().'/';
  57. $script = 'lang/'.config('app.locale').'/locale.js';
  58. if (file_exists($path.$script)) {
  59. $event->addContent('<script src="'.plugin_assets($id, $script).'"></script>');
  60. }
  61. }, 999);
  62. }
  63. public static function addStyleFileToPage($urls, $pages = ['*'], $priority = 1)
  64. {
  65. Event::listen(events\RenderingHeader::class, function($event) use ($urls, $pages)
  66. {
  67. foreach ($pages as $pattern) {
  68. if (!Str::is($pattern,request()->getRequestUri()))
  69. continue;
  70. foreach ((array) $urls as $url) {
  71. $event->addContent("<link rel=\"stylesheet\" href=\"$url\">");
  72. }
  73. return;
  74. }
  75. }, $priority);
  76. }
  77. public static function addScriptFileToPage($urls, $pages = ['*'], $priority = 1)
  78. {
  79. Event::listen(events\RenderingFooter::class, function($event) use ($urls, $pages)
  80. {
  81. foreach ($pages as $pattern) {
  82. if (!Str::is($pattern,request()->getRequestUri()))
  83. continue;
  84. foreach ((array) $urls as $url) {
  85. $event->addContent("<script src=\"$url\"></script>");
  86. }
  87. return;
  88. }
  89. }, $priority);
  90. }
  91. }