1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mnabialek\LaravelSqlLogger; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
7
|
|
|
use Mnabialek\LaravelSqlLogger\Objects\SqlQuery; |
8
|
|
|
|
9
|
|
|
class Formatter |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Application |
13
|
|
|
*/ |
14
|
|
|
private $app; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Config |
18
|
|
|
*/ |
19
|
|
|
private $config; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Formatter constructor. |
23
|
|
|
* |
24
|
|
|
* @param Application $app |
25
|
|
|
* @param Config $config |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Application $app, Config $config) |
28
|
|
|
{ |
29
|
|
|
$this->app = $app; |
30
|
|
|
$this->config = $config; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get formatted line. |
35
|
|
|
* |
36
|
|
|
* @param SqlQuery $query |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getLine(SqlQuery $query) |
41
|
|
|
{ |
42
|
|
|
return '/* ' . $this->originLine() . PHP_EOL . |
43
|
|
|
' ' . $this->querySummaryLine($query) . ' */' . PHP_EOL . |
44
|
|
|
$this->queryLine($query) . PHP_EOL . |
45
|
|
|
$this->separatorLine() . PHP_EOL; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Format time. |
50
|
|
|
* |
51
|
|
|
* @param float $time |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
protected function time($time) |
56
|
|
|
{ |
57
|
|
|
return $this->config->useSeconds() ? ($time / 1000.0) . 's' : $time . 'ms'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get origin line. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected function originLine() |
66
|
|
|
{ |
67
|
|
|
return 'Origin ' . ($this->app->runningInConsole() |
68
|
|
|
? '(console): ' . $this->getArtisanLine() |
69
|
|
|
: '(request): ' . $this->getRequestLine()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get query summary line. |
74
|
|
|
* |
75
|
|
|
* @param SqlQuery $query |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function querySummaryLine(SqlQuery $query) |
80
|
|
|
{ |
81
|
|
|
return 'Query ' . $query->number() . ' - ' . Carbon::now()->toDateTimeString() . ' [' . |
82
|
|
|
$this->time($query->time()) . ']'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get query line. |
87
|
|
|
* |
88
|
|
|
* @param SqlQuery $query |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
protected function queryLine(SqlQuery $query) |
93
|
|
|
{ |
94
|
|
|
return $query->get() . ';'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get Artisan line. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function getArtisanLine() |
103
|
|
|
{ |
104
|
|
|
$command = $this->app['request']->server('argv', []); |
105
|
|
|
|
106
|
|
|
if (is_array($command)) { |
107
|
|
|
$command = implode(' ', $command); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $command; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get request line. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function getRequestLine() |
119
|
|
|
{ |
120
|
|
|
return $this->app['request']->method() . ' ' . $this->app['request']->fullUrl(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get separator line. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function separatorLine() |
129
|
|
|
{ |
130
|
|
|
return '/*' . str_repeat('=', 50) . '*/'; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|