Skip to content

Commit 651e235

Browse files
committed
Extract http.route instead of generating it
1 parent a00defc commit 651e235

1 file changed

Lines changed: 29 additions & 44 deletions

File tree

src/DDTrace/Integrations/Laminas/LaminasIntegration.php

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,10 @@ static function ($This, $scope, $args) {
544544
$rootSpan->resource = "$controller@$eventName $routeName";
545545
$rootSpan->meta['laminas.route.name'] = $routeName;
546546
$rootSpan->meta['laminas.route.action'] = $controller . '@' . $eventName;
547+
$routeTemplate = LaminasIntegration::httpRouteTemplateFromMatchedRoute($event->getRouteMatch(), $event->getRouteMatch());
548+
if ($routeTemplate !== null) {
549+
$rootSpan->meta[Tag::HTTP_ROUTE] = $routeTemplate;
550+
}
547551

548552
if (isset($eventName, self::$EVENT_TYPES)) {
549553
install_hook(
@@ -923,25 +927,42 @@ private static function getUserMetadata($identity)
923927
*/
924928
public static function httpRouteTemplateFromMatchedRoute($matchedRoute, $routeMatch = null)
925929
{
926-
if ($matchedRoute instanceof \Laminas\Router\Http\Literal) {
927-
$rp = new \ReflectionProperty($matchedRoute, 'route');
928-
$rp->setAccessible(true);
930+
if (is_object($matchedRoute)) {
931+
if (method_exists($matchedRoute, 'getSpec')) {
932+
$routeSpec = $matchedRoute->getSpec();
933+
if (is_string($routeSpec) && $routeSpec !== '') {
934+
return $routeSpec;
935+
}
936+
}
929937

930-
return (string) $rp->getValue($matchedRoute);
938+
if (method_exists($matchedRoute, 'getRoute')) {
939+
$routeSpec = $matchedRoute->getRoute();
940+
if (is_string($routeSpec) && $routeSpec !== '') {
941+
return $routeSpec;
942+
}
943+
}
931944
}
932945

933-
if ($matchedRoute instanceof \Laminas\Router\Http\Segment) {
934-
$rp = new \ReflectionProperty($matchedRoute, 'parts');
946+
if ($matchedRoute instanceof \Laminas\Router\Http\Literal) {
947+
$rp = new \ReflectionProperty($matchedRoute, 'route');
935948
$rp->setAccessible(true);
936-
$parts = $rp->getValue($matchedRoute);
937949

938-
return \is_array($parts) ? self::laminasSegmentPartsToRouteTemplate($parts) : null;
950+
return (string) $rp->getValue($matchedRoute);
939951
}
940952

941953
if (
942954
$matchedRoute instanceof \Laminas\Router\Http\TreeRouteStack
943955
&& $routeMatch instanceof RouteMatch
944956
) {
957+
if (method_exists($routeMatch, 'getMatchedRoute')) {
958+
$getMatchedRoute = [$routeMatch, 'getMatchedRoute'];
959+
$nestedMatchedRoute = call_user_func($getMatchedRoute);
960+
$nestedTemplate = self::httpRouteTemplateFromMatchedRoute($nestedMatchedRoute, $routeMatch);
961+
if ($nestedTemplate !== null && $nestedTemplate !== '') {
962+
return $nestedTemplate;
963+
}
964+
}
965+
945966
$matchedName = $routeMatch->getMatchedRouteName();
946967
if ($matchedName === null || $matchedName === '') {
947968
return null;
@@ -1088,42 +1109,6 @@ public static function partRouteBaseTemplate(\Laminas\Router\Http\Part $part): ?
10881109
return self::httpRouteTemplateFromMatchedRoute($baseRoute, null);
10891110
}
10901111

1091-
/**
1092-
* Rebuilds the route string from {@see \Laminas\Router\Http\Segment} parsed parts (inverse of parseRouteDefinition).
1093-
*
1094-
* @internal
1095-
* @param array<int, array> $parts
1096-
*/
1097-
public static function laminasSegmentPartsToRouteTemplate(array $parts): string
1098-
{
1099-
$buf = '';
1100-
foreach ($parts as $part) {
1101-
if (!\is_array($part) || !isset($part[0])) {
1102-
continue;
1103-
}
1104-
switch ($part[0]) {
1105-
case 'literal':
1106-
$buf .= $part[1] ?? '';
1107-
break;
1108-
case 'parameter':
1109-
$buf .= ':';
1110-
$buf .= $part[1] ?? '';
1111-
if (isset($part[2]) && $part[2] !== null && $part[2] !== '') {
1112-
$buf .= '{' . $part[2] . '}';
1113-
}
1114-
break;
1115-
case 'optional':
1116-
$buf .= '[' . self::laminasSegmentPartsToRouteTemplate($part[1] ?? []) . ']';
1117-
break;
1118-
case 'translated-literal':
1119-
$buf .= '{' . ($part[1] ?? '') . '}';
1120-
break;
1121-
}
1122-
}
1123-
1124-
return $buf;
1125-
}
1126-
11271112
public static function debugBacktraceToString(array $backtrace)
11281113
{
11291114
// (methods) #<frame index> <file>(line): <class><type><function>()\n

0 commit comments

Comments
 (0)