我知道这是一个老话题,但我唯一能接受的答案是 CompassElephantBundle 和上面的 AsseticController hack。我有一种方法,本质上但意味着我不必编辑供应商包。
我这样做的方法是编辑复制原始 AsseticController,然后从参数链接到配置中的那个。
parameters:
assetic.controller.class: Acme\RandomBundle\Controller\AsseticController
复制的 AsseticController 只是对源路径中的文件类型进行 preg_match 并从那里更正缓存。
<?php
/* Original Assetic Controller */
public function render($name, $pos = null)
{
if (!$this->enableProfiler && null !== $this->profiler) {
$this->profiler->disable();
}
if (!$this->am->has($name)) {
throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name));
}
$asset = $this->am->get($name);
if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) {
throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
}
$bustCache = preg_match('/\.(scss|sass|less)$/', $asset->getSourcePath());
$response = $this->createResponse();
$response->setExpires(new \DateTime());
if ($bustCache) {
$lastModified = time();
$date = new \DateTime();
$date->setTimestamp($lastModified);
$response->setLastModified($date);
}
else
{
// last-modified
if (null !== $lastModified = $asset->getLastModified()) {
$date = new \DateTime();
$date->setTimestamp($lastModified);
$response->setLastModified($date);
}
}
// etag
if ($this->am->hasFormula($name)) {
$formula = $this->am->getFormula($name);
$formula['last_modified'] = $lastModified;
$response->setETag(md5(serialize($formula)));
}
if ($response->isNotModified($this->request)) {
return $response;
}
if ($bustCache) {
$response->setContent($asset->dump());
}
else {
$response->setContent($this->cachifyAsset($asset)->dump());
}
return $response;
}
/* Rest of controller */