Updated the SPA profiler as Laravel 5 middleware.

This commit is contained in:
Peter Deltchev 2015-09-20 01:47:24 -07:00
parent 7d047a41e4
commit 97681212a4
6 changed files with 73 additions and 9 deletions

View file

@ -194,4 +194,4 @@ class ArtistsController extends ApiControllerBase
return Response::json(["artists" => $users, "current_page" => $page, "total_pages" => ceil($count / $perPage)],
200);
}
}
}

View file

@ -4,9 +4,9 @@ namespace App\Http\Controllers\Api\Web;
use App\Http\Controllers\Controller;
use App\ProfileRequest;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
use Cache;
use Config;
use Response;
class ProfilerController extends Controller
{
@ -26,4 +26,4 @@ class ProfilerController extends Controller
return Response::json(['request' => ProfileRequest::load($request)->toArray()], 200);
}
}
}

View file

@ -18,6 +18,7 @@ class Kernel extends HttpKernel
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\App\Http\Middleware\Profiler::class,
];
/**

View file

@ -0,0 +1,59 @@
<?php
namespace App\Http\Middleware;
use App;
use Closure;
use Cache;
use Config;
use DB;
use Log;
use App\ProfileRequest;
use Symfony\Component\HttpFoundation\Response;
class Profiler
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Profiling magic time!
if (Config::get('app.debug')) {
DB::enableQueryLog();
$profiler = ProfileRequest::create();
try {
$response = $next($request);
} catch (\Exception $e) {
$response = \Response::make(['exception' => $e->getTrace()], 500);
$profiler->log('error', $e->__toString(), []);
}
$response = $this->processResponse($profiler, $response);
Log::listen(function ($level, $message, $context) use ($profiler, $request) {
$profiler->log($level, $message, $context);
});
} else {
// Process the request the usual, boring way.
$response = $next($request);
}
return $response;
}
protected function processResponse(ProfileRequest $profiler, Response $response) {
$profiler->recordQueries();
Cache::put('profiler-request-' . $profiler->getId(), $profiler->toString(), 2);
return $response->header('X-Request-Id', $profiler->getId());
}
}

View file

@ -2,7 +2,7 @@
namespace App;
use Illuminate\Support\Facades\DB;
use DB;
class ProfileRequest
{
@ -17,6 +17,9 @@ class ProfileRequest
return $req;
}
/**
* @return ProfileRequest
*/
public static function create()
{
$req = new ProfileRequest();
@ -45,9 +48,10 @@ class ProfileRequest
return $this->_id;
}
public function after($request, $response)
public function recordQueries()
{
$this->_data['queries'] = [];
foreach (DB::getQueryLog() as $query) {
if (starts_with($query['query'], 'select * from `cache` where')) {
continue;
@ -72,4 +76,4 @@ class ProfileRequest
'message' => $message
];
}
}
}

View file

@ -66,4 +66,4 @@ XMLHttpRequest.prototype.open = (method, url) ->
$.getJSON('/api/web/profiler/' + id).done (res) -> appendRequest method, url, res
(this.addEventListener "readystatechange", intercept, false) if url.indexOf('/api/web/profiler/') == -1
oldOpen.apply this, arguments
oldOpen.apply this, arguments