Manifest.php
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Nextend\Framework\Cache;
class Manifest extends AbstractCache {
private $isRaw = false;
private $manifestData;
public function __construct($group, $isAccessible = false, $isRaw = false) {
parent::__construct($group, $isAccessible);
$this->isRaw = $isRaw;
}
protected function decode($data) {
return $data;
}
/**
* @param $fileName
* @param $hash
* @param callback $callable
*
* @return bool
*/
public function makeCache($fileName, $hash, $callable) {
if (!$this->isCached($fileName, $hash)) {
$return = call_user_func($callable, $this);
if ($return === false) {
return false;
}
return $this->createCacheFile($fileName, $hash, $return);
}
if ($this->isAccessible) {
return $this->getPath($fileName);
}
return $this->decode($this->get($fileName));
}
private function isCached($fileName, $hash) {
$manifestKey = $this->getManifestKey($fileName);
if ($this->exists($manifestKey)) {
$this->manifestData = json_decode($this->get($manifestKey), true);
if (!$this->isCacheValid($this->manifestData) || $this->manifestData['hash'] != $hash || !$this->exists($fileName)) {
$this->clean($fileName);
return false;
}
return true;
}
return false;
}
protected function createCacheFile($fileName, $hash, $content) {
$this->manifestData = array();
$this->manifestData['hash'] = $hash;
$this->addManifestData($this->manifestData);
$this->set($this->getManifestKey($fileName), json_encode($this->manifestData));
$this->set($fileName, $this->isRaw ? $content : json_encode($content));
if ($this->isAccessible) {
return $this->getPath($fileName);
}
return $content;
}
protected function isCacheValid(&$manifestData) {
return true;
}
protected function addManifestData(&$manifestData) {
}
public function clean($fileName) {
$this->remove($this->getManifestKey($fileName));
$this->remove($fileName);
}
protected function getManifestKey($fileName) {
return $fileName . '.manifest';
}
public function getData($key, $default = 0) {
return isset($this->manifestData[$key]) ? $this->manifestData[$key] : $default;
}
}