AsseticController.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Asset\AssetCache;
  12. use Assetic\Asset\AssetInterface;
  13. use Assetic\Factory\LazyAssetManager;
  14. use Assetic\Cache\CacheInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\HttpKernel\Profiler\Profiler;
  19. /**
  20. * Serves assets.
  21. *
  22. * @author Kris Wallsmith <kris@symfony.com>
  23. */
  24. class AsseticController
  25. {
  26. protected $request;
  27. protected $am;
  28. protected $cache;
  29. protected $enableProfiler;
  30. protected $profiler;
  31. public function __construct(Request $request, LazyAssetManager $am, CacheInterface $cache, $enableProfiler = false, Profiler $profiler = null)
  32. {
  33. $this->request = $request;
  34. $this->am = $am;
  35. $this->cache = $cache;
  36. $this->enableProfiler = (boolean) $enableProfiler;
  37. $this->profiler = $profiler;
  38. }
  39. public function render($name, $pos = null)
  40. {
  41. if (!$this->enableProfiler && null !== $this->profiler) {
  42. $this->profiler->disable();
  43. }
  44. if (!$this->am->has($name)) {
  45. throw new NotFoundHttpException(sprintf('The "%s" asset could not be found.', $name));
  46. }
  47. $asset = $this->am->get($name);
  48. if (null !== $pos && !$asset = $this->findAssetLeaf($asset, $pos)) {
  49. throw new NotFoundHttpException(sprintf('The "%s" asset does not include a leaf at position %d.', $name, $pos));
  50. }
  51. $response = $this->createResponse();
  52. $response->setExpires(new \DateTime());
  53. // last-modified
  54. if (null !== $lastModified = $asset->getLastModified()) {
  55. $date = new \DateTime();
  56. $date->setTimestamp($lastModified);
  57. $response->setLastModified($date);
  58. }
  59. // etag
  60. if ($this->am->hasFormula($name)) {
  61. $formula = $this->am->getFormula($name);
  62. $formula['last_modified'] = $lastModified;
  63. $response->setETag(md5(serialize($formula)));
  64. }
  65. if ($response->isNotModified($this->request)) {
  66. return $response;
  67. }
  68. $response->setContent($this->cachifyAsset($asset)->dump());
  69. return $response;
  70. }
  71. protected function createResponse()
  72. {
  73. return new Response();
  74. }
  75. protected function cachifyAsset(AssetInterface $asset)
  76. {
  77. return new AssetCache($asset, $this->cache);
  78. }
  79. private function findAssetLeaf(\Traversable $asset, $pos)
  80. {
  81. $i = 0;
  82. foreach ($asset as $leaf) {
  83. if ($pos == $i++) {
  84. return $leaf;
  85. }
  86. }
  87. }
  88. }