AssetCache.php 3.7KB

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