Skip to content

Commit

Permalink
Strip a parents cache hash from the provided targetPath.
Browse files Browse the repository at this point in the history
  • Loading branch information
boekkooi committed Apr 14, 2016
1 parent 4bb27cf commit 250f31b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/Assetic/Factory/Worker/CacheBustingWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ public function process(AssetInterface $asset, AssetFactory $factory)
return;
}

if (!$search = pathinfo($path, PATHINFO_EXTENSION)) {
$parts = pathinfo($path);
if (!isset($parts['extension'])) {
// nothing to replace
return;
}

$replace = $this->separator.$this->getHash($asset, $factory).'.'.$search;
if (preg_match('/'.preg_quote($replace, '/').'$/', $path)) {
$replace = $this->separator.$this->getHash($asset, $factory);
if (preg_match('/'.preg_quote($replace, '/').'$/', $parts['filename'])) {
// already replaced
return;
}

$asset->setTargetPath(
preg_replace('/\.'.preg_quote($search, '/').'$/', $replace, $path)
('.' === $parts['dirname'] ? '' : $parts['dirname'] . DIRECTORY_SEPARATOR).
preg_replace('/'.preg_quote($this->separator).'[a-z0-9]{7}_/', '_', $parts['filename'], 1) .
$replace .
'.' .
$parts['extension']
);
}

Expand Down
23 changes: 19 additions & 4 deletions tests/Assetic/Test/Factory/Worker/CacheBustingWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ protected function tearDown()

/**
* @test
* @dataProvider providePathExpectations
*/
public function shouldApplyHash()
public function shouldApplyHash($target, $expectedStart, $expectedEnd)
{
$asset = $this->getMock('Assetic\Asset\AssetInterface');
$factory = $this->getMockBuilder('Assetic\Factory\AssetFactory')
Expand All @@ -39,20 +40,34 @@ public function shouldApplyHash()

$asset->expects($this->any())
->method('getTargetPath')
->will($this->returnValue('css/main.css'));
->will($this->returnValue($target));
$factory->expects($this->any())
->method('getLastModified')
->will($this->returnValue(1234));
$asset->expects($this->once())
->method('setTargetPath')
->with($this->logicalAnd(
$this->stringStartsWith('css/main-'),
$this->stringEndsWith('.css')
$this->stringStartsWith($expectedStart),
$this->stringEndsWith($expectedEnd),
$this->matchesRegularExpression('/^'.preg_quote($expectedStart, '/').'[a-z0-9]{7}'.preg_quote($expectedEnd, '/').'$/')
));

$this->worker->process($asset, $factory);
}

public function providePathExpectations()
{
return array(
array('main.js', 'main-', '.js'),
array('css/main.css', 'css/main-', '.css'),

// Strip parent hash
array('main-32d5523_leaf.js', 'main_leaf-', '.js'),
array('main-32d5523_love-my6char_file.js', 'main_love-my6char_file-', '.js'),
array('css-7110eda_css/main-7110eda_leaf.css', 'css-7110eda_css/main_leaf-', '.css'),
);
}

/**
* @test
*/
Expand Down

0 comments on commit 250f31b

Please sign in to comment.