From adb6aed94aee82716a4d6b1a0cb2de99b8367864 Mon Sep 17 00:00:00 2001 From: printempw Date: Tue, 7 Aug 2018 10:59:15 +0800 Subject: [PATCH] Tweak code style of App\Services\Hook --- app/Services/Hook.php | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/app/Services/Hook.php b/app/Services/Hook.php index fd63c9b3..d398ca83 100644 --- a/app/Services/Hook.php +++ b/app/Services/Hook.php @@ -23,10 +23,9 @@ class Hook */ public static function addMenuItem($category, $position, array $menu) { - $class = $category == "user" ? Events\ConfigureUserMenu::class : Events\ConfigureAdminMenu::class; + $class = $category == 'user' ? Events\ConfigureUserMenu::class : Events\ConfigureAdminMenu::class; - Event::listen($class, function ($event) use ($menu, $position, $category) - { + Event::listen($class, function ($event) use ($menu, $position, $category) { $new = []; $offset = 0; @@ -53,29 +52,25 @@ class Hook */ public static function addRoute(Closure $callback) { - Event::listen(Events\ConfigureRoutes::class, function($event) use ($callback) - { + Event::listen(Events\ConfigureRoutes::class, function ($event) use ($callback) { return call_user_func($callback, $event->router); }); } - public static function registerPluginTransScripts($id) + public static function registerPluginTransScripts($id, $pages = ['*'], $priority = 1) { - Event::listen(Events\RenderingFooter::class, function($event) use ($id) - { - $path = app('plugins')->getPlugin($id)->getPath().'/'; - $script = 'lang/'.config('app.locale').'/locale.js'; + $basepath = plugin($id)->getPath().'/'; + $relative = 'lang/'.config('app.locale').'/locale.js'; - if (file_exists($path.$script)) { - $event->addContent(''); - } - }, 999); + if (file_exists($basepath.$relative)) { + static::addScriptFileToPage(plugin_assets($id, $relative), $pages, $priority); + } } public static function addStyleFileToPage($urls, $pages = ['*'], $priority = 1) { - Event::listen(Events\RenderingHeader::class, function($event) use ($urls, $pages) - { + Event::listen(Events\RenderingHeader::class, function ($event) use ($urls, $pages) { + foreach ($pages as $pattern) { if (! app('request')->is($pattern)) continue; @@ -92,8 +87,8 @@ class Hook public static function addScriptFileToPage($urls, $pages = ['*'], $priority = 1) { - Event::listen(Events\RenderingFooter::class, function($event) use ($urls, $pages) - { + Event::listen(Events\RenderingFooter::class, function ($event) use ($urls, $pages) { + foreach ($pages as $pattern) { if (! app('request')->is($pattern)) continue;