add useful functions for plugins

This commit is contained in:
printempw 2017-01-08 16:05:54 +08:00
parent f6aadc71f0
commit 954329a8e7
3 changed files with 42 additions and 17 deletions

View File

@ -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("<link rel=\"stylesheet\" href=\"$url\">");
foreach ($pages as $pattern) {
if (!app('request')->is($pattern))
continue;
foreach ((array) $urls as $url) {
$event->addContent("<link rel=\"stylesheet\" href=\"$url\">");
}
}
}, $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("<script src=\"$url\"></script>");
foreach ($pages as $pattern) {
if (!app('request')->is($pattern))
continue;
foreach ((array) $urls as $url) {
$event->addContent("<script src=\"$url\"></script>");
}
}
}, $priority);
}
}

View File

@ -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

View File

@ -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.");
}