⇦ Back to WebAssetManager classMethod calculateOrderOfActiveAssets
protected \Joomla\CMS\WebAsset\WebAssetItem[]
calculateOrderOfActiveAssets
(mixed $type)
Calculate weight of active Assets, by its Dependencies
Parameters
- string $type The asset type, script or style
Returns
- \Joomla\CMS\WebAsset\WebAssetItem[]
Since
Method calculateOrderOfActiveAssets - Source code
protected function calculateOrderOfActiveAssets($type) : array
{
$graphOrder = [];
$activeAssets = $this->getAssets($type, false);
$connectionsGraph = $this->getConnectionsGraph($activeAssets);
$graphOutgoing = $connectionsGraph['outgoing'];
$graphIncoming = $connectionsGraph['incoming'];
$graphIncomingCopy = $graphIncoming;
$emptyIncoming = array_keys(array_filter($graphIncoming, function ($el) {
return !$el;
}));
$emptyIncoming = array_reverse($emptyIncoming);
while ($emptyIncoming) {
$item = array_shift($emptyIncoming);
$graphOrder[] = $item;
foreach (array_reverse($graphOutgoing[$item]) as $neighbor) {
unset($graphIncoming[$neighbor][$item]);
if (empty($graphIncoming[$neighbor])) {
$emptyIncoming[] = $neighbor;
}
}
}
$fifoWeights = [];
$graphWeights = [];
$requestedWeights = [];
foreach (array_keys($this->activeAssets[$type]) as $index => $name) {
$fifoWeights[$name] = $index * 10 + 10;
}
foreach (array_reverse($graphOrder) as $index => $name) {
$graphWeights[$name] = $index * 10 + 10;
$requestedWeights[$name] = $activeAssets[$name]->getOption('weight') ?: $fifoWeights[$name];
}
while ($requestedWeights) {
$item = key($requestedWeights);
$weight = array_shift($requestedWeights);
if ($weight === null) {
continue;
}
$topBorder = $weight - 1;
if (!empty($graphOutgoing[$item])) {
$prevWeights = [];
foreach ($graphOutgoing[$item] as $pItem) {
$prevWeights[] = $graphWeights[$pItem];
}
$topBorder = max($prevWeights);
}
$newWeight = $weight > $topBorder ? $weight : $topBorder + 1;
if ($newWeight > $graphWeights[$item] && !empty($graphIncomingCopy[$item])) {
foreach ($graphIncomingCopy[$item] as $incomingItem) {
if (empty($requestedWeights[$incomingItem])) {
$requestedWeights[$incomingItem] = $graphWeights[$incomingItem] + $newWeight;
}
}
}
$graphWeights[$item] = $newWeight;
}
asort($graphWeights);
$resultAssets = [];
foreach (array_keys($graphWeights) as $name) {
$resultAssets[$name] = $activeAssets[$name];
}
return $resultAssets;
}