From 250f31ba4753ddc15885df92da06aa9a6753bae4 Mon Sep 17 00:00:00 2001 From: Warnar Boekkooi Date: Thu, 14 Apr 2016 14:30:16 +0200 Subject: [PATCH] Strip a parents cache hash from the provided targetPath. --- .../Factory/Worker/CacheBustingWorker.php | 13 +++++++---- .../Factory/Worker/CacheBustingWorkerTest.php | 23 +++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/Assetic/Factory/Worker/CacheBustingWorker.php b/src/Assetic/Factory/Worker/CacheBustingWorker.php index 712c90d8f..ae3b949c9 100644 --- a/src/Assetic/Factory/Worker/CacheBustingWorker.php +++ b/src/Assetic/Factory/Worker/CacheBustingWorker.php @@ -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'] ); } diff --git a/tests/Assetic/Test/Factory/Worker/CacheBustingWorkerTest.php b/tests/Assetic/Test/Factory/Worker/CacheBustingWorkerTest.php index ecdf6fc0f..c6c9b5c54 100644 --- a/tests/Assetic/Test/Factory/Worker/CacheBustingWorkerTest.php +++ b/tests/Assetic/Test/Factory/Worker/CacheBustingWorkerTest.php @@ -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') @@ -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 */