forked from kriswallsmith/assetic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'Fran6co/cache-buster' into cache-buster
Conflicts: CHANGELOG-1.1.md
- Loading branch information
Showing
7 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Assetic package, an OpenSky project. | ||
* | ||
* (c) 2010-2012 OpenSky Project Inc | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Assetic\Factory\Worker; | ||
|
||
use Assetic\Asset\AssetInterface; | ||
|
||
/** | ||
* Adds cache busting code | ||
* | ||
* @author Kris Wallsmith <[email protected]> | ||
*/ | ||
class CacheBustingWorker implements WorkerInterface | ||
{ | ||
const STRATEGY_CONTENT = 1; | ||
const STRATEGY_MODIFICATION = 2; | ||
|
||
private $strategy; | ||
|
||
public function __construct($strategy = self::STRATEGY_CONTENT) | ||
{ | ||
$this->strategy = $strategy; | ||
} | ||
|
||
public function process(AssetInterface $asset) | ||
{ | ||
$hash = hash_init('sha1'); | ||
|
||
switch($this->strategy) { | ||
case self::STRATEGY_MODIFICATION: | ||
hash_update($hash, $asset->getLastModified()); | ||
break; | ||
case self::STRATEGY_CONTENT: | ||
hash_update($hash, $asset->dump()); | ||
break; | ||
} | ||
|
||
foreach ($asset as $i => $leaf) { | ||
if ($sourcePath = $leaf->getSourcePath()) { | ||
hash_update($hash, $sourcePath); | ||
} else { | ||
hash_update($hash, $i); | ||
} | ||
} | ||
|
||
$hash = substr(hash_final($hash), 0, 7); | ||
$url = $asset->getTargetPath(); | ||
|
||
$oldExt = pathinfo($url, PATHINFO_EXTENSION); | ||
$newExt = '-'.$hash.'.'.$oldExt; | ||
|
||
if (!$oldExt || 0 < preg_match('/'.preg_quote($newExt, '/').'$/', $url)) { | ||
return; | ||
} | ||
|
||
$asset->setTargetPath(substr($url, 0, (strlen($oldExt) + 1) * -1).$newExt); | ||
} | ||
|
||
public function getStrategy() | ||
{ | ||
return $this->strategy; | ||
} | ||
|
||
public function setStrategy($strategy) | ||
{ | ||
$this->strategy = $strategy; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
body{color:#222;background:#fff;} |
76 changes: 76 additions & 0 deletions
76
tests/Assetic/Test/Factory/Worker/CacheBustingWorkerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Assetic package, an OpenSky project. | ||
* | ||
* (c) 2010-2012 OpenSky Project Inc | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Assetic\Test\Factory\Worker; | ||
|
||
use Assetic\Factory\AssetFactory; | ||
use Assetic\Factory\Worker\CacheBustingWorker; | ||
|
||
class CacheBustingWorkerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $worker; | ||
private $factory; | ||
|
||
protected function setUp() | ||
{ | ||
$am = $this->getMock('Assetic\\AssetManager'); | ||
$fm = $this->getMock('Assetic\\FilterManager'); | ||
|
||
$this->worker = new CacheBustingWorker(); | ||
|
||
$this->factory = new AssetFactory(__DIR__ . '/..'); | ||
$this->factory->setAssetManager($am); | ||
$this->factory->setFilterManager($fm); | ||
$this->factory->addWorker($this->worker); | ||
} | ||
|
||
|
||
public function testGenerateUniqueAssetNameByContent() | ||
{ | ||
$this->worker->setStrategy(CacheBustingWorker::STRATEGY_CONTENT); | ||
|
||
$filename = 'Resource/Fixtures/css/style.css'; | ||
$filepath = __DIR__ . '/../' . $filename; | ||
|
||
$originalContent = file_get_contents($filepath); | ||
|
||
file_put_contents($filepath, 'body{color:#444;background:#eee;}'); | ||
$asset = $this->factory->createAsset(array($filename)); | ||
$targetPath1 = $asset->getTargetPath(); | ||
|
||
file_put_contents($filepath, $originalContent); | ||
$asset = $this->factory->createAsset(array($filename)); | ||
$targetPath2 = $asset->getTargetPath(); | ||
|
||
$this->assertNotEquals($targetPath2, $targetPath1); | ||
} | ||
|
||
public function testGenerateUniqueAssetNameByModificationTime() | ||
{ | ||
$this->worker->setStrategy(CacheBustingWorker::STRATEGY_MODIFICATION); | ||
|
||
$filename = 'Resource/Fixtures/css/style.css'; | ||
$filepath = __DIR__ . '/../' . $filename; | ||
|
||
$asset = $this->factory->createAsset(array($filename)); | ||
$this->factory->addWorker(new CacheBustingWorker('modification')); | ||
$targetPath1 = $asset->getTargetPath(); | ||
|
||
sleep(1); | ||
touch($filepath); | ||
clearstatcache(); | ||
|
||
$asset = $this->factory->createAsset(array($filename)); | ||
$targetPath2 = $asset->getTargetPath(); | ||
|
||
$this->assertNotEquals($targetPath2, $targetPath1); | ||
} | ||
} |