mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-28 23:57:59 +01:00
Updated the SPA profiler as Laravel 5 middleware.
This commit is contained in:
parent
7d047a41e4
commit
97681212a4
6 changed files with 73 additions and 9 deletions
|
@ -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
|
||||
{
|
||||
|
|
|
@ -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,
|
||||
];
|
||||
|
||||
/**
|
||||
|
|
59
app/Http/Middleware/Profiler.php
Normal file
59
app/Http/Middleware/Profiler.php
Normal 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());
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue