Completed
Push — master ( 1b7d94...99c6f2 )
by Hong
02:08
created

Profiler::startWatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db;
16
17
use Phossa2\Shared\Base\ObjectAbstract;
18
use Phossa2\Db\Traits\DriverAwareTrait;
19
use Phossa2\Db\Interfaces\ProfilerInterface;
20
21
/**
22
 * Profiler
23
 *
24
 * @package Phossa2\Db
25
 * @author  Hong Zhang <[email protected]>
26
 * @see     ObjectAbstract
27
 * @see     ProfilerInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
class Profiler extends ObjectAbstract implements ProfilerInterface
32
{
33
    use DriverAwareTrait;
34
35
    /**
36
     * Current executed SQL
37
     *
38
     * @var    string
39
     * @access protected
40
     */
41
    protected $sql = '';
42
43
    /**
44
     * Parameters cache
45
     *
46
     * @var    array
47
     * @access protected
48
     */
49
    protected $params = [];
50
51
    /**
52
     * Execution time
53
     *
54
     * @var    float
55
     * @access protected
56
     */
57
    protected $execution_time = 0.0;
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function setSql(/*# string */ $sql)
63
    {
64
        // init
65
        $this->sql = $sql;
66
        $this->params = [];
67
        $this->execution_time = 0.0;
68
69
        return $this;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function setParameters(array $parameters)
76
    {
77
        $this->params = $parameters;
78
        return $this;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function getSql()/*# : string */
85
    {
86
        if (!empty($this->params) && $this->hasDriver()) {
87
            return $this->replaceParamInSql();
88
        } else {
89
            return $this->sql;
90
        }
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function startWatch()
97
    {
98
        $this->execution_time = microtime(true);
99
        return $this;
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function stopWatch()
106
    {
107
        $this->execution_time = microtime(true) - $this->execution_time;
108
        return $this;
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function getExecutionTime()/*# : float */
115
    {
116
        return $this->execution_time;
117
    }
118
119
    /**
120
     * Replace params in the SQL
121
     *
122
     * @return string
123
     * @access protected
124
     */
125
    protected function replaceParamInSql()/*# : string */
126
    {
127
        $count = 0;
128
        $params = $this->params;
129
        return preg_replace_callback(
130
            '/\?|\:\w+/', function($m) use ($count, $params) {
131
                if ('?' === $m[0]) {
132
                    $res = $params[$count++];
133
                } else {
134
                    $res = isset($params[$m[0]]) ? $params[$m[0]] :
135
                        $params[substr($m[0], 1)];
136
                }
137
                return $this->getDriver()->quote($res);
138
            }, $this->sql);
139
    }
140
}
141