/**
* Update Dependencies state for all active Assets or only for given
*
* @param string $type The asset type, script or style
* @param WebAssetItem $asset The asset instance to which need to enable dependencies
*
* @return self
*
* @since 4.0.0
*/
protected function enableDependencies(string $type = null, WebAssetItem $asset = null) : self
{
if ($type === 'preset') {
// Preset items already was enabled by usePresetItems()
return $this;
}
if ($asset) {
// Get all dependencies of given asset recursively
$allDependencies = $this->getDependenciesForAsset($type, $asset, true);
foreach ($allDependencies as $depType => $depItems) {
foreach ($depItems as $depItem) {
// Set dependency state only when it is inactive, to keep a manually activated Asset in their original state
if (empty($this->activeAssets[$depType][$depItem->getName()])) {
// Add the dependency at the top of the list of active assets
$this->activeAssets[$depType] = [$depItem->getName() => static::ASSET_STATE_DEPENDENCY] + $this->activeAssets[$depType];
}
}
}
} else {
// Re-Check for dependencies for all active assets
// Firstly, filter out only active assets
foreach ($this->activeAssets as $type => $activeAsset) {
$this->activeAssets[$type] = array_filter($activeAsset, function ($state) {
return $state === WebAssetManager::ASSET_STATE_ACTIVE;
});
}
// Secondary, check for dependencies of each active asset
// This need to be separated from previous step because we may have "cross type" dependency
foreach ($this->activeAssets as $type => $activeAsset) {
foreach (array_keys($activeAsset) as $name) {
$asset = $this->registry->get($type, $name);
$this->enableDependencies($type, $asset);
}
}
$this->dependenciesIsActual = true;
}
return $this;
}