AssetCache.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Asset;
  11. use Assetic\Cache\CacheInterface;
  12. use Assetic\Filter\FilterInterface;
  13. /**
  14. * Caches an asset to avoid the cost of loading and dumping.
  15. *
  16. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  17. */
  18. class AssetCache implements AssetInterface
  19. {
  20. private $asset;
  21. private $cache;
  22. public function __construct(AssetInterface $asset, CacheInterface $cache)
  23. {
  24. $this->asset = $asset;
  25. $this->cache = $cache;
  26. }
  27. public function ensureFilter(FilterInterface $filter)
  28. {
  29. $this->asset->ensureFilter($filter);
  30. }
  31. public function getFilters()
  32. {
  33. return $this->asset->getFilters();
  34. }
  35. public function clearFilters()
  36. {
  37. $this->asset->clearFilters();
  38. }
  39. public function load(FilterInterface $additionalFilter = null)
  40. {
  41. $cacheKey = self::getCacheKey($this->asset, $additionalFilter, 'load');
  42. if ($this->cache->has($cacheKey)) {
  43. $this->asset->setContent($this->cache->get($cacheKey));
  44. return;
  45. }
  46. $this->asset->load($additionalFilter);
  47. $this->cache->set($cacheKey, $this->asset->getContent());
  48. }
  49. public function dump(FilterInterface $additionalFilter = null)
  50. {
  51. $cacheKey = self::getCacheKey($this->asset, $additionalFilter, 'dump');
  52. if ($this->cache->has($cacheKey)) {
  53. return $this->cache->get($cacheKey);
  54. }
  55. $content = $this->asset->dump($additionalFilter);
  56. $this->cache->set($cacheKey, $content);
  57. return $content;
  58. }
  59. public function getContent()
  60. {
  61. return $this->asset->getContent();
  62. }
  63. public function setContent($content)
  64. {
  65. $this->asset->setContent($content);
  66. }
  67. public function getSourceRoot()
  68. {
  69. return $this->asset->getSourceRoot();
  70. }
  71. public function getSourcePath()
  72. {
  73. return $this->asset->getSourcePath();
  74. }
  75. public function getTargetPath()
  76. {
  77. return $this->asset->getTargetPath();
  78. }
  79. public function setTargetPath($targetPath)
  80. {
  81. $this->asset->setTargetPath($targetPath);
  82. }
  83. public function getLastModified()
  84. {
  85. return $this->asset->getLastModified();
  86. }
  87. /**
  88. * Returns a cache key for the current asset.
  89. *
  90. * The key is composed of everything but an asset's content:
  91. *
  92. * * source root
  93. * * source path
  94. * * target url
  95. * * last modified
  96. * * filters
  97. *
  98. * @param AssetInterface $asset The asset
  99. * @param FilterInterface $additionalFilter Any additional filter being applied
  100. * @param string $salt Salt for the key
  101. *
  102. * @return string A key for identifying the current asset
  103. */
  104. static private function getCacheKey(AssetInterface $asset, FilterInterface $additionalFilter = null, $salt = '')
  105. {
  106. if ($additionalFilter) {
  107. $asset = clone $asset;
  108. $asset->ensureFilter($additionalFilter);
  109. }
  110. $cacheKey = $asset->getSourceRoot();
  111. $cacheKey .= $asset->getSourcePath();
  112. $cacheKey .= $asset->getTargetPath();
  113. $cacheKey .= $asset->getLastModified();
  114. foreach ($asset->getFilters() as $filter) {
  115. $cacheKey .= serialize($filter);
  116. }
  117. return md5($cacheKey.$salt);
  118. }
  119. }