Skip to content

Comments

Refactor Wrapper#45

Merged
ganyicz merged 7 commits intomainfrom
filip/refactor-wrapper
Feb 23, 2026
Merged

Refactor Wrapper#45
ganyicz merged 7 commits intomainfrom
filip/refactor-wrapper

Conversation

@ganyicz
Copy link
Collaborator

@ganyicz ganyicz commented Feb 23, 2026

The scenario

@use directives don't work. Two use statements in a single @php block throw an "unexpected use" exception:

@php
use App\Models\User;
use App\Models\Order;
@endphp

The problem

Use statements need to be hoisted before the function definition. We were extracting them with regexes and juggling raw block placeholders. The regex couldn't handle all valid PHP use syntax, and the placeholder dance made the code fragile.

The Wrapper class had accumulated other issues that made fixing this harder. Props were assigned at the top of the function body before $attributes existed, this added conditional branches and syncing logic throughout the wrapper. This PR refactors this class significantly.

The solution

  • UseExtractor — uses php-parser to extract use statements from <?php ?> blocks. Handles all valid syntax — aliases, group use, function/const imports — without regex.

  • Unified raw block handling — raw blocks are restored before wrapping so UseExtractor works on actual PHP. Verbatim blocks are re-stored afterward so they survive compilation.

  • Props and aware as directives — compiled in-place instead of hoisted to the top of the function body. The attribute bag is created once, before any directive runs, so props read from $attributes directly. This matches Laravel's behavior and eliminates the dual attribute bag.

  • Output trimming — component output is captured with ob_start() and passed through ltrim before echoing, matching Laravel's behavior directly instead of regex hacks.

  • Adds $_instance support for Livewire's @this directive.

@ganyicz ganyicz changed the title Wrapper refactor Refactor Wrapper Feb 23, 2026
@ganyicz ganyicz merged commit 0d2cbc1 into main Feb 23, 2026
6 checks passed
@joshhanley joshhanley deleted the filip/refactor-wrapper branch February 23, 2026 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant