Yaffs site version 1.1
[yaffs-website] / vendor / symfony / http-kernel / Profiler / SqliteProfilerStorage.php
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 @trigger_error('The '.__NAMESPACE__.'\SqliteProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
15
16 /**
17  * SqliteProfilerStorage stores profiling information in a SQLite database.
18  *
19  * @author Fabien Potencier <fabien@symfony.com>
20  *
21  * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
22  *             Use {@link FileProfilerStorage} instead.
23  */
24 class SqliteProfilerStorage extends PdoProfilerStorage
25 {
26     /**
27      * @throws \RuntimeException When neither of SQLite3 or PDO_SQLite extension is enabled
28      */
29     protected function initDb()
30     {
31         if (null === $this->db || $this->db instanceof \SQLite3) {
32             if (0 !== strpos($this->dsn, 'sqlite')) {
33                 throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Sqlite with an invalid dsn "%s". The expected format is "sqlite:/path/to/the/db/file".', $this->dsn));
34             }
35             if (class_exists('SQLite3')) {
36                 $db = new \SQLite3(substr($this->dsn, 7, strlen($this->dsn)), \SQLITE3_OPEN_READWRITE | \SQLITE3_OPEN_CREATE);
37                 if (method_exists($db, 'busyTimeout')) {
38                     // busyTimeout only exists for PHP >= 5.3.3
39                     $db->busyTimeout(1000);
40                 }
41             } elseif (class_exists('PDO') && in_array('sqlite', \PDO::getAvailableDrivers(), true)) {
42                 $db = new \PDO($this->dsn);
43             } else {
44                 throw new \RuntimeException('You need to enable either the SQLite3 or PDO_SQLite extension for the profiler to run properly.');
45             }
46
47             $db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
48             $db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER, status_code INTEGER)');
49             $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
50             $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
51             $db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');
52             $db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)');
53             $db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)');
54             $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)');
55
56             $this->db = $db;
57         }
58
59         return $this->db;
60     }
61
62     protected function exec($db, $query, array $args = array())
63     {
64         if ($db instanceof \SQLite3) {
65             $stmt = $this->prepareStatement($db, $query);
66             foreach ($args as $arg => $val) {
67                 $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
68             }
69
70             $res = $stmt->execute();
71             if (false === $res) {
72                 throw new \RuntimeException(sprintf('Error executing SQLite query "%s"', $query));
73             }
74             $res->finalize();
75         } else {
76             parent::exec($db, $query, $args);
77         }
78     }
79
80     protected function fetch($db, $query, array $args = array())
81     {
82         $return = array();
83
84         if ($db instanceof \SQLite3) {
85             $stmt = $this->prepareStatement($db, $query);
86             foreach ($args as $arg => $val) {
87                 $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
88             }
89             $res = $stmt->execute();
90             while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
91                 $return[] = $row;
92             }
93             $res->finalize();
94             $stmt->close();
95         } else {
96             $return = parent::fetch($db, $query, $args);
97         }
98
99         return $return;
100     }
101
102     /**
103      * {@inheritdoc}
104      */
105     protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
106     {
107         $criteria = array();
108         $args = array();
109
110         if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
111             $criteria[] = 'ip LIKE :ip';
112             $args[':ip'] = '%'.$ip.'%';
113         }
114
115         if ($url) {
116             $criteria[] = 'url LIKE :url ESCAPE "\"';
117             $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
118         }
119
120         if ($method) {
121             $criteria[] = 'method = :method';
122             $args[':method'] = $method;
123         }
124
125         if (!empty($start)) {
126             $criteria[] = 'time >= :start';
127             $args[':start'] = $start;
128         }
129
130         if (!empty($end)) {
131             $criteria[] = 'time <= :end';
132             $args[':end'] = $end;
133         }
134
135         return array($criteria, $args);
136     }
137
138     protected function close($db)
139     {
140         if ($db instanceof \SQLite3) {
141             $db->close();
142         }
143     }
144 }