ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
AssetCache.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2014 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
12namespace Assetic\Asset;
13
17
23class AssetCache implements AssetInterface
24{
25 private $asset;
26 private $cache;
27
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 getSourceDirectory()
96 {
97 return $this->asset->getSourceDirectory();
98 }
99
100 public function getTargetPath()
101 {
102 return $this->asset->getTargetPath();
103 }
104
105 public function setTargetPath($targetPath)
106 {
107 $this->asset->setTargetPath($targetPath);
108 }
109
110 public function getLastModified()
111 {
112 return $this->asset->getLastModified();
113 }
114
115 public function getVars()
116 {
117 return $this->asset->getVars();
118 }
119
120 public function setValues(array $values)
121 {
122 $this->asset->setValues($values);
123 }
124
125 public function getValues()
126 {
127 return $this->asset->getValues();
128 }
129
147 private static function getCacheKey(AssetInterface $asset, FilterInterface $additionalFilter = null, $salt = '')
148 {
149 if ($additionalFilter) {
150 $asset = clone $asset;
151 $asset->ensureFilter($additionalFilter);
152 }
153
154 $cacheKey = $asset->getSourceRoot();
155 $cacheKey .= $asset->getSourcePath();
156 $cacheKey .= $asset->getTargetPath();
157 $cacheKey .= $asset->getLastModified();
158
159 foreach ($asset->getFilters() as $filter) {
160 if ($filter instanceof HashableInterface) {
161 $cacheKey .= $filter->hash();
162 } else {
163 $cacheKey .= serialize($filter);
164 }
165 }
166
167 if ($values = $asset->getValues()) {
168 asort($values);
169 $cacheKey .= serialize($values);
170 }
171
172 return md5($cacheKey.$salt);
173 }
174}
Caches an asset to avoid the cost of loading and dumping.
Definition: AssetCache.php:24
getLastModified()
Returns the time the current asset was last modified.
Definition: AssetCache.php:110
setTargetPath($targetPath)
Sets the URL for the current asset.
Definition: AssetCache.php:105
setContent($content)
Sets the content of the current asset.
Definition: AssetCache.php:80
getFilters()
Returns an array of filters currently applied.
Definition: AssetCache.php:39
getContent()
Returns the loaded content of the current asset.
Definition: AssetCache.php:75
getSourcePath()
Returns the relative path for the source asset.
Definition: AssetCache.php:90
getTargetPath()
Returns the URL for the current asset.
Definition: AssetCache.php:100
setValues(array $values)
Sets the values for the asset's variables.
Definition: AssetCache.php:120
dump(FilterInterface $additionalFilter=null)
Applies dump filters and returns the asset as a string.
Definition: AssetCache.php:62
ensureFilter(FilterInterface $filter)
Ensures the current asset includes the supplied filter.
Definition: AssetCache.php:34
clearFilters()
Clears all filters from the current asset.
Definition: AssetCache.php:44
static getCacheKey(AssetInterface $asset, FilterInterface $additionalFilter=null, $salt='')
Returns a cache key for the current asset.
Definition: AssetCache.php:147
getValues()
Returns the current values for this asset.
Definition: AssetCache.php:125
getSourceRoot()
Returns an absolute path or URL to the source asset's root directory.
Definition: AssetCache.php:85
getSourceDirectory()
Returns the asset's source directory.
Definition: AssetCache.php:95
load(FilterInterface $additionalFilter=null)
Loads the asset into memory and applies load filters.
Definition: AssetCache.php:49
__construct(AssetInterface $asset, CacheInterface $cache)
Definition: AssetCache.php:28
getVars()
Returns an array of variable names for this asset.
Definition: AssetCache.php:115
An exception for terminatinating execution or to throw for unit testing.
An asset has a mutable URL and content and can be loaded and dumped.
Interface for a cache backend.
A filter manipulates an asset at load and dump.
A filter can implement a hash function.