framework icon indicating copy to clipboard operation
framework copied to clipboard

Fix @JSON Blade directive parsing with nested structures

Open elliota43 opened this issue 4 days ago • 0 comments

Description

Fixes @json Blade directive incorrectly parsing complex expressions with nested structures. Replaces explode() with tokenizer-based parser that respects nesting depth.

Problem

The directive would split on commas inside nested arrays, closures, and method calls, causing parse errors.

Example (from original issue):

@json([
    'items' => $helpers['benefit']['getAll']()->map(fn($item) => [
        'icon' => $item->icon,
        'title' => (string)$item->title,
        'description' => (string)$item->description
    ]),
    'translation' => '%:booking.benefits%'
])

would incorrectly split on the comma inside the closure.

Solution

  • Added parseArguments() method using PHP's tokenizer
  • Tracks nesting depth (parentheses, brackets, braces)
  • Only splits on commas at top level (depth === 0)
  • Handles empty expressions (defaults to null)

Fully backwards compatible - defaults to same behavior as before.

Fixes #56331

elliota43 avatar Dec 10 '25 21:12 elliota43