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