New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds CacheBustingWorker #297
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 <kris.wallsmith@gmail.com> | ||
*/ | ||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
body{color:#222;background:#fff;} |
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__ . DIRECTORY_SEPARATOR . '..'); | ||
$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__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $filename; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
$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__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $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); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc is wrong here as they are now 1 and 2 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done