AsseticController.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // last-modified
  53. if (null !== $lastModified = $asset->getLastModified()) {
  54. $date = new \DateTime();
  55. $date->setTimestamp($lastModified);
  56. $response->setLastModified($date);
  57. }
  58. // etag
  59. if ($this->am->hasFormula($name)) {
  60. $formula = $this->am->getFormula($name);
  61. $formula['last_modified'] = $lastModified;
  62. $response->setETag(md5(serialize($formula)));
  63. }
  64. if ($response->isNotModified($this->request)) {
  65. return $response;
  66. }
  67. $response->setContent($this->cachifyAsset($asset)->dump());
  68. return $response;
  69. }
  70. protected function createResponse()
  71. {
  72. return new Response();
  73. }
  74. protected function cachifyAsset(AssetInterface $asset)
  75. {
  76. return new AssetCache($asset, $this->cache);
  77. }
  78. private function findAssetLeaf(\Traversable $asset, $pos)
  79. {
  80. $i = 0;
  81. foreach ($asset as $leaf) {
  82. if ($pos == $i++) {
  83. return $leaf;
  84. }
  85. }
  86. }
  87. }