Skip to content

Commit

Permalink
#649 - Specialise isLoaded() method
Browse files Browse the repository at this point in the history
- Retrieve the method name from the callstack if no key provided
- Automatically setLoaded()
  • Loading branch information
johanjanssens committed Apr 15, 2021
1 parent c080e5b commit 10bb50b
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions code/site/components/com_pages/template/helper/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function prefetcher($config = array())
));

$html = '';
if (!static::isLoaded('prefetcher'))
if (!static::isLoaded())
{
$html .= '<ktml:script src="https://files.joomlatools.com/pages@'.$config->version.'/prefetcher.'.(!$config->debug ? 'min.js' : 'js').'" defer="defer" />';
$html .= <<<PREFETCHER
Expand All @@ -32,7 +32,6 @@ public function prefetcher($config = array())
</script>
PREFETCHER;

static::setLoaded('prefetcher');
}

return $html;
Expand All @@ -48,10 +47,8 @@ public function alpine($config = [])

$html = '';

if (!static::isLoaded('alpine'))
{
if (!static::isLoaded()) {
$html .= '<ktml:script src="https://unpkg.com/alpinejs@'.$config->version.'/dist/alpine.js" defer="defer" />';
static::setLoaded('alpine');
}

return $html;
Expand Down Expand Up @@ -82,7 +79,7 @@ public function highlight($config = array())
}

$html = '';
if (!static::isLoaded('highlight'))
if (!static::isLoaded())
{
$style_url = 'https://unpkg.com/@highlightjs/cdn-assets@'.$config->version.'/styles/'.$config->style.'.min.css';
$hljs_url = 'https://unpkg.com/@highlightjs/cdn-assets@'.$config->version.'/highlight.' . (!$config->debug ? 'min.js' : 'js');
Expand Down Expand Up @@ -166,9 +163,23 @@ public function highlight($config = array())
</style>
HIGHLIGHT;
static::setLoaded('highlight');
}

return $html;
}

public static function isLoaded($key = null)
{
if(!$key)
{
$method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,2);
$key = $method[1]['function'];
}

if(!$result = !empty(static::$_loaded[$key])) {
self::setLoaded($key);
}

return $result;
}
}

0 comments on commit 10bb50b

Please sign in to comment.