|
2 | 2 |
|
3 | 3 | namespace Intouch\LaravelNewrelic; |
4 | 4 |
|
5 | | -use Illuminate\Http\Request as Request; |
| 5 | +use Illuminate\Http\Request; |
6 | 6 | use Closure; |
7 | 7 |
|
8 | 8 | class LumenNewrelicMiddleware |
9 | 9 | { |
10 | | - /** |
11 | | - * @param Request $request |
12 | | - * @param Closure $next |
13 | | - */ |
14 | | - public function handle(Request $request, Closure $next) |
15 | | - { |
16 | | - $config = app()['config']; |
17 | | - |
18 | | - if (true == $config->get('newrelic.auto_name_transactions')) { |
19 | | - app('newrelic')->nameTransaction($this->getTransactionName()); |
20 | | - } |
21 | | - $response = $next($request); |
22 | | - |
23 | | - return $response; |
24 | | - } |
25 | | - |
26 | | - /** |
27 | | - * Build the transaction name |
28 | | - * |
29 | | - * @return string |
30 | | - */ |
31 | | - public function getTransactionName() |
32 | | - { |
33 | | - return str_replace( |
34 | | - [ |
35 | | - '{controller}', |
36 | | - '{method}', |
37 | | - '{route}', |
38 | | - '{path}', |
39 | | - '{uri}', |
40 | | - ], |
41 | | - [ |
42 | | - $this->getController(), |
43 | | - app('request')->getMethod(), |
44 | | - $this->getRoute(), |
45 | | - app('request')->getPathInfo(), |
46 | | - app('request')->getUri(), |
47 | | - ], |
48 | | - app('config')->get('newrelic.name_provider') |
49 | | - ); |
50 | | - } |
51 | | - |
52 | | - /** |
53 | | - * Get the current route name, or controller if not named |
54 | | - * |
55 | | - * @return string |
56 | | - */ |
57 | | - protected function getRoute() |
58 | | - { |
59 | | - return $this->getRouteObject()['uri']; |
60 | | - } |
61 | | - |
62 | | - /** |
63 | | - * Get the current controller / action |
64 | | - * |
65 | | - * @return string |
66 | | - */ |
67 | | - protected function getController() |
68 | | - { |
69 | | - return isset($this->getRouteObject()['action']['uses']) ? $this->getRouteObject()['action']['uses'] : 'Closure'; |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * Get current route object |
74 | | - * @date 2016-02-10 |
75 | | - * @return array |
76 | | - */ |
77 | | - protected function getRouteObject() { |
78 | | - $request = app('request'); |
79 | | - |
80 | | - $verbs = 'GET|POST|PUT|DELETE|PATCH'; |
81 | | - |
82 | | - $routeToRegex = function ($string) use ($verbs) { |
83 | | - $string = preg_replace("/^({$verbs})/", '', $string); |
84 | | - $string = preg_replace('/\{\w+\}/', '[^/]+', $string); |
85 | | - $string = preg_replace('/\{(\w+):(.+?)\}/', '\2', $string); |
86 | | - return '#^'.$string.'$#'; |
87 | | - }; |
88 | | - |
89 | | - $routeToMethod = function ($string) use ($verbs) { |
90 | | - return preg_replace("/^({$verbs}).+$/", '\1', $string); |
91 | | - }; |
92 | | - |
93 | | - $routes = []; |
94 | | - foreach (app()->getRoutes() as $routeName => $route) { |
95 | | - $regex = $routeToRegex($routeName); |
96 | | - $method = $routeToMethod($routeName); |
97 | | - $routes[$method.$regex] = compact('route', 'method', 'regex'); |
98 | | - } |
99 | | - |
100 | | - uksort($routes, function ($a, $b) { |
101 | | - return strlen($b) - strlen($a); |
102 | | - }); |
103 | | - |
104 | | - $method = $request->getMethod(); |
105 | | - $path = rtrim($request->getPathInfo(), '/'); |
106 | | - $foundRoute = null; |
107 | | - |
108 | | - foreach ($routes as $regex => $details) { |
109 | | - $regex = substr($regex, strlen($details['method'])); |
110 | | - if (true == preg_match($regex, $path) && $method == $details['method']) { |
111 | | - $foundRoute = $details['route']; |
112 | | - break; |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - return $foundRoute; |
117 | | - } |
| 10 | + /** |
| 11 | + * @param Request $request |
| 12 | + * @param Closure $next |
| 13 | + */ |
| 14 | + public function handle(Request $request, Closure $next) |
| 15 | + { |
| 16 | + $config = app()['config']; |
| 17 | + |
| 18 | + if (true == $config->get('newrelic.auto_name_transactions')) { |
| 19 | + app('newrelic')->nameTransaction($this->getTransactionName()); |
| 20 | + } |
| 21 | + $response = $next($request); |
| 22 | + |
| 23 | + return $response; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Build the transaction name |
| 28 | + * |
| 29 | + * @return string |
| 30 | + */ |
| 31 | + public function getTransactionName() |
| 32 | + { |
| 33 | + return str_replace( |
| 34 | + [ |
| 35 | + '{controller}', |
| 36 | + '{method}', |
| 37 | + '{route}', |
| 38 | + '{path}', |
| 39 | + '{uri}', |
| 40 | + ], |
| 41 | + [ |
| 42 | + $this->getController(), |
| 43 | + app('request')->getMethod(), |
| 44 | + $this->getRoute(), |
| 45 | + app('request')->getPathInfo(), |
| 46 | + app('request')->getUri(), |
| 47 | + ], |
| 48 | + app('config')->get('newrelic.name_provider') |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Get the current route name, or controller if not named |
| 54 | + * |
| 55 | + * @return string |
| 56 | + */ |
| 57 | + protected function getRoute() |
| 58 | + { |
| 59 | + return $this->getRouteObject()['uri']; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get the current controller / action |
| 64 | + * |
| 65 | + * @return string |
| 66 | + */ |
| 67 | + protected function getController() |
| 68 | + { |
| 69 | + return isset($this->getRouteObject()['action']['uses']) ? $this->getRouteObject()['action']['uses'] : 'Closure'; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Get current route object |
| 74 | + * @date 2016-02-10 |
| 75 | + * @return array |
| 76 | + */ |
| 77 | + protected function getRouteObject() |
| 78 | + { |
| 79 | + $request = app('request'); |
| 80 | + |
| 81 | + $verbs = 'GET|POST|PUT|DELETE|PATCH'; |
| 82 | + |
| 83 | + $routeToRegex = function ($string) use ($verbs) { |
| 84 | + $string = preg_replace("/^({$verbs})/", '', $string); |
| 85 | + $string = preg_replace('/\{\w+\}/', '[^/]+', $string); |
| 86 | + $string = preg_replace('/\{(\w+):(.+?)\}/', '\2', $string); |
| 87 | + return '#^'.$string.'$#'; |
| 88 | + }; |
| 89 | + |
| 90 | + $routeToMethod = function ($string) use ($verbs) { |
| 91 | + return preg_replace("/^({$verbs}).+$/", '\1', $string); |
| 92 | + }; |
| 93 | + |
| 94 | + $routes = []; |
| 95 | + foreach (app()->getRoutes() as $routeName => $route) { |
| 96 | + $regex = $routeToRegex($routeName); |
| 97 | + $method = $routeToMethod($routeName); |
| 98 | + $routes[$method.$regex] = compact('route', 'method', 'regex'); |
| 99 | + } |
| 100 | + |
| 101 | + uksort($routes, function ($a, $b) { |
| 102 | + return strlen($b) - strlen($a); |
| 103 | + }); |
| 104 | + |
| 105 | + $method = $request->getMethod(); |
| 106 | + $path = rtrim($request->getPathInfo(), '/'); |
| 107 | + $foundRoute = null; |
| 108 | + |
| 109 | + foreach ($routes as $regex => $details) { |
| 110 | + $regex = substr($regex, strlen($details['method'])); |
| 111 | + if (true == preg_match($regex, $path) && $method == $details['method']) { |
| 112 | + $foundRoute = $details['route']; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return $foundRoute; |
| 118 | + } |
118 | 119 | } |
119 | 120 |
|
0 commit comments