|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\HttpKernel\Profiler; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* SqliteProfilerStorage stores profiling information in a SQLite database. |
|
|
18 |
* |
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
20 |
*/ |
|
|
21 |
class SqliteProfilerStorage extends PdoProfilerStorage |
|
|
22 |
{ |
|
|
23 |
/** |
|
|
24 |
* @throws \RuntimeException When neither of SQLite3 or PDO_SQLite extension is enabled |
|
|
25 |
*/ |
|
|
26 |
protected function initDb() |
|
|
27 |
{ |
|
|
28 |
if (null === $this->db || $this->db instanceof \SQLite3) { |
|
|
29 |
if ('sqlite' !== substr($this->dsn, 0, 6 )) { |
|
|
30 |
throw new \RuntimeException('You are trying to use Sqlite with a wrong dsn. "'.$this->dsn.'"'); |
|
|
31 |
} |
|
|
32 |
if (class_exists('SQLite3')) { |
|
|
33 |
$db = new \SQLite3(substr($this->dsn, 7, strlen($this->dsn)), \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE); |
|
|
34 |
if (method_exists($db, 'busyTimeout')) { |
|
|
35 |
// busyTimeout only exists for PHP >= 5.3.3 |
|
|
36 |
$db->busyTimeout(1000); |
|
|
37 |
} |
|
|
38 |
} elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) { |
|
|
39 |
$db = new \PDO($this->dsn); |
|
|
40 |
} else { |
|
|
41 |
throw new \RuntimeException('You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.'); |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER)'); |
|
|
45 |
$db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)'); |
|
|
46 |
$db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)'); |
|
|
47 |
$db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)'); |
|
|
48 |
$db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)'); |
|
|
49 |
$db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)'); |
|
|
50 |
|
|
|
51 |
$this->db = $db; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
return $this->db; |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
protected function exec($db, $query, array $args = array()) |
|
|
58 |
{ |
|
|
59 |
if ($db instanceof \SQLite3) { |
|
|
60 |
$stmt = $this->prepareStatement($db, $query); |
|
|
61 |
foreach ($args as $arg => $val) { |
|
|
62 |
$stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT); |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
$res = $stmt->execute(); |
|
|
66 |
if (false === $res) { |
|
|
67 |
throw new \RuntimeException(sprintf('Error executing SQLite query "%s"', $query)); |
|
|
68 |
} |
|
|
69 |
$res->finalize(); |
|
|
70 |
} else { |
|
|
71 |
parent::exec($db, $query, $args); |
|
|
72 |
} |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
protected function fetch($db, $query, array $args = array()) |
|
|
76 |
{ |
|
|
77 |
$return = array(); |
|
|
78 |
|
|
|
79 |
if ($db instanceof \SQLite3) { |
|
|
80 |
$stmt = $this->prepareStatement($db, $query, true); |
|
|
81 |
foreach ($args as $arg => $val) { |
|
|
82 |
$stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT); |
|
|
83 |
} |
|
|
84 |
$res = $stmt->execute(); |
|
|
85 |
while ($row = $res->fetchArray(\SQLITE3_ASSOC)) { |
|
|
86 |
$return[] = $row; |
|
|
87 |
} |
|
|
88 |
$res->finalize(); |
|
|
89 |
$stmt->close(); |
|
|
90 |
} else { |
|
|
91 |
$return = parent::fetch($db, $query, $args); |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
return $return; |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
/** |
|
|
98 |
* {@inheritdoc} |
|
|
99 |
*/ |
|
|
100 |
protected function buildCriteria($ip, $url, $limit) |
|
|
101 |
{ |
|
|
102 |
$criteria = array(); |
|
|
103 |
$args = array(); |
|
|
104 |
|
|
|
105 |
if ($ip = preg_replace('/[^\d\.]/', '', $ip)) { |
|
|
106 |
$criteria[] = 'ip LIKE :ip'; |
|
|
107 |
$args[':ip'] = '%'.$ip.'%'; |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
if ($url) { |
|
|
111 |
$criteria[] = 'url LIKE :url ESCAPE "\"'; |
|
|
112 |
$args[':url'] = '%'.addcslashes($url, '%_\\').'%'; |
|
|
113 |
} |
|
|
114 |
|
|
|
115 |
return array($criteria, $args); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
protected function close($db) |
|
|
119 |
{ |
|
|
120 |
if ($db instanceof \SQLite3) { |
|
|
121 |
$db->close(); |
|
|
122 |
} |
|
|
123 |
} |
|
|
124 |
} |