aboutsummaryrefslogtreecommitdiff
path: root/includes/vendor/aura/sql/src/Profiler.php
blob: 93a1c9e36e7ce4b6f66a12789cfa208819bf2fac (plain) (blame)
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
<?php
/**
 *
 * This file is part of Aura for PHP.
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 *
 */
namespace Aura\Sql;

/**
 *
 * Retains query profiles.
 *
 * @package Aura.Sql
 *
 */
class Profiler implements ProfilerInterface
{
    /**
     *
     * Is the profiler active?
     *
     * @var bool
     *
     */
    protected $active = false;

    /**
     *
     * Retained profiles.
     *
     * @var array
     *
     */
    protected $profiles = array();

    protected static $count = 0;

    /**
     *
     * Turns the profiler on and off.
     *
     * @param bool $active True to turn on, false to turn off.
     *
     * @return null
     *
     */
    public function setActive($active)
    {
        $this->active = (bool) $active;
    }

    /**
     *
     * Is the profiler active?
     *
     * @return bool
     *
     */
    public function isActive()
    {
        return (bool) $this->active;
    }

    /**
     *
     * Adds a profile entry.
     *
     * @param float $duration The query duration.
     *
     * @param string $function The PDO method that made the entry.
     *
     * @param string $statement The SQL query statement.
     *
     * @param array $bind_values The values bound to the statement.
     *
     * @return null
     *
     */
    public function addProfile(
        $duration,
        $function,
        $statement,
        array $bind_values = array()
    ) {
        if (! $this->isActive()) {
            return;
        }

        $e = new \Exception;

        // this allows for multiple profilers getting inter-sorted later
        $k = self::$count ++;

        $this->profiles[$k] = array(
            'duration'    => $duration,
            'function'    => $function,
            'statement'   => $statement,
            'bind_values' => $bind_values,
            'trace'       => $e->getTraceAsString(),
        );
    }

    /**
     *
     * Returns all the profile entries.
     *
     * @return array
     *
     */
    public function getProfiles()
    {
        return $this->profiles;
    }

    /**
     *
     * Reset all the profiles
     *
     * @return null
     *
     */
    public function resetProfiles()
    {
        $this->profiles = array();
        self::$count = 0;
    }
}