Skip to content

Commit ad80b09

Browse files
committed
Merge remote branch 'kriswallsmith/assetic/etags'
* kriswallsmith/assetic/etags: [AsseticBundle] added etags to controller so changes to filters etc trigger invalidation
2 parents ee4db2a + 968c870 commit ad80b09

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/Symfony/Bundle/AsseticBundle/Controller/AsseticController.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Bundle\AsseticBundle\Controller;
1313

1414
use Assetic\Asset\AssetCache;
15-
use Assetic\AssetManager;
15+
use Assetic\Factory\LazyAssetManager;
1616
use Assetic\Cache\CacheInterface;
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\Response;
@@ -29,7 +29,7 @@ class AsseticController
2929
protected $am;
3030
protected $cache;
3131

32-
public function __construct(Request $request, AssetManager $am, CacheInterface $cache)
32+
public function __construct(Request $request, LazyAssetManager $am, CacheInterface $cache)
3333
{
3434
$this->request = $request;
3535
$this->am = $am;
@@ -43,25 +43,36 @@ public function render($name)
4343
}
4444

4545
$asset = $this->getAsset($name);
46+
$response = $this->createResponse();
4647

47-
$response = new Response();
48-
49-
// validate if-modified-since
48+
// last-modified
5049
if (null !== $lastModified = $asset->getLastModified()) {
5150
$date = new \DateTime();
5251
$date->setTimestamp($lastModified);
5352
$response->setLastModified($date);
53+
}
54+
55+
// etag
56+
if ($this->am->hasFormula($name)) {
57+
$formula = $this->am->getFormula($name);
58+
$formula['last_modified'] = $lastModified;
59+
$response->setETag(md5(serialize($formula)));
60+
}
5461

55-
if ($response->isNotModified($this->request)) {
56-
return $response;
57-
}
62+
if ($response->isNotModified($this->request)) {
63+
return $response;
5864
}
5965

6066
$response->setContent($asset->dump());
6167

6268
return $response;
6369
}
6470

71+
protected function createResponse()
72+
{
73+
return new Response();
74+
}
75+
6576
protected function getAsset($name)
6677
{
6778
return new AssetCache($this->am->get($name), $this->cache);

0 commit comments

Comments
 (0)