Skip to content

Commit 2e2fc6d

Browse files
author
Xavier Barbosa
committed
Added basic package setup and configuration and command to display laravel debug bar debug information.
0 parents  commit 2e2fc6d

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
composer.lock
3+
vendor

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "madewithlove/laravel-debug-console",
3+
"license": "MIT",
4+
"description": "Works as a console view for laravel debug bar.",
5+
"authors": [
6+
{
7+
"name": "Xavier Barbosa",
8+
"email": "xavier@madewithlove.be"
9+
}
10+
],
11+
"require": {
12+
"illuminate/console": ">5.3",
13+
"barryvdh/laravel-debugbar": ">2.4"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Madewithlove\\LaravelDebugConsole\\": "src/"
18+
}
19+
},
20+
"extra": {
21+
"laravel": {
22+
"providers": [
23+
"Madewithlove\\LaravelDebugConsole\\ServiceProvider"
24+
]
25+
}
26+
},
27+
"minimum-stability": "stable"
28+
}

src/Console/Debug.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace Madewithlove\LaravelDebugConsole\Console;
4+
5+
use DebugBar\DebugBar;
6+
use Illuminate\Console\Command;
7+
use InvalidArgumentException;
8+
9+
class Debug extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'app:debug';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Collects query data and stats.';
24+
25+
/**
26+
* Execute the console command.
27+
*/
28+
public function handle()
29+
{
30+
/** @var DebugBar $debug */
31+
$debug = app('debugbar');
32+
$debug->boot();
33+
34+
// Watch files every second
35+
$profileData = [];
36+
while (true) {
37+
try {
38+
$latestFile = $debug->getStorage()->find([], 1);
39+
} catch (InvalidArgumentException $exception) {
40+
$this->error($exception->getMessage());
41+
42+
return;
43+
}
44+
45+
$id = array_get($latestFile, '0.id');
46+
47+
// No file found so continue
48+
if (empty($id)) {
49+
sleep(1);
50+
51+
continue;
52+
}
53+
54+
// Latest file is the same do not render
55+
if (array_get($profileData, '__meta.id') === $id) {
56+
sleep(1);
57+
58+
continue;
59+
}
60+
61+
$profileData = $debug->getStorage()->get($id);
62+
63+
system('clear');
64+
$this->info('Latest Request Information:');
65+
$this->renderMetaData($profileData);
66+
$this->renderQueryData($profileData);
67+
68+
sleep(1);
69+
}
70+
}
71+
72+
/**
73+
* @param array $data
74+
*/
75+
public function renderMetaData(array $data)
76+
{
77+
$this->table([
78+
'Date and Time',
79+
'Total Memory',
80+
'Total Time',
81+
], [
82+
[
83+
array_get($data, '__meta.datetime'),
84+
array_get($data, 'memory.peak_usage_str'),
85+
array_get($data, 'time.duration_str')
86+
]
87+
]);
88+
}
89+
90+
/**
91+
* @param array $data
92+
*/
93+
public function renderQueryData(array $data)
94+
{
95+
$queries = collect(array_get($data, 'queries.statements', []));
96+
97+
// Retrieve grouped sql statements
98+
$rows = $queries
99+
->map(function ($query, $index) {
100+
return [
101+
$index,
102+
array_get($query, 'sql'),
103+
array_get($query, 'duration_str'),
104+
];
105+
})
106+
->all();
107+
108+
$this->info('Queries Executed:');
109+
$this->table([
110+
'N.',
111+
'SQL',
112+
'Duration',
113+
], $rows);
114+
115+
$this->info('Queries Summary:');
116+
$this->table([
117+
'Total Number of Queries',
118+
'Total Execution Time',
119+
], [
120+
[
121+
array_get($data, 'queries.nb_statements'),
122+
array_get($data, 'queries.accumulated_duration_str')
123+
]
124+
]);
125+
}
126+
}

src/ServiceProvider.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Madewithlove\LaravelDebugConsole;
4+
5+
use Madewithlove\LaravelDebugConsole\Console\Debug;
6+
use \Illuminate\Support\ServiceProvider as BaseServiceProvider;
7+
8+
class ServiceProvider extends BaseServiceProvider
9+
{
10+
/**
11+
* Bootstrap the application services.
12+
*/
13+
public function boot()
14+
{
15+
if ($this->app->runningInConsole()) {
16+
$this->commands([
17+
Debug::class,
18+
]);
19+
}
20+
}
21+
22+
/**
23+
* Register the application services.
24+
*/
25+
public function register()
26+
{
27+
$this->app->register(\Barryvdh\Debugbar\ServiceProvider::class);
28+
}
29+
}

0 commit comments

Comments
 (0)