Skip to content

Commit

Permalink
[kriswallsmithGH-82] added clone once logic to asset iteration so cha…
Browse files Browse the repository at this point in the history
…nges made to each leaf persist between iterations
  • Loading branch information
kriswallsmith authored and fuel-packages committed Aug 16, 2011
1 parent 8bc3af3 commit 22e9107
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Assetic/Asset/AssetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ public function getChildren()
*/
class AssetCollectionIterator implements \RecursiveIterator
{
static private $clones = array();

private $assets;
private $filters;
private $output;
Expand Down Expand Up @@ -264,13 +266,20 @@ public function current($raw = false)
return $asset;
}

// clone before making changes
$clone = clone $asset;
// clone once
$oid = spl_object_hash($asset);
if (!isset(self::$clones[$oid])) {
self::$clones[$oid] = clone $asset;
}
$clone = self::$clones[$oid];

// generate an url based on asset name
$name = sprintf('%s_%d', pathinfo($asset->getSourcePath(), PATHINFO_FILENAME) ?: 'part', $this->key() + 1);
$clone->setTargetPath(str_replace('*', $name, $this->output));
if (!$clone->getTargetPath()) {
// generate a target path based on asset name
$name = sprintf('%s_%d', pathinfo($asset->getSourcePath(), PATHINFO_FILENAME) ?: 'part', $this->key() + 1);
$clone->setTargetPath(str_replace('*', $name, $this->output));
}

// cascade filters
foreach ($this->filters as $filter) {
$clone->ensureFilter($filter);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Assetic/Test/Asset/AssetCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,17 @@ public function testEmptyMtime()
$coll = new AssetCollection();
$this->assertNull($coll->getLastModified(), '->getLastModified() returns null on empty collection');
}

public function testLeafManipulation()
{
$coll = new AssetCollection(array(new StringAsset('asdf')));

foreach ($coll as $leaf) {
$leaf->setTargetPath('asdf');
}

foreach ($coll as $leaf) {
$this->assertEquals('asdf', $leaf->getTargetPath(), 'leaf changes persist between iterations');
}
}
}

0 comments on commit 22e9107

Please sign in to comment.