Created
February 8, 2018 13:11
-
-
Save fizerkhan/fab19d93b8f45e0e425febcf04b4c2d5 to your computer and use it in GitHub Desktop.
Atatus Middleware for Lumen Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// bootstrap/app.php | |
//... | |
$app->middleware([ | |
//... | |
\App\Http\Middleware\AtatusLumenMiddleware::class, | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Http/Middleware/AtatusLumenMiddleware.php | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Http\Request; | |
/** | |
* Class AtatusLumenMiddleware | |
*/ | |
class AtatusLumenMiddleware | |
{ | |
/** | |
* Set Custom Transaction Name | |
* | |
* @param Request $request | |
* @param Closure $next | |
*/ | |
public function handle(Request $request, Closure $next) | |
{ | |
$response = $next($request); | |
if (extension_loaded('atatus')) { | |
atatus_set_transaction_name($this->txnNameFromRequest($request)); | |
} | |
return $response; | |
} | |
/** | |
* Transaction name from request. | |
* | |
* @param Request $request | |
* @return string | |
*/ | |
public function txnNameFromRequest(Request $request) | |
{ | |
$route = $request->route(); | |
if (is_array($route)) { | |
if (isset($route[1]) && isset($route[1]['uses'])) { | |
return $route[1]['uses']; | |
} | |
elseif (isset($route[1]) && isset($route[1]['as'])) { | |
return $route[1]['as']; | |
} | |
} | |
return 'index.php'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment