This commit is contained in:
nuno maduro
2026-05-02 17:37:34 +01:00
parent 1aa80dc398
commit 635460653c

View File

@ -36,11 +36,7 @@ final class InertiaEdges
return; return;
} }
if ($app->bound(self::MARKER)) { if ($app->bound(self::MARKER) || ! $app->bound('events')) {
return;
}
if (! $app->bound('events')) {
return; return;
} }
@ -54,18 +50,11 @@ final class InertiaEdges
} }
$events->listen(self::REQUEST_HANDLED_EVENT, static function (object $event) use ($recorder): void { $events->listen(self::REQUEST_HANDLED_EVENT, static function (object $event) use ($recorder): void {
if (! property_exists($event, 'response')) { if (! property_exists($event, 'response') || ! is_object($event->response)) {
return; return;
} }
/** @var mixed $response */ $component = self::extractComponent($event->response);
$response = $event->response;
if (! is_object($response)) {
return;
}
$component = self::extractComponent($response);
if ($component !== null) { if ($component !== null) {
$recorder->linkInertiaComponent($component); $recorder->linkInertiaComponent($component);
@ -75,32 +64,16 @@ final class InertiaEdges
private static function extractComponent(object $response): ?string private static function extractComponent(object $response): ?string
{ {
if (property_exists($response, 'headers') && is_object($response->headers)) {
$headers = $response->headers;
if (method_exists($headers, 'has') && $headers->has('X-Inertia')) {
$content = self::readContent($response);
if ($content !== null) {
/** @var mixed $decoded */
$decoded = json_decode($content, true);
if (is_array($decoded)
&& isset($decoded['component'])
&& is_string($decoded['component'])
&& $decoded['component'] !== '') {
return $decoded['component'];
}
}
}
}
$content = self::readContent($response); $content = self::readContent($response);
if ($content === null) { if ($content === null) {
return null; return null;
} }
if (self::isInertiaJsonResponse($response)) {
return self::componentFromJson($content);
}
if (str_contains($content, 'type="application/json"') if (str_contains($content, 'type="application/json"')
&& preg_match('#<script\b(?=[^>]*\bdata-page="app")(?=[^>]*\btype="application/json")[^>]*>(.+?)</script>#s', $content, $match) === 1) { && preg_match('#<script\b(?=[^>]*\bdata-page="app")(?=[^>]*\btype="application/json")[^>]*>(.+?)</script>#s', $content, $match) === 1) {
$component = self::componentFromJson(html_entity_decode($match[1])); $component = self::componentFromJson(html_entity_decode($match[1]));
@ -112,16 +85,23 @@ final class InertiaEdges
if (str_contains($content, 'data-page=') if (str_contains($content, 'data-page=')
&& preg_match('/\sdata-page="(\{[^"]+\})"/', $content, $match) === 1) { && preg_match('/\sdata-page="(\{[^"]+\})"/', $content, $match) === 1) {
$component = self::componentFromJson(html_entity_decode($match[1])); return self::componentFromJson(html_entity_decode($match[1]));
if ($component !== null) {
return $component;
}
} }
return null; return null;
} }
private static function isInertiaJsonResponse(object $response): bool
{
if (! property_exists($response, 'headers') || ! is_object($response->headers)) {
return false;
}
$headers = $response->headers;
return method_exists($headers, 'has') && $headers->has('X-Inertia') === true;
}
private static function componentFromJson(string $json): ?string private static function componentFromJson(string $json): ?string
{ {
/** @var mixed $decoded */ /** @var mixed $decoded */