AsseticController.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Bundle\AsseticBundle\Controller;
  11. use Assetic\ValueSupplierInterface;
  12. use Assetic\Asset\AssetCache;
  13. use Assetic\Asset\AssetInterface;
  14. use Assetic\Factory\LazyAssetManager;
  15. use Assetic\Cache\CacheInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use Symfony\Component\HttpKernel\Profiler\Profiler;
  20. /**
  21. * Serves assets.
  22. *
  23. * @author Kris Wallsmith <kris@symfony.com>
  24. */
  25. class AsseticController
  26. {
  27. protected $request;
  28. protected $am;
  29. protected $cache;
  30. protected $enableProfiler;
  31. protected $profiler;
  32. protected $valueSupplier;
  33. public function __construct(Request $request, LazyAssetManager $am, CacheInterface $cache, $enableProfiler = false, Profiler $profiler = null)
  34. {
  35. $this->request = $request;
  36. $this->am = $am;
  37. $this->cache = $cache;
  38. $this->enableProfiler = (boolean) $enableProfiler;
  39. $this->profiler = $profiler;
  40. }
  41. public function setValueSupplier(ValueSupplierInterface $supplier)
  42. {
  43. $this->valueSupplier = $supplier;
  44. }
  45. public function render($name, $pos = null)
  46. {
  47. if (!$this->enableProfiler && null !== $this->profiler) {
  48. $this->profiler->disable();
  49. }
  50. if (!$this->am->has($name)) {
  51. throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name));
  52. }
  53. $asset = $this->am->get($name);
  54. if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) {
  55. throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
  56. }
  57. $response = $this->createResponse();
  58. $response->setExpires(new \DateTime());
  59. // last-modified
  60. if (null !== $lastModified = $asset->getLastModified()) {
  61. $date = new \DateTime();
  62. $date->setTimestamp($lastModified);
  63. $response->setLastModified($date);
  64. }
  65. // etag
  66. if ($this->am->hasFormula($name)) {
  67. $formula = $this->am->getFormula($name);
  68. $formula['last_modified'] = $lastModified;
  69. $response->setETag(md5(serialize($formula)));
  70. }
  71. if ($response->isNotModified($this->request)) {
  72. return $response;
  73. }
  74. $response->setContent($this->cachifyAsset($asset)->dump());
  75. return $response;
  76. }
  77. protected function createResponse()
  78. {
  79. return new Response();
  80. }
  81. protected function cachifyAsset(AssetInterface $asset)
  82. {
  83. if ($vars = $asset->getVars()) {
  84. if (null === $this->valueSupplier) {
  85. throw new \RuntimeException(sprintf('You must configure a value supplier if you have assets with variables.'));
  86. }
  87. $asset->setValues(array_intersect_key($this->valueSupplier->getValues(), array_flip($vars)));
  88. }
  89. return new AssetCache($asset, $this->cache);
  90. }
  91. private function findAssetLeaf(\Traversable $asset, $pos)
  92. {
  93. $i = 0;
  94. foreach ($asset as $leaf) {
  95. if ($pos == $i++) {
  96. return $leaf;
  97. }
  98. }
  99. }
  100. }