/
SQLiteProfilerStorage.php
183 lines (159 loc) · 5.29 KB
/
SQLiteProfilerStorage.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Symfony\Component\HttpKernel\Profiler;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* SQLiteProfilerStorage stores profiling information in a SQLite database.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class SQLiteProfilerStorage implements ProfilerStorageInterface
{
protected $store;
protected $lifetime;
/**
* Constructor.
*
* @param string $store The path to the SQLite DB
* @param integer $lifetime The lifetime to use for the purge
*/
public function __construct($store, $lifetime = 86400)
{
$this->store = $store;
$this->lifetime = (int) $lifetime;
}
/**
* {@inheritdoc}
*/
public function find($ip, $url, $limit)
{
$criteria = array();
if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
$criteria[] = ' ip LIKE "%'.$ip.'%"';
}
if ($url) {
$criteria[] = ' url LIKE "%'.$url.'%"';
}
$criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
$db = $this->initDb();
$tokens = $this->fetch($db, 'SELECT token, ip, url, time FROM data '.$criteria.' ORDER BY time DESC LIMIT '.((integer) $limit));
$this->close($db);
return $tokens;
}
/**
* {@inheritdoc}
*/
public function read($token)
{
$db = $this->initDb();
$args = array(':token' => $token);
$data = $this->fetch($db, 'SELECT data, ip, url, time FROM data WHERE token = :token ORDER BY time DESC LIMIT 1', $args);
$this->close($db);
if (isset($data[0]['data'])) {
return array($data[0]['data'], $data[0]['ip'], $data[0]['url'], $data[0]['time']);
} else {
return false;
}
}
/**
* {@inheritdoc}
*/
public function write($token, $data, $ip, $url, $time)
{
$db = $this->initDb();
$args = array(
':token' => $token,
':data' => $data,
':ip' => $ip,
':url' => $url,
':time' => $time,
);
$this->exec($db, 'INSERT INTO data (token, data, ip, url, time) VALUES (:token, :data, :ip, :url, :time)', $args);
$this->cleanup();
$this->close($db);
}
/**
* {@inheritdoc}
*/
public function purge()
{
$db = $this->initDb();
$this->exec($db, 'DELETE FROM data');
$this->close($db);
}
protected function cleanup()
{
$db = $this->initDb();
$this->exec($db, 'DELETE FROM data WHERE time < :time', array(':time' => time() - $this->lifetime));
$this->close($db);
}
/**
* @throws \RuntimeException When neither of SQLite or PDO_SQLite extension is enabled
*/
protected function initDb()
{
if (class_exists('SQLite3')) {
$db = new \SQLite3($this->store, \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
} elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
$db = new \PDO('sqlite:'.$this->store);
} else {
throw new \RuntimeException('You need to enable either the SQLite or PDO_SQLite extension for the profiler to run properly.');
}
$db->exec('CREATE TABLE IF NOT EXISTS data (token STRING, data STRING, ip STRING, url STRING, time INTEGER)');
$db->exec('CREATE INDEX IF NOT EXISTS data_data ON data (time)');
$db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON data (token)');
return $db;
}
protected function exec($db, $query, array $args = array())
{
$stmt = $db->prepare($query);
if ($db instanceof \SQLite3) {
foreach ($args as $arg => $val) {
$stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
}
$res = $stmt->execute();
$res->finalize();
} else {
foreach ($args as $arg => $val) {
$stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
}
$stmt->execute();
}
}
protected function fetch($db, $query, array $args = array())
{
$return = array();
$stmt = $db->prepare($query);
if ($db instanceof \SQLite3) {
foreach ($args as $arg => $val) {
$stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
}
$res = $stmt->execute();
while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
$return[] = $row;
}
$res->finalize();
$stmt->close();
} else {
foreach ($args as $arg => $val) {
$stmt->bindValue($arg, $val, is_int($val) ? \PDO::PARAM_INT : \PDO::PARAM_STR);
}
$stmt->execute();
$return = $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
return $return;
}
protected function close($db)
{
if ($db instanceof \SQLite3) {
$db->close();
}
}
}