public function AssetCollection::replaceLeaf

Replaces an existing leaf with a new one.

Parameters

AssetInterface $needle The current asset to replace:

AssetInterface $replacement The new asset:

Boolean $graceful Whether the failure should return false or throw an exception:

Return value

Boolean Whether the asset has been found

Throws

\InvalidArgumentException If the asset cannot be found

Overrides AssetCollectionInterface::replaceLeaf

File

drupal/core/vendor/kriswallsmith/assetic/src/Assetic/Asset/AssetCollection.php, line 95

Class

AssetCollection
A collection of assets.

Namespace

Assetic\Asset

Code

public function replaceLeaf(AssetInterface $needle, AssetInterface $replacement, $graceful = false) {
  foreach ($this->assets as $i => $asset) {
    $clone = isset($this->clones[$asset]) ? $this->clones[$asset] : null;
    if (in_array($needle, array(
      $asset,
      $clone,
    ), true)) {
      unset($this->clones[$asset]);
      $this->assets[$i] = $replacement;
      return true;
    }
    if ($asset instanceof AssetCollectionInterface && $asset
      ->replaceLeaf($needle, $replacement, true)) {
      return true;
    }
  }
  if ($graceful) {
    return false;
  }
  throw new \InvalidArgumentException('Leaf not found.');
}