Skip to content

Commit 75c100f

Browse files
committed
Test Clockwork output
1 parent 6c02097 commit 75c100f

File tree

5 files changed

+264
-0
lines changed

5 files changed

+264
-0
lines changed

config/debugbar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@
6161

6262
'capture_ajax' => true,
6363

64+
/*
65+
|--------------------------------------------------------------------------
66+
| Clockwork integration
67+
|--------------------------------------------------------------------------
68+
|
69+
| The Debugbar can emulate the Clockwork headers, so you can use the Chrome
70+
| Extension, without the server-side code. It uses Debugbar collectors instead.
71+
|
72+
*/
73+
'clockwork' => false,
74+
6475
/*
6576
|--------------------------------------------------------------------------
6677
| DataCollectors

src/Controllers/OpenHandlerController.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Barryvdh\Debugbar\Controllers;
22

3+
use Barryvdh\Debugbar\Support\Clockwork\Converter;
34
use DebugBar\OpenHandler;
45
use Illuminate\Http\Response;
56

@@ -24,4 +25,35 @@ public function handle()
2425
)
2526
);
2627
}
28+
29+
/**
30+
* Return Clockwork output
31+
*
32+
* @param $id
33+
* @return mixed
34+
* @throws \DebugBar\DebugBarException
35+
*/
36+
public function clockwork($id)
37+
{
38+
$request = [
39+
'op' => 'get',
40+
'id' => $id,
41+
];
42+
43+
$debugbar = $this->debugbar;
44+
45+
if (!$debugbar->isEnabled()) {
46+
$this->app->abort('500', 'Debugbar is not enabled');
47+
}
48+
49+
$openHandler = new OpenHandler($debugbar);
50+
51+
$data = $openHandler->handle($request, false, false);
52+
53+
// Convert to Clockwork
54+
$converter = new Converter();
55+
$output = $converter->convert(json_decode($data, true));
56+
57+
return response()->json($output);
58+
}
2759
}

src/ServiceProvider.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function boot()
7373
'uses' => 'OpenHandlerController@handle',
7474
'as' => 'debugbar.openhandler',
7575
]);
76+
77+
$router->get('clockwork/{id?}', [
78+
'uses' => 'OpenHandlerController@clockwork',
79+
'as' => 'debugbar.clockwork',
80+
]);
7681

