Completed
Pull Request — master (#2282)
by ྅༻ Ǭɀħ
01:36
created

Logger::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 YOURLS\Database\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;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ydb of type object<YOURLS\Database\YDB> is incompatible with the declared type object<YOURLS\Admin\YOURLS\Database\YDB> of property $ydb.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        /**
30
         * @todo Extend this to allow calling an external logger
31
         */
32
    }
33
34
    /**
35
     * @param string $message
36
     */
37
    public function log($message) {
38
        yourls_do_action('debug_log', $message);
39
        $this->debug_log[] = $message;
40
    }
41
42
    public function get_log() {
43
        return $this->debug_log;
44
    }
45
46
    /**
47
     * Extends \Aura\Sql\Profiler::addProfile() to log queries in our YOURLS logger
48
     *
49
     * @since  1.7.3
50
     * @param  float  $duration     The query duration.
51
     * @param  string $function     The PDO method that made the entry.
52
     * @param  string $statement    The SQL query statement.
53
     * @param  array  $bind_values  The values bound to the statement.
54
     * @return void
55
     */
56
    public function addProfile($duration, $function, $statement, array $bind_values = array() ) {
57
        parent::addProfile($duration, $function, $statement, $bind_values);
58
59
        if($function == 'connect') {
60
            $this->log( sprintf('SQL: CONNECT (%s s)', number_format($duration, 5)) );
61
        } else {
62
            if ( $this->ydb->getAttribute(\PDO::ATTR_EMULATE_PREPARES) && $function !== 'prepare' ) {
63
                $this->log( sprintf('SQL: %s (%s s)', $this->pretty_format($statement, $bind_values), number_format($duration, 5) ) );
64
            }
65
        }
66
    }
67
68
    /**
69
     * Format PDO statement with bind/values replacement
70
     *
71
     * This replaces PDO binds such as 'key_name = :name' with corresponding array values, eg array('name'=>'some value')
72
     * This is merely a cosmetic replacement to allow for readability: the result WILL NOT be valid SQL! (eg no proper quotes)
73
     *
74
     * @since  1.7.3
75
     * @param  string $statement  SQL query with PDO style named placeholders
76
     * @param  array  $values     Optional array of values corresponding to placeholders
77
     * @return string             Readable SQL query with placeholders replaced
78
     */
79
    public function pretty_format($statement, array $values = array() ) {
80
        if (!$values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            return $statement;
82
        }
83
84
        return preg_replace_callback(
85
            '/:([^\s;)]*)/',
86
87
            /**
88
             * @param string $matches
89
             */
90
            function ($matches) use ($values) {
91
                $replacement = isset( $values[$matches[1]] ) ? $values[$matches[1]] : '';
92
                if(is_array($replacement)) {
93
                    $replacement = implode(",", $replacement);
94
                }
95
                return "'$replacement'";
96
            },
97
            $statement
98
        );
99
    }
100
101
}
102