1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by jensk on 25-10-2017. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace CloudControl\Cms\storage; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Cache |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Cache |
13
|
|
|
*/ |
14
|
|
|
private static $instance; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $storagePath; |
20
|
|
|
/** |
21
|
|
|
* @var \PDO |
22
|
|
|
*/ |
23
|
|
|
protected $dbHandle; |
24
|
|
|
|
25
|
|
|
private function __construct() |
26
|
|
|
{ |
27
|
|
|
// Singleton, so private constructor |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function getInstance() |
31
|
|
|
{ |
32
|
|
|
if (!self::$instance instanceof Cache) { |
33
|
|
|
self::$instance = new Cache(); |
34
|
|
|
} |
35
|
|
|
return self::$instance; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getCacheForPath($path) |
39
|
|
|
{ |
40
|
|
|
$dbInstace = $this->getDbInstance(); |
41
|
|
|
$sql = ' |
42
|
|
|
SELECT * |
43
|
|
|
FROM `cache` |
44
|
|
|
WHERE `path` = :path |
45
|
|
|
LIMIT 1 |
46
|
|
|
'; |
47
|
|
|
$stmt = $dbInstace->prepare($sql); |
48
|
|
|
$stmt->bindParam(':path', $path); |
49
|
|
View Code Duplication |
if ($stmt->execute()) { |
|
|
|
|
50
|
|
|
return $stmt->fetch(\PDO::FETCH_OBJ); |
51
|
|
|
} else { |
52
|
|
|
$error = $stmt->errorInfo(); |
53
|
|
|
$errorMsg = $error[2]; |
54
|
|
|
throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setStoragePath($storagePath) |
59
|
|
|
{ |
60
|
|
|
$this->storagePath = $storagePath; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
private function getDbInstance() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
if ($this->dbHandle === null) { |
66
|
|
|
$this->dbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'cache.db'); |
67
|
|
|
} |
68
|
|
|
return $this->dbHandle; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function init($baseCacheSqlPath) |
72
|
|
|
{ |
73
|
|
|
$realBaseCacheSqlPath = realpath($baseCacheSqlPath); |
74
|
|
|
|
75
|
|
|
$db = $this->getDbInstance(); |
76
|
|
|
$sql = file_get_contents($realBaseCacheSqlPath); |
77
|
|
|
$db->exec($sql); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function setCacheForPath($requestUri, $renderedContent) |
81
|
|
|
{ |
82
|
|
|
$dbInstace = $this->getDbInstance(); |
83
|
|
|
$sql = ' |
84
|
|
|
INSERT OR REPLACE INTO `cache` (path, creationStamp, contents) |
85
|
|
|
VALUES (:path, :creationStamp, :contents); |
86
|
|
|
'; |
87
|
|
|
$contents = \sanitize_output($renderedContent); |
88
|
|
|
$creationStamp = time(); |
89
|
|
|
$stmt = $dbInstace->prepare($sql); |
90
|
|
|
$stmt->bindParam(':path', $requestUri); |
91
|
|
|
$stmt->bindParam(':creationStamp', $creationStamp); |
92
|
|
|
$stmt->bindParam(':contents', $contents); |
93
|
|
View Code Duplication |
if ($stmt->execute()) { |
|
|
|
|
94
|
|
|
return; |
95
|
|
|
} else { |
96
|
|
|
$error = $stmt->errorInfo(); |
97
|
|
|
$errorMsg = $error[2]; |
98
|
|
|
throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>'); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
} |