7782
$router->get('assets/stylesheets', [
7883
'uses' => 'AssetController@css',
@@ -102,6 +107,11 @@ public function boot()
102107
$debugbar->boot();
103108

104109
$kernel = $this->app['Illuminate\Contracts\Http\Kernel'];
110+
111+
if ($this->app['config']->get('debugbar.clockwork')) {
112+
$kernel->pushMiddleware('Barryvdh\Debugbar\Support\Clockwork\Middleware');
113+
}
114+
105115
$kernel->pushMiddleware('Barryvdh\Debugbar\Middleware\Debugbar');
106116
}
107117

src/Support/Clockwork/Converter.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php namespace Barryvdh\Debugbar\Support\Clockwork;
2+
3+
class Converter {
4+
5+
/**
6+
* Convert the phpdebugbar data to Clockwork format.
7+
*
8+
* @param array $data
9+
* @return array
10+
*/
11+
public function convert($data)
12+
{
13+
$meta = $data['__meta'];
14+
15+
// Default output
16+
$output = [
17+
'id' => $meta['id'],
18+
'method' => $meta['method'],
19+
'uri' => $meta['uri'],
20+
'time' => $meta['utime'],
21+
'headers' => [],
22+
'cookies' => [],
23+
'emailsData' => [],
24+
'getData' => [],
25+
'headers' => [],
26+
'log' => [],
27+
'postData' => [],
28+
'sessionData' => [],
29+
'timelineData' => [],
30+
'viewsData' => [],
31+
'controller' => null,
32+
'responseTime' => null,
33+
'responseStatus' => null,
34+
'responseDuration' => 0,
35+
'log' => [],
36+
];
37+
38+
39+
if (isset($data['time'])) {
40+
$time = $data['time'];
41+
$output['responseTime'] = $time['end'];
42+
$output['responseDuration'] = $time['duration'] * 1000;
43+
}
44+
45+
if (isset($data['route'])) {
46+
$route = $data['route'];
47+
48+
if (isset($route['uses'])) {
49+
$output['controller'] = $route['uses'];
50+
}
51+
}
52+
53+
if (isset($data['messages'])) {
54+
$messages = $data['messages'];
55+
$output['messages'] = $messages['messages'];
56+
}
57+
58+
if (isset($data['request'])) {
59+
$request = $data['request'];
60+
61+
$output['responseStatus'] = $request['status_code'];
62+
}
63+
64+
if (isset($data['queries'])) {
65+
$queries = $data['queries'];
66+
foreach($queries['statements'] as $statement){
67+
$output['databaseQueries'][] = [
68+
'query' => $statement['sql'],
69+
'bindings' => $statement['params'],
70+
'time' => $statement['duration'] * 1000,
71+
'connection' => $statement['connection']
72+
];
73+
}
74+
75+
$output['databaseDuration'] = $queries['accumulated_duration'] * 1000;
76+
77+
}
78+
79+
return $output;
80+
}
81+
82+
protected function unvar_dump($str) {
83+
$str = str_replace($str, '\n', "\n");
84+
if (strpos($str, "\n") === false) {
85+
//Add new lines:
86+
$regex = array(
87+
'#(\\[.*?\\]=>)#',
88+
'#(string\\(|int\\(|float\\(|array\\(|NULL|object\\(|})#',
89+
);
90+
$str = preg_replace($regex, "\n\\1", $str);
91+
$str = trim($str);
92+
}
93+
$regex = array(
94+
'#^\\040*NULL\\040*$#m',
95+
'#^\\s*array\\((.*?)\\)\\s*{\\s*$#m',
96+
'#^\\s*string\\((.*?)\\)\\s*(.*?)$#m',
97+
'#^\\s*int\\((.*?)\\)\\s*$#m',
98+
'#^\\s*bool\\(true\\)\\s*$#m',
99+
'#^\\s*bool\\(false\\)\\s*$#m',
100+
'#^\\s*float\\((.*?)\\)\\s*$#m',
101+
'#^\\s*\[(\\d+)\\]\\s*=>\\s*$#m',
102+
'#\\s*?\\r?\\n\\s*#m',
103+
);
104+
$replace = array(
105+
'N',
106+
'a:\\1:{',
107+
's:\\1:\\2',
108+
'i:\\1',
109+
'b:1',
110+
'b:0',
111+
'd:\\1',
112+
'i:\\1',
113+
';'
114+
);
115+
$serialized = preg_replace($regex, $replace, $str);
116+
$func = create_function(
117+
'$match',
118+
'return "s:".strlen($match[1]).":\\"".$match[1]."\\"";'
119+
);
120+
$serialized = preg_replace_callback(
121+
'#\\s*\\["(.*?)"\\]\\s*=>#',
122+
$func,
123+
$serialized
124+
);
125+
$func = create_function(
126+
'$match',
127+
'return "O:".strlen($match[1]).":\\"".$match[1]."\\":".$match[2].":{";'
128+
);
129+
$serialized = preg_replace_callback(
130+
'#object\\((.*?)\\).*?\\((\\d+)\\)\\s*{\\s*;#',
131+
$func,
132+
$serialized
133+
);
134+
$serialized = preg_replace(
135+
array('#};#', '#{;#'),
136+
array('}', '{'),
137+
$serialized
138+
);
139+
140+
return unserialize($serialized);
141+
}
142+
}

src/Support/Clockwork/Middleware.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php namespace Barryvdh\Debugbar\Support\Clockwork;
2+
3+
use Closure;
4+
use Illuminate\Contracts\Debug\ExceptionHandler;
5+
use Illuminate\Contracts\Foundation\Application;
6+
7+
class Middleware {
8+
9+
/**
10+
* The Laravel Application
11+
*
12+
* @var Application
13+
*/
14+
protected $app;
15+
16+
/**
17+
* The Exception Handler
18+
*
19+
* @var ExceptionHandler
20+
*/
21+
protected $exceptionHandler;
22+
23+
/**
24+
* Create a new middleware instance.
25+
*
26+
* @param Application $app
27+
* @param ExceptionHandler $exceptionHandler
28+
*/
29+
public function __construct(Application $app, ExceptionHandler $exceptionHandler)
30+
{
31+
$this->app = $app;
32+
$this->exceptionHandler = $exceptionHandler;
33+
}
34+
35+
/**
36+
* Handle an incoming request.
37+
*
38+
* @param \Illuminate\Http\Request $request
39+
* @param \Closure $next
40+
* @return mixed
41+
*/
42+
public function handle($request, Closure $next)
43+
{
44+
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugbar */
45+
$debugbar = $this->app['debugbar'];
46+
47+
try {
48+
/** @var \Illuminate\Http\Response $response */
49+
$response = $next($request);
50+
} catch (\Exception $e) {
51+
$debugbar->addException($e);
52+
53+
$this->exceptionHandler->report($e);
54+
$response = $this->exceptionHandler->render($request, $e);
55+
}
56+
57+
if ($request->is('_debugbar*') || $request->is('*clockwork*')) {
58+
return $response;
59+
}
60+
61+
$response->headers->set('X-Clockwork-Id', $debugbar->getCurrentRequestId(), true);
62+
$response->headers->set('X-Clockwork-Version', 1, true);
63+
$response->headers->set('X-Clockwork-Path','/_debugbar/clockwork/', true);
64+
65+
66+
return $debugbar->modifyResponse($request, $response);
67+
68+
}
69+
}

0 commit comments

Comments
 (0)