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