SqliteProfilerStorage::fetch()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 2
nop 3
dl 0
loc 20
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
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 Sitetheory\Bundle\ProfilerStorageBundle\Profiler;
13
14
/**
15
 * SqliteProfilerStorage stores profiling information in a SQLite database.
16
 *
17
 * Class SqliteProfilerStorage
18
 *
19
 * @author Fabien Potencier <[email protected]>
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 (0 !== strpos($this->dsn, 'sqlite')) {
30
                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));
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('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
45
            $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)');
46
            $db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
47
            $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
48
            $db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');
49
            $db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)');
50
            $db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)');
51
            $db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)');
52
53
            $this->db = $db;
54
        }
55
56
        return $this->db;
57
    }
58
59
    protected function exec($db, $query, array $args = array())
60
    {
61
        if ($db instanceof \SQLite3) {
62
            $stmt = $this->prepareStatement($db, $query);
63
            foreach ($args as $arg => $val) {
64
                $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
65
            }
66
67
            $res = $stmt->execute();
68
            if (false === $res) {
69
                throw new \RuntimeException(sprintf('Error executing SQLite query "%s"', $query));
70
            }
71
            $res->finalize();
72
        } else {
73
            parent::exec($db, $query, $args);
74
        }
75
    }
76
77
    protected function fetch($db, $query, array $args = array())
78
    {
79
        $return = array();
80
81
        if ($db instanceof \SQLite3) {
82
            $stmt = $this->prepareStatement($db, $query);
83
            foreach ($args as $arg => $val) {
84
                $stmt->bindValue($arg, $val, is_int($val) ? \SQLITE3_INTEGER : \SQLITE3_TEXT);
85
            }
86
            $res = $stmt->execute();
87
            while ($row = $res->fetchArray(\SQLITE3_ASSOC)) {
88
                $return[] = $row;
89
            }
90
            $res->finalize();
91
            $stmt->close();
92
        } else {
93
            $return = parent::fetch($db, $query, $args);
94
        }
95
96
        return $return;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    protected function buildCriteria($ip, $url, $start, $end, $limit, $method)
103
    {
104
        $criteria = array();
105
        $args = array();
106
107
        if ($ip = preg_replace('/[^\d\.]/', '', $ip)) {
108
            $criteria[] = 'ip LIKE :ip';
109
            $args[':ip'] = '%'.$ip.'%';
110
        }
111
112
        if ($url) {
113
            $criteria[] = 'url LIKE :url ESCAPE "\"';
114
            $args[':url'] = '%'.addcslashes($url, '%_\\').'%';
115
        }
116
117
        if ($method) {
118
            $criteria[] = 'method = :method';
119
            $args[':method'] = $method;
120
        }
121
122
        if (!empty($start)) {
123
            $criteria[] = 'time >= :start';
124
            $args[':start'] = $start;
125
        }
126
127
        if (!empty($end)) {
128
            $criteria[] = 'time <= :end';
129
            $args[':end'] = $end;
130
        }
131
132
        return array($criteria, $args);
133
    }
134
135
    protected function close($db)
136
    {
137
        if ($db instanceof \SQLite3) {
138
            $db->close();
139
        }
140
    }
141
}
142