Skip to content

Commit 149ab72

Browse files
committed
Merge branch 'master' into 6.x
2 parents 3a7c950 + ab2aa79 commit 149ab72

File tree

6 files changed

+213
-99
lines changed

6 files changed

+213
-99
lines changed

src/DbProfilerServiceProvider.php

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,78 @@
22

33
namespace Illuminated\Database;
44

5+
use Illuminate\Database\Events\QueryExecuted;
56
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Request;
68
use Illuminate\Support\ServiceProvider;
7-
use Illuminate\Database\Events\QueryExecuted;
9+
use Illuminate\Support\Str;
810

911
class DbProfilerServiceProvider extends ServiceProvider
1012
{
11-
private static $counter;
12-
13-
private static function tickCounter()
14-
{
15-
return self::$counter++;
16-
}
13+
/**
14+
* The query counter.
15+
*
16+
* @var int
17+
*/
18+
private $counter = 1;
1719

20+
/**
21+
* Boot the service provider.
22+
*
23+
* @return void
24+
*
25+
* @noinspection ForgottenDebugOutputInspection
26+
*/
1827
public function boot()
1928
{
2029
if (!$this->isEnabled()) {
2130
return;
2231
}
2332

24-
self::$counter = 1;
25-
2633
DB::listen(function (QueryExecuted $query) {
27-
$i = self::tickCounter();
28-
$sql = $this->applyBindings($query->sql, $query->bindings);
29-
dump("[$i]: {$sql}; ({$query->time} ms)");
34+
$i = $this->counter++;
35+
$sql = $this->applyQueryBindings($query->sql, $query->bindings);
36+
$time = $query->time;
37+
dump("[{$i}]: {$sql}; ({$time} ms)");
3038
});
3139
}
3240

41+
/**
42+
* Check whether database profiling is enabled or not.
43+
*
44+
* @return bool
45+
*/
3346
private function isEnabled()
3447
{
35-
if (!config('db-profiler.force') && !$this->app->isLocal()) {
48+
if (!$this->app->isLocal() && !config('db-profiler.force')) {
3649
return false;
3750
}
3851

39-
if ($this->app->runningInConsole()) {
40-
return in_array('-vvv', $_SERVER['argv']);
41-
}
42-
43-
return request()->exists('vvv');
52+
return $this->app->runningInConsole()
53+
? collect($_SERVER['argv'])->contains('-vvv')
54+
: Request::exists('vvv');
4455
}
4556

46-
private function applyBindings($sql, array $bindings)
57+
/**
58+
* Apply query bindings to the given SQL query.
59+
*
60+
* @param string $sql
61+
* @param array $bindings
62+
* @return string
63+
*/
64+
private function applyQueryBindings(string $sql, array $bindings)
4765
{
48-
if (empty($bindings)) {
49-
return $sql;
50-
}
51-
52-
foreach ($bindings as $binding) {
66+
$bindings = collect($bindings)->map(function ($binding) {
5367
switch (gettype($binding)) {
5468
case 'boolean':
55-
$binding = (int) $binding;
56-
break;
57-
69+
return (int) $binding;
5870
case 'string':
59-
$binding = "'{$binding}'";
60-
break;
71+
return "'{$binding}'";
72+
default:
73+
return $binding;
6174
}
75+
})->toArray();
6276

63-
$sql = preg_replace('/\?/', $binding, $sql, 1);
64-
}
65-
66-
return $sql;
77+
return Str::replaceArray('?', $bindings, $sql);
6778
}
6879
}

tests/ConsoleProfilingTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,50 @@
44

55
class ConsoleProfilingTest extends TestCase
66
{
7+
/**
8+
* Define whether the app is running in console or not.
9+
*
10+
* @return bool
11+
*/
712
protected function runningInConsole()
813
{
914
return true;
1015
}
1116

17+
/**
18+
* Emulate the "vvv" flag set.
19+
*
20+
* @return $this
21+
*/
1222
protected function withVvv()
1323
{
1424
$_SERVER['argv']['-vvv'] = true;
25+
1526
return $this;
1627
}
1728

1829
/** @test */
1930
public function it_is_disabled_if_environment_is_not_local()
2031
{
2132
$this->notLocal()->boot();
33+
2234
$this->assertDbProfilerNotActivated();
2335
}
2436

2537
/** @test */
2638
public function it_is_disabled_if_environment_is_local_but_there_is_no_vvv_option()
2739
{
2840
$this->local()->boot();
41+
2942
$this->assertDbProfilerNotActivated();
3043
}
3144

3245
/** @test */
3346
public function it_is_enabled_if_environment_is_local_and_there_is_vvv_option()
3447
{
3548
$this->local()->withVvv()->boot();
36-
$this->assertDbProfilerActivated();
37-
}
3849

39-
/** @test */
40-
public function it_dumps_all_database_queries_with_applied_bindings()
41-
{
42-
$this->local()->withVvv()->boot();
50+
$this->assertDbProfilerActivated();
4351
$this->assertDbQueriesDumped();
4452
}
4553
}

tests/HttpProfilingTest.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,54 @@
22

33
namespace Illuminated\Database\Tests;
44

5+
use Illuminate\Support\Facades\Request;
6+
57
class HttpProfilingTest extends TestCase
68
{
9+
/**
10+
* Define whether the app is running in console or not.
11+
*
12+
* @return bool
13+
*/
714
protected function runningInConsole()
815
{
916
return false;
1017
}
1118

19+
/**
20+
* Emulate the "vvv" flag set.
21+
*
22+
* @return $this
23+
*/
1224
protected function withVvv()
1325
{
14-
request()['vvv'] = true;
26+
Request::merge(['vvv' => true]);
27+
1528
return $this;
1629
}
1730

1831
/** @test */
1932
public function it_is_disabled_if_environment_is_not_local()
2033
{
2134
$this->notLocal()->boot();
35+
2236
$this->assertDbProfilerNotActivated();
2337
}
2438

2539
/** @test */
2640
public function it_is_disabled_if_environment_is_local_but_there_is_no_vvv_request_param()
2741
{
2842
$this->local()->boot();
43+
2944
$this->assertDbProfilerNotActivated();
3045
}
3146

3247
/** @test */
3348
public function it_is_enabled_if_environment_is_local_and_there_is_vvv_request_param()
3449
{
3550
$this->local()->withVvv()->boot();
36-
$this->assertDbProfilerActivated();
37-
}
3851

39-
/** @test */
40-
public function it_dumps_all_database_queries_with_applied_bindings()
41-
{
42-
$this->local()->withVvv()->boot();
52+
$this->assertDbProfilerActivated();
4353
$this->assertDbQueriesDumped();
4454
}
4555
}

0 commit comments

Comments
 (0)