diff --git a/app/Http/Controllers/Api/Web/ArtistsController.php b/app/Http/Controllers/Api/Web/ArtistsController.php index a2a6e13f..cf48401f 100644 --- a/app/Http/Controllers/Api/Web/ArtistsController.php +++ b/app/Http/Controllers/Api/Web/ArtistsController.php @@ -194,4 +194,4 @@ class ArtistsController extends ApiControllerBase return Response::json(["artists" => $users, "current_page" => $page, "total_pages" => ceil($count / $perPage)], 200); } -} \ No newline at end of file +} diff --git a/app/Http/Controllers/Api/Web/ProfilerController.php b/app/Http/Controllers/Api/Web/ProfilerController.php index 1a2c81b2..2dbb4a79 100644 --- a/app/Http/Controllers/Api/Web/ProfilerController.php +++ b/app/Http/Controllers/Api/Web/ProfilerController.php @@ -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); } -} \ No newline at end of file +} diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 8436e93b..cc879ba5 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -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, ]; /** diff --git a/app/Http/Middleware/Profiler.php b/app/Http/Middleware/Profiler.php new file mode 100644 index 00000000..edaa1e67 --- /dev/null +++ b/app/Http/Middleware/Profiler.php @@ -0,0 +1,59 @@ + $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()); + } +} diff --git a/app/ProfileRequest.php b/app/ProfileRequest.php index bf134ce1..f9183f42 100644 --- a/app/ProfileRequest.php +++ b/app/ProfileRequest.php @@ -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 ]; } -} \ No newline at end of file +} diff --git a/resources/assets/scripts/debug/profiler.coffee b/resources/assets/scripts/debug/profiler.coffee index 470c0cca..f9e0a34c 100644 --- a/resources/assets/scripts/debug/profiler.coffee +++ b/resources/assets/scripts/debug/profiler.coffee @@ -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 \ No newline at end of file + oldOpen.apply this, arguments