diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index 4028fa7db..3c565d026 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -194,7 +194,7 @@ class Assets * * @return $this */ - public function add($asset, $priority = 10, $pipeline = true) + public function add($asset, $priority = null, $pipeline = null) { // More than one asset if (is_array($asset)) { @@ -233,7 +233,7 @@ class Assets * * @return $this */ - public function addCss($asset, $priority = 10, $pipeline = true) + public function addCss($asset, $priority = null, $pipeline = null) { if (is_array($asset)) { foreach ($asset as $a) { @@ -249,14 +249,25 @@ class Assets $asset = $this->buildLocalLink($asset); } + $data = [ + 'asset' => $asset, + 'priority' => intval($priority ?: 10), + 'order' => count($this->css), + 'pipeline' => $pipeline ?: true + ]; + + // check for dynamic array and merge with defaults + $count_args = func_num_args(); + if (func_num_args() == 2) { + $dynamic_arg = func_get_arg(1); + if (is_array($dynamic_arg)) { + $data = array_merge($data, $dynamic_arg); + } + } + $key = md5($asset); if ($asset) { - $this->css[$key] = [ - 'asset' => $asset, - 'priority' => $priority, - 'order' => count($this->css), - 'pipeline' => $pipeline - ]; + $this->css[$key] = $data; } return $this; @@ -269,21 +280,21 @@ class Assets * You may add more than one asset passing an array as argument. * * @param mixed $asset - * @param int $priority the priority, bigger comes first - * @param bool $pipeline false if this should not be pipelined - * @param string $loading how the asset is loaded (async/defer) - * + * @param int $priority the priority, bigger comes first + * @param bool $pipeline false if this should not be pipelined + * @param string $loading how the asset is loaded (async/defer) + * @param string $group name of the group * @return $this */ - public function addJs($asset, $priority = 10, $pipeline = true, $loading = '') + public function addJs($asset, $priority = null, $pipeline = null, $loading = null, $group = null) { if (is_array($asset)) { foreach ($asset as $a) { - $this->addJs($a, $priority, $pipeline); + $this->addJs($a, $priority, $pipeline, $loading, $group); } return $this; } elseif (isset($this->collections[$asset])) { - $this->add($this->collections[$asset], $priority, $pipeline); + $this->add($this->collections[$asset], $priority, $pipeline, $loading, $group); return $this; } @@ -291,15 +302,27 @@ class Assets $asset = $this->buildLocalLink($asset); } + $data = [ + 'asset' => $asset, + 'priority' => intval($priority ?: 10), + 'order' => count($this->js), + 'pipeline' => $pipeline ?: true, + 'loading' => $loading ?: '', + 'group' => $group ?: 'head' + ]; + + // check for dynamic array and merge with defaults + $count_args = func_num_args(); + if (func_num_args() == 2) { + $dynamic_arg = func_get_arg(1); + if (is_array($dynamic_arg)) { + $data = array_merge($data, $dynamic_arg); + } + } + $key = md5($asset); if ($asset) { - $this->js[$key] = [ - 'asset' => $asset, - 'priority' => $priority, - 'order' => count($this->js), - 'pipeline' => $pipeline, - 'loading' => $loading - ]; + $this->js[$key] = $data; } return $this; @@ -311,12 +334,15 @@ class Assets * @param $asset * @param int $priority * @param bool $pipeline + * @param string $group name of the group + * + * @deprecated Please use dynamic method with ['loading' => 'async'] * * @return \Grav\Common\Assets */ - public function addAsyncJs($asset, $priority = 10, $pipeline = true) + public function addAsyncJs($asset, $priority = null, $pipeline = null, $group = null) { - return $this->addJs($asset, $priority, $pipeline, 'async'); + return $this->addJs($asset, $priority, $pipeline, 'async', $group); } /** @@ -325,12 +351,15 @@ class Assets * @param $asset * @param int $priority * @param bool $pipeline + * @param string $group name of the group + * + * @deprecated Please use dynamic method with ['loading' => 'defer'] * * @return \Grav\Common\Assets */ - public function addDeferJs($asset, $priority = 10, $pipeline = true) + public function addDeferJs($asset, $priority = null, $pipeline = null, $group = null) { - return $this->addJs($asset, $priority, $pipeline, 'defer'); + return $this->addJs($asset, $priority, $pipeline, 'defer', $group); } /** @@ -344,18 +373,30 @@ class Assets * * @return $this */ - public function addInlineCss($asset, $priority = 10) + public function addInlineCss($asset, $priority = null) { if (is_a($asset, 'Twig_Markup')) { $asset = strip_tags((string)$asset); } + + $data = [ + 'priority' => intval($priority ?: 10), + 'order' => count($this->inline_css), + 'asset' => $asset + ]; + + // check for dynamic array and merge with defaults + $count_args = func_num_args(); + if (func_num_args() == 2) { + $dynamic_arg = func_get_arg(1); + if (is_array($dynamic_arg)) { + $data = array_merge($data, $dynamic_arg); + } + } + $key = md5($asset); if (is_string($asset) && !array_key_exists($key, $this->inline_css)) { - $this->inline_css[$key] = [ - 'priority' => $priority, - 'order' => count($this->inline_css), - 'asset' => $asset - ]; + $this->inline_css[$key] = $data; } return $this; @@ -369,21 +410,35 @@ class Assets * * @param mixed $asset * @param int $priority the priority, bigger comes first + * @param string $group name of the group * * @return $this */ - public function addInlineJs($asset, $priority = 10) + public function addInlineJs($asset, $priority = null, $group = null) { if (is_a($asset, 'Twig_Markup')) { $asset = strip_tags((string)$asset); } + + $data = [ + 'asset' => $asset, + 'priority' => intval($priority ?: 10), + 'order' => count($this->js), + 'group' => $group ?: 'head' + ]; + + // check for dynamic array and merge with defaults + $count_args = func_num_args(); + if (func_num_args() == 2) { + $dynamic_arg = func_get_arg(1); + if (is_array($dynamic_arg)) { + $data = array_merge($data, $dynamic_arg); + } + } + $key = md5($asset); if (is_string($asset) && !array_key_exists($key, $this->inline_js)) { - $this->inline_js[$key] = [ - 'priority' => $priority, - 'order' => count($this->inline_js), - 'asset' => $asset - ]; + $this->inline_js[$key] = $data; } return $this; @@ -452,11 +507,12 @@ class Assets /** * Build the JavaScript script tags. * + * @param string $group name of the group * @param array $attributes * * @return string */ - public function js($attributes = []) + public function js($group = 'head', $attributes = []) { if (!$this->js) { return null; @@ -482,25 +538,34 @@ class Assets $attributes = $this->attributes(array_merge(['type' => 'text/javascript'], $attributes)); + $output = ''; + $inline_js = ''; + if ($this->js_pipeline) { $output .= '' . "\n"; foreach ($this->js_no_pipeline as $file) { - $output .= '' . "\n"; + if ($group && $file['group'] == $group) { + $output .= '' . "\n"; + } } } else { foreach ($this->js as $file) { - $output .= '' . "\n"; + if ($group && $file['group'] == $group) { + $output .= '' . "\n"; + } } } // Render Inline JS - if (count($this->inline_js) > 0) { - $output .= "\n"; + } + + if ($inline_js) { + $output .= "\n\n"; } return $output; diff --git a/system/src/Grav/Common/Debugger.php b/system/src/Grav/Common/Debugger.php index 2484b31ac..a996a7d5f 100644 --- a/system/src/Grav/Common/Debugger.php +++ b/system/src/Grav/Common/Debugger.php @@ -94,10 +94,10 @@ class Debugger return $this; } - public function startTimer($name, $desription = null) + public function startTimer($name, $description = null) { if ($name[0] == '_' || $this->grav['config']->get('system.debugger.enabled')) { - $this->debugbar['time']->startMeasure($name, $desription); + $this->debugbar['time']->startMeasure($name, $description); } return $this; }