Skip to content

Commit

Permalink
[1.x] Fixes bottom script being included as template (#38)
Browse files Browse the repository at this point in the history
* Fixes bottom conflicts

* One more test

* One more test
  • Loading branch information
nunomaduro authored Aug 28, 2023
1 parent d50867c commit 9d6c455
Show file tree
Hide file tree
Showing 5 changed files with 406 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Precompilers/ExtractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,31 @@ protected function shouldExtractTemplate(string $template): bool
*/
protected function imports(string $template): string
{
$phpScript = trim(preg_replace('/.*<\?php\s*(.*?)\s*\?>.*/s', '$1', $template));
$phpScript = trim(preg_replace('/^(?!use\s+.*?;).*$/m', '', $phpScript));
$phpScript = trim(preg_replace('/^use\s+function\s+Livewire\\\\Volt.*$/m', '', $phpScript));
preg_match_all('/<\?php\s*(.*?)\s*\?>/s', $template, $matches);

if (! empty($phpScript)) {
$phpScript = '<?php'."\n\n".$phpScript."\n\n".'?>'."\n\n";
$script = collect($matches[1])
->map(fn (string $script) => trim($script))
->reject(fn (string $script) => empty($script))
->implode("\n");

$script = trim(preg_replace('/^(?!use\s+.*?;).*$/m', '', $script));
$script = trim(preg_replace('/^use\s+function\s+Livewire\\\\Volt.*$/m', '', $script));

if (! empty($script)) {
$script = '<?php'."\n\n".$script."\n\n".'?>'."\n\n";
}

return $phpScript;
return $script;
}

/**
* Extract the HTML from the given template.
*/
protected function html(string $template): string
{
return trim(preg_replace('/<\?php\s*(.*?)\s*\?>/s', '', $template));
$template = trim(preg_replace('/<\?php\s*(.*?)\s*\?>/s', '', $template));

return str($template)->beforeLast('<?php')->trim()->value();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Livewire\Volt\Actions;
use Livewire\Volt\CompileContext;
use Livewire\Volt\Contracts\Compiled;
use Livewire\Volt\Component;

new class extends Component implements Livewire\Volt\Contracts\FunctionalComponent
{
public static CompileContext $__context;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

public $name;

public $counter;

public function mount($name = NULL)
{
(new Actions\InitializeState)->execute(static::$__context, $this, get_defined_vars());

(new Actions\CallHook('mount'))->execute(static::$__context, $this, get_defined_vars());
}

public function increment()
{
$arguments = [static::$__context, $this, func_get_args()];

return (new Actions\CallMethod('increment'))->execute(...$arguments);
}

};
16 changes: 16 additions & 0 deletions tests/Feature/FunctionalComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Livewire\Volt\Volt;
use Pest\TestSuite;
use Tests\Fixtures\GlobalTrait;
use Tests\Fixtures\User;

beforeEach(function () {
View::setFinder(new FileViewFinder(app()['files'], [__DIR__.'/resources/views']));
Expand Down Expand Up @@ -789,3 +790,18 @@ public function render()
->assertSeeVolt('basic-component')
->assertOk();
});

test('user imports on bottom do not conflict', function () {
User::create([
'name' => 'Taylor',
'email' => '[email protected]',
'password' => 'password',
]);

Livewire::test('component-with-user-imports', ['name' => 'Taylor'])
->assertSet('counter', 0)
->call('increment')
->call('increment')
->call('increment')
->assertSet('counter', 3);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div>
Hello {{ $name }}
<br/>
Counter: {{ $counter }}
<br/>
<button wire:click="increment">
Increment Counter
</button>
</div>

<?php
use Tests\Fixtures\User;
use function Livewire\Volt\mount;
use function Livewire\Volt\state;
state(name: '', counter: 0);
mount(fn ($name = null) => $this->name = $name ?? User::first()->name);
$increment = fn () => $this->counter++;
Loading

0 comments on commit 9d6c455

Please sign in to comment.