Issues (55)

src/storage/Cache.php (1 issue)

Severity
1
<?php
2
/**
3
 * Created by jensk on 25-10-2017.
4
 */
5
6
namespace CloudControl\Cms\storage;
7
8
9
use CloudControl\Cms\util\GlobalFunctions;
10
11
class Cache
12
{
13
    /**
14
     * @var Cache
15
     */
16
    private static $instance;
17
18
    /**
19
     * @var string
20
     */
21
    protected $storagePath;
22
    /**
23
     * @var \PDO
24
     */
25
    protected $dbHandle;
26
27
    private function __construct()
28
    {
29
        // Singleton, so private constructor
30
    }
31
32
    /**
33
     * @return Cache
34
     */
35
    public static function getInstance()
36
    {
37
        if (!self::$instance instanceof Cache) {
0 ignored issues
show
self::instance is always a sub-type of CloudControl\Cms\storage\Cache.
Loading history...
38
            self::$instance = new Cache();
39
        }
40
        return self::$instance;
41
    }
42
43
    /**
44
     * @param $path
45
     * @return \stdClass
46
     * @throws \RuntimeException
47
     */
48
    public function getCacheForPath($path)
49
    {
50
        $dbInstace = $this->getDbInstance();
51
        $sql = '
52
            SELECT *
53
              FROM `cache`
54
             WHERE `path` = :path
55
             LIMIT 1
56
        ';
57
        $stmt = $dbInstace->prepare($sql);
58
        $stmt->bindParam(':path', $path);
59
        if ($stmt->execute()) {
60
            return $stmt->fetch(\PDO::FETCH_OBJ);
61
        } else {
62
            $error = $stmt->errorInfo();
63
            $errorMsg = $error[2];
64
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
65
        }
66
    }
67
68
    /**
69
     * Clears all cache
70
     */
71
    public function clearCache()
72
    {
73
        $dbInstace = $this->getDbInstance();
74
        $sql = '
75
            DELETE FROM `cache`;
76
            VACUUM;
77
        ';
78
        $stmt = $dbInstace->prepare($sql);
79
        if ($stmt->execute()) {
80
            return;
81
        } else {
82
            $error = $stmt->errorInfo();
83
            $errorMsg = $error[2];
84
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
85
        }
86
    }
87
88
    /**
89
     * Clears the cache for path
90
     *
91
     * @param $path string
92
     * @return mixed
93
     */
94
    public function clearCacheForPath($path)
95
    {
96
        $dbInstace = $this->getDbInstance();
97
        $sql = '
98
       DELETE FROM `cache`
99
             WHERE `path` = :path;
100
        ';
101
        $stmt = $dbInstace->prepare($sql);
102
        $stmt->bindParam(':path', $path);
103
        if ($stmt->execute()) {
104
            return $stmt->fetch(\PDO::FETCH_OBJ);
105
        } else {
106
            $error = $stmt->errorInfo();
107
            $errorMsg = $error[2];
108
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
109
        }
110
    }
111
112
    /**
113
     * @param $storagePath
114
     */
115
    public function setStoragePath($storagePath)
116
    {
117
        $this->storagePath = $storagePath;
118
    }
119
120
    /**
121
     * @return \PDO
122
     */
123
    private function getDbInstance()
124
    {
125
        if ($this->dbHandle === null) {
126
            $this->dbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'cache.db');
127
        }
128
        return $this->dbHandle;
129
    }
130
131
    /**
132
     * @param $baseCacheSqlPath
133
     */
134
    public function init($baseCacheSqlPath)
135
    {
136
        $realBaseCacheSqlPath = realpath($baseCacheSqlPath);
137
138
        $db = $this->getDbInstance();
139
        $sql = file_get_contents($realBaseCacheSqlPath);
140
        $db->exec($sql);
141
    }
142
143
    /**
144
     * @param string $requestUri
145
     * @param string $renderedContent
146
     * @param string $headers
147
     * @throws \RuntimeException
148
     */
149
    public function setCacheForPath($requestUri, $renderedContent, $headers)
150
    {
151
        $dbInstace = $this->getDbInstance();
152
        $sql = '
153
            INSERT OR REPLACE INTO `cache` (path, creationStamp, headers, contents)
154
                 VALUES (:path, :creationStamp, :headers, :contents);
155
        ';
156
        $contents = GlobalFunctions::sanitizeOutput($renderedContent);
157
        $creationStamp = time();
158
        $stmt = $dbInstace->prepare($sql);
159
        $stmt->bindParam(':path', $requestUri);
160
        $stmt->bindParam(':creationStamp', $creationStamp);
161
        $stmt->bindParam(':contents', $contents);
162
        $stmt->bindParam(':headers', $headers);
163
        if ($stmt->execute()) {
164
            return;
165
        } else {
166
            $error = $stmt->errorInfo();
167
            $errorMsg = $error[2];
168
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
169
        }
170
    }
171
172
}