phpvms/app/Http/Middleware/MeasureExecutionTime.php

38 lines
903 B
PHP
Raw Normal View History

2017-08-15 07:26:20 +08:00
<?php
/**
* https://stackoverflow.com/a/34894933
*/
namespace App\Http\Middleware;
use App\Contracts\Middleware;
2017-08-15 07:26:20 +08:00
use Closure;
use Illuminate\Http\Request;
2017-08-15 07:26:20 +08:00
class MeasureExecutionTime implements Middleware
2017-08-15 07:26:20 +08:00
{
public function handle(Request $request, Closure $next)
2017-08-15 07:26:20 +08:00
{
// Get the response
$response = $next($request);
2018-08-27 02:50:08 +08:00
if (!\defined('LUMEN_START')) {
2017-08-15 07:41:55 +08:00
return $response;
}
2017-08-15 07:26:20 +08:00
// Calculate execution time
$executionTime = microtime(true) - LUMEN_START;
// I assume you're using valid json in your responses
// Then I manipulate them below
$content = json_decode($response->getContent(), true) + [
'execution_time' => $executionTime,
];
2017-08-15 07:26:20 +08:00
// Change the content of your response
$response->setData($content);
// Return the response
return $response;
}
}