2017-08-15 07:26:20 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* https://stackoverflow.com/a/34894933
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
use App\Contracts\Middleware;
|
2017-08-15 07:26:20 +08:00
|
|
|
use Closure;
|
2019-11-27 22:19:20 +08:00
|
|
|
use Illuminate\Http\Request;
|
2017-08-15 07:26:20 +08:00
|
|
|
|
2019-11-27 22:19:20 +08:00
|
|
|
class MeasureExecutionTime implements Middleware
|
2017-08-15 07:26:20 +08:00
|
|
|
{
|
2019-11-27 22:19: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) + [
|
2020-01-07 02:44:43 +08:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
}
|