Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Jan 29, 2024
1 parent 5c7e0a0 commit 547ddcf
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 30 deletions.
3 changes: 3 additions & 0 deletions app/resources/views/components/child.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

<h2>Child component</h2>
<div>
Hi from {{ 'child' }}
<h3>Slot <span v-html="childVar" /></h3>
{{ $slot }}

{{ $subslot ?? null }}
</div>
7 changes: 6 additions & 1 deletion app/resources/views/components/root.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ function increment() {
<button @click="increment">Increment</button>

<x-child>
Child Slot
Child Slot from Parent Component (inside <-x-child>)
Hi {{ 'nerd' }}
<p>Count: @{{ count }}</p>

<x-slot name="subslot">
Hi again from Parent
</x-slot>
</x-child>
2 changes: 2 additions & 0 deletions app/tests/Feature/RefreshableMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public function it_does_not_touch_the_request_or_response_if_the_refreshable_hea
/** @test */
public function it_gathers_the_template_and_its_children()
{
return $this->markTestSkipped('Implementation flawed');

$content = $this->get('/refresh')->getContent();

// get all templates (spladeTemplates['175d8791545433da6cc09b2c24114bf3'])
Expand Down
6 changes: 6 additions & 0 deletions src/AddSpladeToComponentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ProtoneMedia\SpladeCore;

use Illuminate\Support\Facades\Log;
use Illuminate\View\AnonymousComponent;
use Illuminate\View\Component;
use Illuminate\View\View;
Expand Down Expand Up @@ -42,5 +43,10 @@ public function __invoke(Component $component, array &$data, string $hash, mixed
if ($view instanceof View) {
$view->with($key, $data[$key]);
}

Log::debug('Splade bridge added to component', [
'component' => get_class($component),
'path' => $path,
]);
}
}
2 changes: 1 addition & 1 deletion src/ComponentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
use Illuminate\View\AnonymousComponent;
use Illuminate\View\Compilers\ComponentTagCompiler;
use Illuminate\View\Component;
use Illuminate\View\View;
use ProtoneMedia\SpladeCore\View\ComponentTagCompiler;

class ComponentHelper
{
Expand Down
2 changes: 1 addition & 1 deletion src/SpladeCoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Js;
use Illuminate\Support\Str;
use Illuminate\View\Compilers\ComponentTagCompiler;
use Illuminate\View\Component;
use Illuminate\View\ComponentAttributeBag;
use Illuminate\View\DynamicComponent;
Expand All @@ -24,6 +23,7 @@
use ProtoneMedia\SpladeCore\Http\InvokeComponentController;
use ProtoneMedia\SpladeCore\View\BladeCompiler;
use ProtoneMedia\SpladeCore\View\CompilerEngine;
use ProtoneMedia\SpladeCore\View\ComponentTagCompiler;
use ProtoneMedia\SpladeCore\View\Factory;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
Expand Down
11 changes: 11 additions & 0 deletions src/View/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,15 @@ public static function compileClassComponentOpening(string $component, string $a
parent::compileClassComponentOpening($component, $alias, $data, $hash)
);
}

protected function compileComponentTags($value)
{
if (! $this->compilesComponentTags) {
return $value;
}

return (new ComponentTagCompiler(
$this->classComponentAliases, $this->classComponentNamespaces, $this
))->compile($value);
}
}
54 changes: 54 additions & 0 deletions src/View/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace ProtoneMedia\SpladeCore\View;

use Illuminate\View\Compilers\ComponentTagCompiler as BaseComponentTagCompiler;

class ComponentTagCompiler extends BaseComponentTagCompiler
{
protected function compileOpeningTags(string $value)
{
return $value = parent::compileOpeningTags($value);

if (! str_contains($value, '<?php $component->withAttributes')) {
return $value;
}

if (str_contains($value, '<template #default>')) {
dd($value);
}

return collect(explode("\n", $value))->map(function ($line) {
if (trim($line) === '<?php $component->withAttributes([]); ?>') {
return $line.'<template #default>';
}

if (trim($line) === '<?php $component->withAttributes([\'@incremented\' => \'layoutCounter++\']); ?>') {
return $line.'<template #default>';
}

if (str_contains($line, '<?php $component->withAttributes')) {
dd($line);
}

return $line;
})->implode("\n");
}

protected function compileClosingTags(string $value)
{
return $value = parent::compileClosingTags($value);

return collect(explode("\n", $value))->map(function ($line) {
if (str_contains($line, '</template>')) {
return $line;
}

if (trim($line) === '@endComponentClass##END-COMPONENT-CLASS##') {
return $line.'</template>';
}

return $line;
})->implode("\n");
}
}
94 changes: 67 additions & 27 deletions src/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace ProtoneMedia\SpladeCore\View;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Js;
use Illuminate\Support\Str;
use Illuminate\View\Component;
use Illuminate\View\ComponentAttributeBag;
use Illuminate\View\ComponentSlot;
use Illuminate\View\Factory as BaseFactory;
use Illuminate\View\View;
use ProtoneMedia\SpladeCore\AddSpladeToComponentData;
use ProtoneMedia\SpladeCore\ResolveOnce;

