From 8ff1042fe327be901b4529a13ecc866f3101db8f Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 14 Sep 2017 15:20:24 -0600 Subject: [PATCH] Refactored asset sorting logic --- CHANGELOG.md | 2 ++ system/src/Grav/Common/Assets.php | 47 +++++++------------------------ 2 files changed, 12 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f601937cc..4effc9695 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # v1.3.4 ## xx/xx/2017 +1. [](#improved) + * Refactored the Assets sorting logic 1. [](#bugfix) * Fixed `Page::summary()` when using delimiter and multibyte UTF8 Characters [#1644](https://github.com/getgrav/grav/issues/1644) diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index a329cd618..bc3f19416 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -508,24 +508,9 @@ class Assets // Sort array by priorities (larger priority first) if (Grav::instance()) { - uasort($this->css, function ($a, $b) { - if ($a['priority'] == $b['priority']) { - return $b['order'] - $a['order']; - } - - return $a['priority'] - $b['priority']; - }); - - uasort($this->inline_css, function ($a, $b) { - if ($a['priority'] == $b['priority']) { - return $b['order'] - $a['order']; - } - - return $a['priority'] - $b['priority']; - }); + uasort($this->css, array($this, 'sortAssetsByPriorityThenOrder')); + uasort($this->inline_css, array($this, 'sortAssetsByPriorityThenOrder')); } - $this->css = array_reverse($this->css); - $this->inline_css = array_reverse($this->inline_css); $inlineGroup = array_key_exists('loading', $attributes) && $attributes['loading'] === 'inline'; @@ -609,24 +594,8 @@ class Assets } // Sort array by priorities (larger priority first) - uasort($this->js, function ($a, $b) { - if ($a['priority'] == $b['priority']) { - return $b['order'] - $a['order']; - } - - return $a['priority'] - $b['priority']; - }); - - uasort($this->inline_js, function ($a, $b) { - if ($a['priority'] == $b['priority']) { - return $b['order'] - $a['order']; - } - - return $a['priority'] - $b['priority']; - }); - - $this->js = array_reverse($this->js); - $this->inline_js = array_reverse($this->inline_js); + uasort($this->js, array($this, 'sortAssetsByPriorityThenOrder')); + uasort($this->inline_js, array($this, 'sortAssetsByPriorityThenOrder')); $inlineGroup = array_key_exists('loading', $attributes) && $attributes['loading'] === 'inline'; @@ -1416,9 +1385,13 @@ class Assets * * @return mixed */ - protected function priorityCompare($a, $b) + protected function sortAssetsByPriorityThenOrder($a, $b) { - return $a ['priority'] - $b ['priority']; + if ($a['priority'] == $b['priority']) { + return $a['order'] - $b['order']; + } + + return $b['priority'] - $a['priority']; } }