|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Naoray\LaravelGithubMonolog\Tracing; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Http\Events\RequestHandled; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Illuminate\Support\Facades\Context; |
| 8 | +use Naoray\LaravelGithubMonolog\Tracing\Concerns\RedactsData; |
| 9 | +use Naoray\LaravelGithubMonolog\Tracing\Concerns\ResolvesTracingConfig; |
| 10 | +use Naoray\LaravelGithubMonolog\Tracing\Contracts\EventDrivenCollectorInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Inertia.js data collector. |
| 14 | + * |
| 15 | + * This collector detects Inertia requests by examining request headers, |
| 16 | + * without requiring the Inertia package as a dependency. It captures |
| 17 | + * component and version information from the request structure. |
| 18 | + */ |
| 19 | +class InertiaDataCollector implements EventDrivenCollectorInterface |
| 20 | +{ |
| 21 | + use RedactsData; |
| 22 | + use ResolvesTracingConfig; |
| 23 | + |
| 24 | + public function isEnabled(): bool |
| 25 | + { |
| 26 | + return $this->isTracingFeatureEnabled('inertia'); |
| 27 | + } |
| 28 | + |
| 29 | + public function __invoke(RequestHandled $event): void |
| 30 | + { |
| 31 | + if (! $this->isInertiaRequest($event->request)) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + $this->captureFromRequest($event->request, $event->response); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Detect if the current request is an Inertia request. |
| 40 | + */ |
| 41 | + protected function isInertiaRequest(Request $request): bool |
| 42 | + { |
| 43 | + return $request->hasHeader('X-Inertia'); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Capture Inertia request data. |
| 48 | + * |
| 49 | + * @param \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response $response |
| 50 | + */ |
| 51 | + protected function captureFromRequest(Request $request, $response): void |
| 52 | + { |
| 53 | + $data = [ |
| 54 | + 'version' => $request->header('X-Inertia-Version'), |
| 55 | + 'partial_reload' => $request->hasHeader('X-Inertia-Partial-Data'), |
| 56 | + ]; |
| 57 | + |
| 58 | + // Capture partial reload details |
| 59 | + if ($data['partial_reload']) { |
| 60 | + $data['partial_component'] = $request->header('X-Inertia-Partial-Component'); |
| 61 | + $data['partial_keys'] = $this->parsePartialKeys( |
| 62 | + $request->header('X-Inertia-Partial-Data') |
| 63 | + ); |
| 64 | + $data['partial_except'] = $this->parsePartialKeys( |
| 65 | + $request->header('X-Inertia-Partial-Except') |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + // Try to extract component name from response |
| 70 | + $component = $this->extractComponentFromResponse($response); |
| 71 | + if ($component !== null) { |
| 72 | + $data['component'] = $component; |
| 73 | + } |
| 74 | + |
| 75 | + // Add request URL for context |
| 76 | + $data['url'] = $request->fullUrl(); |
| 77 | + |
| 78 | + // Filter nulls but keep false values (like partial_reload = false) |
| 79 | + Context::add('inertia', array_filter($data, fn ($value) => $value !== null)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Parse comma-separated partial keys into an array. |
| 84 | + * |
| 85 | + * @return array<string>|null |
| 86 | + */ |
| 87 | + protected function parsePartialKeys(?string $keys): ?array |
| 88 | + { |
| 89 | + if ($keys === null || $keys === '') { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return array_map('trim', explode(',', $keys)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Extract the Inertia component name from the response. |
| 98 | + * |
| 99 | + * @param \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response $response |
| 100 | + */ |
| 101 | + protected function extractComponentFromResponse($response): ?string |
| 102 | + { |
| 103 | + // Check X-Inertia header in response |
| 104 | + if (! $response->headers->has('X-Inertia')) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + // Try to parse component from JSON response |
| 109 | + $content = $response->getContent(); |
| 110 | + if (! $content || ! is_string($content)) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + $decoded = json_decode($content, true); |
| 115 | + if (! is_array($decoded)) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + return $decoded['component'] ?? null; |
| 120 | + } |
| 121 | +} |
0 commit comments