pdo-debug-class

Gesponsorde koppelingen

PHP script bestanden

  1. pdo-debug-class

« Lees de omschrijving en reacties

Simpel voorbeeldje:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
<?php
$pdo
= new Debug_PDO('mysql:host=localhost;dbname=test', 'root', '');
register_shutdown_function(array($pdo, 'dump'));
?>


Advanced voorbeeldje (gebruikt mijn log_socket scriptje http://phphulp.nl/php/scripts/11/1465/):
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
$pdo
= new Debug_PDO('mysql:host=localhost;dbname=test', 'root', '');

/* Alle INSERT INTO queries niet uitvoeren, maar wel als succesvol merken */
$pdo->add_filter(function($snapshot, &$success) {
    if(strstr($snapshot->query, 'INSERT INTO')) {
        $success = true;
        return false;
    }

    
    return true;
});


/* In het antwoord van Debug_PDO_Snapshot::query_as_sql() de argumenten met
 * wat terminal-kleurtjes versieren, zodat ze goed opvallen */

$pdo->add_decorator(function($value) {
    if($value === null) {
        return chr(0x1B) . "[0;37;45mNULL" . chr(0x1B). "[0;32;40m";
    }
else {
        return chr(0x1B) . "[0;37;41m" . $value . chr(0x1B). "[0;32;40m";
    }
});


/* Alle INSERT INTO queries (die dus niet uitgevoerd worden) over de log_socket
 * sturen zodat ze gekleurd en wel in mijn recieve programmaatje verschijnen */

$pdo->add_listener(function($snapshot) {
    if(strstr($snapshot->query, 'INSERT INTO')) {
        log_send((string) $snapshot);
    }
});

?>



De klassen:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php

[code]<?php

class Debug_PDO extends PDO {
    
    public $snapshots = array();
    
    protected $_filters = array();
    
    protected $_listeners = array();
    
    protected $_decorators = array();
    
    public function __construct($uri, $username = null, $password = null) {
        parent::__construct($uri, $username, $password);
        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('Debug_PDO_Statement', array($this)));
    }

    
    public function dump() {
        foreach($this->snapshots as $snapshot) {
            echo "<pre>" . $snapshot . "</pre><br><br>\n";
        }
    }

    
    public function add_listener($listener) {
        if(!is_callable($listener)) {
            throw new InvalidArgumentException('add_listener expects a valid callback');
        }

        
        $this->_listeners[] = $listener;
    }

    
    public function add_filter($filter) {
        if(!is_callable($filter)) {
            throw new InvalidArgumentException('add_filter expects a valid callback');
        }

        
        $this->_filters[] = $filter;
    }

    
    public function add_decorator($decorator) {
        if(!is_callable($decorator)) {
            throw new InvalidArgumentException('add_decorator expects a valid callback');
        }


        $this->_decorators[] = $decorator;
    }

    
    public function should_execute_statement(Debug_PDO_Snapshot $snapshot, &$success) {
        foreach($this->_filters as $filter) {
            if(!call_user_func_array($filter, array($snapshot, &$success))) return false;
        }

        
        return true;
    }

    
    public function add_snapshot(Debug_PDO_Snapshot $snapshot) {
        $this->snapshots[] = $snapshot;

        $snapshot->set_decorators($this->_decorators);
        
        foreach($this->_listeners as $listener) {
            call_user_func($listener, $snapshot, $this);
        }
    }


    /*public function lastInsertId() {
        return $this->snapshots[count($this->snapshots) - 1]->insert_id;
    }*/

}

class Debug_PDO_Statement extends PDOStatement {
    
    protected $_delegate;
    
    protected $_parameters = array();
    
    protected function __construct($delegate) {
        $this->_delegate = $delegate;
    }

    
    public function execute($arguments = null) {
        
        $snapshot = $this->make_snapshot($arguments);
        
        $this->_delegate->add_snapshot($snapshot);
        
        if($this->_delegate->should_execute_statement($snapshot, $success)) {
        
            $snapshot->timer->start();
        
            $success = parent::execute($arguments);
        
            $snapshot->timer->stop();
            
            $snapshot->insert_id = $this->_delegate->lastInsertId();
        
            $snapshot->affected_rows = $this->rowCount();
        }

        
        return $success;
    }

    
    public function bindParam($name, &$value, $data_type = null, $length = null, $driver_options = null) {
        $this->_parameters[$name] = $this->_delegate->quote($value);
        
        return parent::bindParam($name, $value, $data_type, $length, $driver_options);
    }

    
    public function bindValue($name, $value, $data_type = null, $length = null, $driver_options = null) {
        return $this->bindParam($name, $value, $data_type, $length, $driver_options);
    }

    
    public function make_snapshot($arguments) {
        $snapshot = new Debug_PDO_Snapshot();
        $snapshot->query = $this->queryString;
        $snapshot->parameters = $this->_parameters;
        $snapshot->anonymous_parameters = $arguments;
        
        return $snapshot;
    }
}


class Debug_PDO_Snapshot {
    
    public $query;
    
    public $parameters;
    
    public $insert_id;
    
    public $anonymous_parameters;
    
    private $_anonymous_parameter_index = 0;
    
    public $affected_rows;
    
    public $timer;
    
    protected $_decorators = array();
    
    public function __construct() {
        $this->timer = new Debug_PDO_Timer();
    }

        
    public function query_as_sql() {
        $this->_anonymous_parameter_index = 0;
        
        $query = self::_remove_indent(preg_replace_callback('/(?:(\:([a-zA-Z0-9\_]+))|(\?))/', array($this, '_replace_placeholder'), $this->query));
        
        return $query;
    }

    
    public function execution_time() {
        return $this->timer->delta();
    }

    
    public function set_decorators(array $decorators) {
        $this->_decorators = $decorators;
    }

    
    public function __toString() {
        return sprintf("%s\n%f seconds, %d affected rows\n",
            $this->query_as_sql(), $this->execution_time(), $this->affected_rows);
    }

    
    protected function _replace_placeholder($matches) {
        if($matches[0] == '?') {
            $value = $this->anonymous_parameters[$this->_anonymous_parameter_index++];
        }
else {
            $value = $this->parameters[$matches[1]];
        }

        
        foreach($this->_decorators as $decorator) {
            $value = call_user_func($decorator, $value);
        }

        
        return $value;
    }

    
    static protected function _remove_indent($text) {
        $lines = explode("\n", $text);
        
        $smallest_indent = INF;
        
        foreach($lines as $line) {
            
            if(trim($line) == '') continue;
            
            $leading_tabs = 0;
            while($line[$leading_tabs] == "\t") $leading_tabs++;
            
            if($leading_tabs < $smallest_indent) {
                $smallest_indent = $leading_tabs;
            }
        }

        
        foreach($lines as $line_number => $line) {
            if(trim($line) == '') continue;
            
            $lines[$line_number] = substr($line, $smallest_indent);
        }

        
        return implode("\n", $lines);
    }
}


class Debug_PDO_Timer {
    
    protected $_start_time;
    
    protected $_stop_time;
    
    public function start() {
        $this->_start_time = microtime(true);
    }

    
    public function stop() {
        $this->_stop_time = microtime(true);
    }

    
    public function delta() {
        return $this->_start_time && $this->_stop_time ? $this->_stop_time - $this->_start_time : false;
    }
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.