From 954329a8e756a36f15eb4a5a0583c510ab715c8a Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 8 Jan 2017 16:05:54 +0800 Subject: [PATCH] add useful functions for plugins --- app/Services/Hook.php | 28 ++++++++++++++++++++-------- app/Services/Plugin.php | 5 +++++ app/helpers.php | 26 +++++++++++++++++--------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/app/Services/Hook.php b/app/Services/Hook.php index 316f3b6c..8d502633 100644 --- a/app/Services/Hook.php +++ b/app/Services/Hook.php @@ -72,23 +72,35 @@ class Hook }, 999); } - public static function addStyleFileToPage($urls, $priority = 1) + public static function addStyleFileToPage($urls, $pages = ['*'], $priority = 1) { - Event::listen(Events\RenderingHeader::class, function($event) use ($urls) + Event::listen(Events\RenderingHeader::class, function($event) use ($urls, $pages) { - foreach ((array) $urls as $url) { - $event->addContent(""); + foreach ($pages as $pattern) { + if (!app('request')->is($pattern)) + continue; + + foreach ((array) $urls as $url) { + $event->addContent(""); + } } + }, $priority); } - public static function addScriptFileToPage($urls, $priority = 1) + public static function addScriptFileToPage($urls, $pages = ['*'], $priority = 1) { - Event::listen(Events\RenderingFooter::class, function($event) use ($urls) + Event::listen(Events\RenderingFooter::class, function($event) use ($urls, $pages) { - foreach ((array) $urls as $url) { - $event->addContent(""); + foreach ($pages as $pattern) { + if (!app('request')->is($pattern)) + continue; + + foreach ((array) $urls as $url) { + $event->addContent(""); + } } + }, $priority); } } diff --git a/app/Services/Plugin.php b/app/Services/Plugin.php index 0687efab..6a1b4be8 100644 --- a/app/Services/Plugin.php +++ b/app/Services/Plugin.php @@ -103,6 +103,11 @@ class Plugin implements Arrayable, ArrayAccess return Arr::get($this->packageInfo, $name); } + public function assets($relativeUri) + { + return url("plugins/{$this->getDirname()}/$relativeUri"); + } + /** * @param bool $installed * @return Plugin diff --git a/app/helpers.php b/app/helpers.php index ec762db2..dd4dfe05 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -36,25 +36,33 @@ if (! function_exists('avatar')) { if (! function_exists('assets')) { - function assets($relative_uri) + function assets($relativeUri) { // add query string to fresh cache - if (Str::startsWith($relative_uri, 'css') || Str::startsWith($relative_uri, 'js')) { - return url("resources/assets/dist/$relative_uri")."?v=".config('app.version'); - } elseif (Str::startsWith($relative_uri, 'lang')) { - return url("resources/$relative_uri"); + if (Str::startsWith($relativeUri, 'css') || Str::startsWith($relativeUri, 'js')) { + return url("resources/assets/dist/$relativeUri")."?v=".config('app.version'); + } elseif (Str::startsWith($relativeUri, 'lang')) { + return url("resources/$relativeUri"); } else { - return url("resources/assets/$relative_uri"); + return url("resources/assets/$relativeUri"); } } } +if (! function_exists('plugin')) { + + function plugin($id) + { + return app('plugins')->getPlugin($id); + } +} + if (! function_exists('plugin_assets')) { - function plugin_assets($id, $relative_uri) + function plugin_assets($id, $relativeUri) { - if ($plugin = app('plugins')->getPlugin($id)) { - return url("plugins/{$plugin->getDirname()}/$relative_uri"); + if ($plugin = plugin($id)) { + return $plugin->assets($relativeUri); } else { throw new InvalidArgumentException("No such plugin."); }