1 | <?php |
||
2 | |||
3 | /** |
||
4 | * YOURLS simple logger |
||
5 | * |
||
6 | * @since 1.7.3 |
||
7 | */ |
||
8 | |||
9 | namespace YOURLS\Admin; |
||
10 | |||
11 | use YOURLS\Database\YDB; |
||
12 | |||
13 | class Logger extends \Aura\Sql\Profiler { |
||
14 | |||
15 | /** |
||
16 | * the YDB instance |
||
17 | * @var YDB |
||
18 | */ |
||
19 | protected $ydb; |
||
20 | |||
21 | /** |
||
22 | * Debug log where messages are stored |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $debug_log = array(); |
||
26 | |||
27 | public function __construct(YDB $ydb) { |
||
28 | $this->ydb = $ydb; |
||
29 | /** |
||
30 | * @todo Extend this to allow calling an external logger |
||
31 | */ |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @param string $message |
||
36 | */ |
||
37 | 40 | public function log($message) { |
|
38 | 40 | $this->debug_log[] = $message; |
|
39 | 40 | } |
|
40 | |||
41 | 1 | public function get_log() { |
|
42 | 1 | return $this->debug_log; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Extends \Aura\Sql\Profiler::addProfile() to log queries in our YOURLS logger |
||
47 | * |
||
48 | * @since 1.7.3 |
||
49 | * @param float $duration The query duration. |
||
50 | * @param string $function The PDO method that made the entry. |
||
51 | * @param string $statement The SQL query statement. |
||
52 | * @param array $bind_values The values bound to the statement. |
||
53 | * @return void |
||
54 | */ |
||
55 | 36 | public function addProfile($duration, $function, $statement, array $bind_values = array() ) { |
|
56 | 36 | parent::addProfile($duration, $function, $statement, $bind_values); |
|
57 | |||
58 | 36 | if($function == 'connect') { |
|
59 | $this->log( sprintf('SQL: CONNECT (%s s)', number_format($duration, 5)) ); |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | // If we are emulating prepare, don't log 'prepare' statement, as they are not actual queries sent to the server |
||
64 | // See YDB::get_queries() for details |
||
65 | 36 | if ($this->ydb->get_emulate_state() && $function !== 'prepare') { |
|
66 | 36 | $this->log( sprintf('SQL: %s (%s s)', $this->pretty_format($statement, $bind_values), number_format($duration, 5) ) ); |
|
67 | } |
||
68 | 36 | } |
|
69 | |||
70 | /** |
||
71 | * Format PDO statement with bind/values replacement |
||
72 | * |
||
73 | * This replaces PDO binds such as 'key_name = :name' with corresponding array values, eg array('name'=>'some value') |
||
74 | * This is merely a cosmetic replacement to allow for readability: the result WILL NOT be valid SQL! (eg no proper quotes) |
||
75 | * |
||
76 | * @since 1.7.3 |
||
77 | * @param string $statement SQL query with PDO style named placeholders |
||
78 | * @param array $values Optional array of values corresponding to placeholders |
||
79 | * @return string Readable SQL query with placeholders replaced |
||
80 | */ |
||
81 | 36 | public function pretty_format($statement, array $values = array() ) { |
|
82 | 36 | if (!$values) { |
|
83 | 3 | return $statement; |
|
84 | } |
||
85 | |||
86 | 34 | return preg_replace_callback( |
|
87 | 34 | '/:([^\s;)]*)/', |
|
88 | |||
89 | /** |
||
90 | * @param string $matches |
||
91 | */ |
||
92 | function ($matches) use ($values) { |
||
93 | 34 | $replacement = isset( $values[$matches[1]] ) ? $values[$matches[1]] : ''; |
|
94 | 34 | if(is_array($replacement)) { |
|
95 | $replacement = implode(",", $replacement); |
||
0 ignored issues
–
show
|
|||
96 | } |
||
97 | 34 | return "'$replacement'"; |
|
98 | 34 | }, |
|
99 | $statement |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | } |
||
104 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.