phpvms/app/Http/Middleware/MeasureExecutionTime.php

44 lines
992 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 Closure;
class MeasureExecutionTime
{
/**
* Handle an incoming request.
*
2018-08-27 00:40:04 +08:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
2017-08-15 07:26:20 +08:00
*
* @return mixed
*/
public function handle($request, Closure $next)
{
// Get the response
$response = $next($request);
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;
}
}