Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ Component-level `@blaze` directives override directory-level settings.

Blaze supports all essential features and produces HTML output identical to Blade. While the focus is on maximizing performance with full compatibility, there are some limitations to be aware of:

- **Class-based components** are not supported
- **The `$component` variable** is not available
- **View composers / creators / lifecycle events** do not fire
- **Auto-injecting `View::share()` variables** is not supported
Expand All @@ -123,9 +122,6 @@ Blaze supports all essential features and produces HTML output identical to Blad
- **Cross boundary `@aware`** between Blade and Blaze

Both parent and child must use Blaze for values to propagate
- **Rendering Blaze components using `view()`** will not work

Blaze components can only be rendered using the component tag

# Optimization Strategies

Expand Down
11 changes: 5 additions & 6 deletions src/Compiler/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ public function wrap(string $compiled, string $path, ?string $source = null): st

$output = '';

$output .= '<'.'?php' . "\n";
$output .= $imports;
$output .= 'if (!function_exists(\''.$name.'\')):'."\n";
$output .= $imports ? "<?php\n".$imports."?>\n" : '';
$output .= '<?php if (isset($__path) && $__path === __FILE__): ?>'."\n";
$output .= $compiled;
$output .= '<?php elseif (!function_exists(\''.$name.'\')):'."\n";
$output .= 'function '.$name.'($__blaze, $__data = [], $__slots = [], $__bound = [], $__keys = [], $__this = null) {'."\n";

if ($sourceUsesThis) {
Expand All @@ -77,8 +78,6 @@ public function wrap(string $compiled, string $path, ?string $source = null): st
->directive('aware', $this->awareCompiler->compile(...))
->compile($compiled);

$compiled = $this->blade->restoreRawBlocks($compiled);

$output .= $compiled;

$output .= '<?php' . "\n";
Expand All @@ -93,7 +92,7 @@ public function wrap(string $compiled, string $path, ?string $source = null): st

$output .= '} endif; ?>';

return $output;
return $this->blade->restoreRawBlocks($output);
}

protected function globalVariables(string $source, string $compiled): string
Expand Down
10 changes: 7 additions & 3 deletions tests/Compiler/WrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
$wrapped = app(Wrapper::class)->wrap($source, $path, $source);

expect($wrapped)->toEqualCollapsingWhitespace(join('', [
'<?php if (!function_exists(\'_'. $hash .'\')): function _'. $hash .'($__blaze, $__data = [], $__slots = [], $__bound = [], $__keys = [], $__this = null) { ',
'<?php if (isset($__path) && $__path === __FILE__): ?> ',
$source,
'<?php elseif (!function_exists(\'_'. $hash .'\')): function _'. $hash .'($__blaze, $__data = [], $__slots = [], $__bound = [], $__keys = [], $__this = null) { ',
'$__env = $__blaze->env; ',
'if (($__data[\'attributes\'] ?? null) instanceof \Illuminate\View\ComponentAttributeBag) { $__data = $__data + $__data[\'attributes\']->all(); unset($__data[\'attributes\']); } ',
'extract($__slots, EXTR_SKIP); unset($__slots); ',
Expand All @@ -39,7 +41,9 @@
$wrapped = app(Wrapper::class)->wrap($source, $path, $source);

expect($wrapped)->toEqualCollapsingWhitespace(join('', [
'<?php if (!function_exists(\'_'. $hash .'\')): function _'. $hash .'($__blaze, $__data = [], $__slots = [], $__bound = [], $__keys = [], $__this = null) { ',
'<?php if (isset($__path) && $__path === __FILE__): ?> ',
$source,
'<?php elseif (!function_exists(\'_'. $hash .'\')): function _'. $hash .'($__blaze, $__data = [], $__slots = [], $__bound = [], $__keys = [], $__this = null) { ',
'$__env = $__blaze->env; ',
'if (($__data[\'attributes\'] ?? null) instanceof \Illuminate\View\ComponentAttributeBag) { $__data = $__data + $__data[\'attributes\']->all(); unset($__data[\'attributes\']); } ',
'extract($__slots, EXTR_SKIP); unset($__slots); ',
Expand Down Expand Up @@ -99,7 +103,7 @@
// Replace raw @php blocks for placeholders. This normally happens in BlazeManager before the template gets to the Wrapper
$source = app(BladeService::class)->preStoreUncompiledBlocks($statement);

expect(app(Wrapper::class)->wrap($source, '', $source))->toStartWith("<?php\nuse \App\Models\User");
expect(app(Wrapper::class)->wrap($source, '', $source))->toStartWith("<?php\nuse \App\Models\User;\n?>");
})->with([
['@use(\'App\Models\User\')'],
['@php use \App\Models\User; @endphp'],
Expand Down
12 changes: 12 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
view('mix')->render();
})->throwsNoExceptions();

test('renders components as views', function () {
Blaze::optimize()->in(fixture_path('views/components'));

expect(view('components.alert', ['message' => 'Hello world'])->render())->toBe('<div>Hello world</div>');
});

test('renders class based component', function () {
Blaze::optimize()->in(fixture_path('views/components'));

expect(Blade::render('<x-alert message="Hello world" />'))->toBe('<div>Hello world</div>');
});

test('supports php engine', function () {
view('php-view')->render();
})->throwsNoExceptions();
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/views/components/alert.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>{{ $message }}</div>
8 changes: 6 additions & 2 deletions workbench/app/View/Components/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

class Alert extends Component
{
public function render(): string
public function __construct(
public string $message,
) {}

public function render()
{
return '<div class="alert"></div>';
return view('components.alert');
}
}
Loading