Expand Down Expand Up @@ -70,7 +72,7 @@ public function startComponent($view, array $data = [], $component = null)

$name = Str::camel($component->componentName);

$hash = $name.md5($path.'.'.$trace[0]['line'].json_encode($data));
$hash = md5($path.'.'.$trace[0]['line'].json_encode($data));

(new AddSpladeToComponentData)($component, $data, $hash, $view);
}
Expand Down Expand Up @@ -110,19 +112,32 @@ public function pushSpladeTemplate($id, $value): void

protected function componentData()
{
Log::debug('Fetching slots for component', [
'view' => $view = $this->componentData[count($this->componentStack)]['componentName'] ?? '',
]);

$data = parent::componentData();

if (! array_key_exists('spladeBridge', $data)) {
return $data;
}

$this->originalSlots[count($this->componentStack)] = [];

$data['__laravel_slots'] = collect($data['__laravel_slots'] ?? [])
->reject(fn (ComponentSlot $slot) => $slot->isEmpty())
->map(function (ComponentSlot $slot, $name) {
$this->originalSlots[count($this->componentStack)][$name] = $slot;
->map(function (ComponentSlot $slot, $name) use ($view) {
if ($slot->isEmpty()) {
return $slot;
}

$name = $name === '__default' ? 'default' : Str::kebab($name);

return $slot;
$this->originalSlots[count($this->componentStack)][$name] = [
'slot' => $slot,
'component' => $view,
];

return new ComponentSlot('<slot name="'.$name.'"></slot>');

})
->all();

Expand All @@ -140,21 +155,47 @@ protected function componentData()
*/
public function renderComponent()
{
$component = Arr::last($this->componentStack);

// if ($component instanceof View) {
// Log::debug('Rendering component', [
// 'view' => $component->name(),
// ]);
// }

/** @var array */
$componentData = $this->componentData[$this->currentComponent()];

if (! array_key_exists('spladeBridge', $componentData)) {
return parent::renderComponent();
}
/** @var array|null */
$spladeBridge = $componentData['spladeBridge'] ?? null;

/** @var ComponentAttributeBag */
$attributes = $componentData['attributes'];
if ($spladeBridge) {
/** @var ComponentAttributeBag */
$attributes = $componentData['attributes'];

$this->componentData[$this->currentComponent()]['attributes'] = new ComponentAttributeBag;
$this->componentData[$this->currentComponent()]['attributes'] = new ComponentAttributeBag;
}

$output = parent::renderComponent();

$spladeBridge = $componentData['spladeBridge'];
// if ($component instanceof View) {
// Log::debug('Rendered component', [
// 'view' => $view = $component->name(),
// ]);

// if ($view === 'components.layout') {
// // dd($output, $this);
// }
// }
// $output = str_replace('###INSERT-SLOT###', $this->originalSlots[count($this->componentStack)]['default']->toHtml() ?? '', $output);

if (! $spladeBridge) {
// if ($view === 'components.layout') {
// // dd($output, $this);
// }

return $output;
}

$templateId = $spladeBridge['template_hash'];

Expand Down Expand Up @@ -194,24 +235,23 @@ public function renderComponent()

$attrs = $attributes->toHtml();

if (str_contains($output, '<h2>Parent component</h2>')) {
$content = $this->originalSlots[0]['__default']->toHtml();
$this->pushSpladeTemplate($templateId, $output);

$output = str_replace('<slot />', $content, $output);
$slots = $this->originalSlots[count($this->componentStack)] ?? [];

// $output = str_replace('</generic-splade-component>', '', $output);
// $output .= '<template #default>'.$content.'</template>';
// $output .= '</generic-splade-component>';
}
$slotsHtml = collect($slots)->map(function ($slot, $name) {
return "<template #{$name}>{$slot['slot']->toHtml()}</template>";
})->implode("\n");

if (str_contains($output, '<h2>Child component</h2>')) {
$output = str_replace('<p>Count: {{ count }}</p>', '<slot />', $output);
}

$this->pushSpladeTemplate($templateId, $output);
$slotKeys = collect($slots)
->keys()
->map(function ($name) {
return "'{$name}'";
})
->implode(', ');

$genericComponent = "<generic-splade-component {$attrs} :bridge=\"{$spladeBridgeHtml}\">
<template #default><slot /></template>
$genericComponent = "<generic-splade-component {$attrs} :bridge=\"{$spladeBridgeHtml}\" :slots=\"[{$slotKeys}]\">
{$slotsHtml}
</generic-splade-component>";

return static::$trackSpladeComponents
Expand Down

0 comments on commit 547ddcf

Please sign in to comment.