Skip to content

Commit

Permalink
Rename priority to order per @LukeTowers comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bennothommo authored Dec 22, 2023
1 parent b46d04d commit 570b9c7
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions modules/system/traits/AssetMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ trait AssetMaker
public $assetPath;

/**
* Ensures "first-come, first-served" applies to assets of the same priority level.
* Ensures "first-come, first-served" applies to assets of the same ordering.
*/
protected int $priorityFactor = 0;
protected int $orderFactor = 0;

/**
* Disables the use, and subequent broadcast, of assets. This is useful
Expand All @@ -55,12 +55,12 @@ public function makeAssets(string $type = null): ?string
$type = strtolower($type);
}
$result = null;
$reserved = ['build', 'priority', 'preload'];
$reserved = ['build', 'order', 'preload'];

$this->removeDuplicates();

if ($type == null || $type == 'css') {
foreach ($this->prioritiseAssets($this->assets['css']) as $asset) {
foreach ($this->orderAssets($this->assets['css']) as $asset) {
if ($asset['attributes']['preload'] ?? false) {
$preloadAttributes = Html::attributes([
'rel' => 'preload',
Expand Down Expand Up @@ -88,7 +88,7 @@ public function makeAssets(string $type = null): ?string
}

if ($type == null || $type == 'rss') {
foreach ($this->prioritiseAssets($this->assets['rss']) as $asset) {
foreach ($this->orderAssets($this->assets['rss']) as $asset) {
$attributes = Html::attributes(array_merge(
[
'rel' => 'alternate',
Expand All @@ -104,7 +104,7 @@ public function makeAssets(string $type = null): ?string
}

if ($type == null || $type == 'js') {
foreach ($this->prioritiseAssets($this->assets['js']) as $asset) {
foreach ($this->orderAssets($this->assets['js']) as $asset) {
if ($asset['attributes']['preload'] ?? false) {
$preloadAttributes = Html::attributes([
'rel' => 'preload',
Expand Down Expand Up @@ -269,12 +269,12 @@ protected function addAsset(string $type, string $path, array $attributes)
// Fire global event
(Event::fire('system.assets.beforeAddAsset', [&$type, &$path, &$attributes], true) !== false)
) {
$this->priorityFactor++;
$this->orderFactor++;

// Apply priority
$attributes['priority'] = (!isset($attributes['priority']))
? 100 + ($this->priorityFactor / 10000)
: intval($attributes['priority']) + ($this->priorityFactor / 10000);
// Apply ordering
$attributes['order'] = (!isset($attributes['order']))
? 500 + ($this->orderFactor / 10000)
: intval($attributes['order']) + ($this->orderFactor / 10000);

$this->assets[$type][] = ['path' => $path, 'attributes' => $attributes];
}
Expand All @@ -300,7 +300,7 @@ public function combineAssets(array $assets, $localPath = '')
/**
* Returns an array of all registered asset paths.
*
* Assets will be prioritised based on their priority levels.
* Assets will be prioritized based on their defined ordering.
*
* @return array
*/
Expand All @@ -311,7 +311,7 @@ public function getAssetPaths()
$assets = [];
foreach ($this->assets as $type => $collection) {
$assets[$type] = [];
foreach ($this->prioritiseAssets($collection) as $asset) {
foreach ($this->orderAssets($collection) as $asset) {
$assets[$type][] = $this->getAssetEntryBuildPath($asset);
}
}
Expand Down Expand Up @@ -434,16 +434,16 @@ protected function getLocalPath(?string $relativePath)
}

/**
* Prioritise assets based on a given "priority" level.
* Prioritize assets based on the given order.
*/
public function prioritizeAssets(array $assets): array
public function orderAssets(array $assets): array
{
// Copy assets array so that the stored asset array is not modified.
$sortedAssets = $assets;

array_multisort(
array_map(function ($item) {
return $item['attributes']['priority'];
return $item['attributes']['order'];
}, $sortedAssets),
SORT_NUMERIC,
SORT_ASC,
Expand Down

0 comments on commit 570b9c7

Please sign in to comment.