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

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Oct 18, 2023
1 parent 01f5bd7 commit 516dad4
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 118 deletions.
1 change: 0 additions & 1 deletion app/resources/views/components/layout.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<script> @stack('splade-templates') </script>

<div id="app">
<div>SPLADE CORE</div>
{{ $slot }}
</div>
</body>
Expand Down
1 change: 0 additions & 1 deletion app/tests/Browser/AnonymousTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function it_handles_anonymous_components()
{
$this->browse(function (Browser $browser) {
$browser->visit('/anonymous')
->waitForText('SPLADE CORE')
->type('message', 'Hello World')
->assertSeeIn('@reversed', 'dlroW olleH');
});
Expand Down
2 changes: 2 additions & 0 deletions app/tests/Feature/DemoRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public static function demoRoutes(): array
['/refresh-state'],
['/refresh'],
['/two-way-binding'],
['/base-view'],
['/regular-view'],
];
}

Expand Down
47 changes: 22 additions & 25 deletions src/AddSpladeToComponentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,34 @@

class AddSpladeToComponentData
{
public static function callback(): callable
public function __invoke(Component $component, array &$data, string $hash, mixed $view = null): void
{
return function (Component $component, array &$data, string $hash, mixed $view = null) {
if (! ($view instanceof View || $component instanceof AnonymousComponent)) {
return;
}

if (! ($view instanceof View || $component instanceof AnonymousComponent)) {
return;
}
/** @var ComponentHelper */
$componentHelper = app(ComponentHelper::class);

/** @var ComponentHelper */
$componentHelper = app(ComponentHelper::class);
$viewContents = $componentHelper->filesystem->get(
$componentHelper->getPath($view)
);

$viewContents = $componentHelper->filesystem->get(
$componentHelper->getPath($view)
);
if (! str_starts_with(trim($viewContents), '<script setup')) {
// No Vue 3 script setup, so no need to add the Splade bridge.
return;
}

if (! str_starts_with(trim($viewContents), '<script setup')) {
// No Vue 3 script setup, so no need to add the Splade bridge.
return;
}
$key = 'spladeBridge';

$key = 'spladeBridge';
$data[$key] = ComponentSerializer::make($component)->toArray([
'template_hash' => $hash,
'original_url' => url()->current(),
'original_verb' => request()->method(),
]);

$data[$key] = ComponentSerializer::make($component)->toArray([
'template_hash' => $hash,
'original_url' => url()->current(),
'original_verb' => request()->method(),
]);

if ($view instanceof View) {
$view->with($key, $data[$key]);
}
};
if ($view instanceof View) {
$view->with($key, $data[$key]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
use InvalidArgumentException;

class ExtractVueScriptFromBladeView
class BladeViewExtractor
{
protected readonly string $originalScript;

Expand Down Expand Up @@ -47,6 +46,13 @@ protected function isComponent(): bool
&& Str::contains($this->bladePath, '/components/');
}

public function getPendingView(): PendingView
{
$this->splitOriginalView();

return PendingView::from($this->viewWithoutScriptTag, $this->bladePath, $this->viewRootLayoutTags);
}

/**
* Check if the view has a <script setup> tag.
*/
Expand Down Expand Up @@ -83,17 +89,10 @@ protected function extractWrappedViewInRootLayout(): void
];
}

public function getPendingView(): PendingView
{
$this->splitOriginalView();

return PendingView::from($this->viewWithoutScriptTag, $this->bladePath, $this->viewRootLayoutTags);
}

/**
* Handle the extraction of the Vue script. Returns the view without the <script setup> tag.
*/
public function handle(Filesystem $filesystem): string|PendingView
public function handle(Filesystem $filesystem): string
{
if (! $this->hasScriptSetup()) {
// The view does not contain a <script setup> tag, so we don't need to do anything.
Expand Down Expand Up @@ -133,9 +132,7 @@ public function handle(Filesystem $filesystem): string|PendingView
Process::path(base_path())->run("node_modules/.bin/eslint --fix {$vuePath}");
}

return $this->isComponent()
? $this->viewWithoutScriptTag
: PendingView::from($this->viewWithoutScriptTag, $this->bladePath, $this->viewRootLayoutTags);
return $this->viewWithoutScriptTag;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/BuildComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace ProtoneMedia\SpladeCore\Commands;

use Illuminate\Console\Command;
use ProtoneMedia\SpladeCore\BladeViewExtractor;
use ProtoneMedia\SpladeCore\ComponentHelper;
use ProtoneMedia\SpladeCore\ComponentSerializer;
use ProtoneMedia\SpladeCore\ExtractVueScriptFromBladeView;
use Symfony\Component\Finder\SplFileInfo;

class BuildComponents extends Command
Expand Down Expand Up @@ -52,7 +52,7 @@ public function handle(ComponentHelper $componentHelper): int

$componentClass = $componentHelper->getClass($viewPath);

ExtractVueScriptFromBladeView::from($contents, ['spladeBridge' => [
BladeViewExtractor::from($contents, ['spladeBridge' => [
'data' => $componentClass ? ComponentSerializer::getDataFromComponentClass($componentClass) : [],
'tag' => $componentHelper->getTag($viewPath),
'functions' => $componentClass ? ComponentSerializer::getFunctionsFromComponentClass($componentClass) : [],
Expand Down
25 changes: 9 additions & 16 deletions src/PendingView.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,37 @@

class PendingView
{
public readonly string $originalView;

public function __construct(
public readonly string $viewWithoutScript,
public readonly string $tag,
public readonly array $rootLayoutTags
) {
}

public function setOriginalView(string $originalView): self
public static function from(string $viewWithoutScript, string $path, array $rootLayoutTags)
{
$this->originalView = $originalView;
/** @var ComponentHelper */
$componentHelper = app(ComponentHelper::class);

return $this;
return new static($viewWithoutScript, $componentHelper->getTag($path), $rootLayoutTags);
}

public function render(string $hash): string
public function render(string $templateId): string
{
$tag = Str::kebab($this->tag);

$component = "<{$tag} splade-template-id=\"{$hash}\"></{$tag}>";
$component = "<{$tag} splade-template-id=\"{$templateId}\"></{$tag}>";

if (empty($this->rootLayoutTags)) {
return $component;
}

return Blade::render(<<<HTML
$rootLayout = Blade::render(<<<HTML
{$this->rootLayoutTags[0]}
$component
###SPLADE-INJECT-HERE###
{$this->rootLayoutTags[1]}
HTML);
}

public static function from(string $viewWithoutScript, string $path, array $rootLayoutTags)
{
/** @var ComponentHelper */
$componentHelper = app(ComponentHelper::class);

return new static($viewWithoutScript, $componentHelper->getTag($path), $rootLayoutTags);
return str_replace('###SPLADE-INJECT-HERE###', $component, $rootLayout);
}
}
2 changes: 0 additions & 2 deletions src/SpladeCoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ protected function registerFactory()

return $factory;
});

Factory::beforeStartComponent(AddSpladeToComponentData::callback());
}

public function packageBooted()
Expand Down
24 changes: 4 additions & 20 deletions src/View/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace ProtoneMedia\SpladeCore\View;

use Illuminate\Support\Str;
use Illuminate\View\Compilers\BladeCompiler as BaseBladeCompiler;
use ProtoneMedia\SpladeCore\ExtractVueScriptFromBladeView;
use ProtoneMedia\SpladeCore\BladeViewExtractor;

class BladeCompiler extends BaseBladeCompiler
{
Expand All @@ -22,25 +21,10 @@ class BladeCompiler extends BaseBladeCompiler
*/
public function compileString($value): string
{
$service = ExtractVueScriptFromBladeView::from($value, $this->data, $this->getPath());
$result = BladeViewExtractor::from($value, $this->data, $this->getPath())
->handle($this->files);

$result = $service->handle($this->files);

if (is_string($result)) {
return parent::compileString($result);
}

// TODO: move to CompilerEngine::get()
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 9);

// prevent leaking the full path
$path = Str::after($trace[8]['file'], base_path());

$hash = md5($path.'.'.$trace[8]['line']);

$this->pendingViews[$hash] = $result->setOriginalView($value);

return parent::compileString($result->viewWithoutScript);
return parent::compileString($result);
}

/**
Expand Down
27 changes: 9 additions & 18 deletions src/View/CompilerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;
use Illuminate\View\Engines\CompilerEngine as BaseCompilerEngine;
use ProtoneMedia\SpladeCore\BladeViewExtractor;
use ProtoneMedia\SpladeCore\ComponentHelper;
use ProtoneMedia\SpladeCore\ExtractVueScriptFromBladeView;

class CompilerEngine extends BaseCompilerEngine
{
Expand All @@ -28,19 +28,17 @@ public function setComponentHelper(ComponentHelper $componentHelper): self
public function get($path, array $data = [])
{
/** @var BladeCompiler */
$compiler = $this->compiler;
$compiler = $this->getCompiler();
$compiler->setData($data);

$isComponent = str_contains($path, DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR);

if ($isComponent) {
if (! str_starts_with($path, config('view.compiled'))) {
$vueComponent = $this->componentHelper->getTag($path).'.vue';

// Delete the compiled script if the Vue component is not found,
// for example, when the compiled component is deleted but not
// the compiled template.
if (! $this->files->exists(config('splade-core.compiled_scripts').DIRECTORY_SEPARATOR.$vueComponent)) {
$this->files->delete($this->getCompiler()->getCompiledPath($path));
if (! $this->files->exists(config('splade-core.compiled_scripts').'/'.$vueComponent)) {
$this->files->delete($compiler->getCompiledPath($path));
}
}

Expand All @@ -49,27 +47,20 @@ public function get($path, array $data = [])
fn () => $compiler->setData([])
);

if ($isComponent) {
if (str_contains($path, '/components/')) {
return $result;
}

$service = ExtractVueScriptFromBladeView::from($this->files->get($path), $data, $path);
$service = BladeViewExtractor::from($this->files->get($path), $data, $path);

if (! $service->hasScriptSetup()) {
return $result;
}

$pendingView = $service->getPendingView();

$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 6);

// prevent leaking the full path
$tracePath = Str::after($trace[5]['file'], base_path());

$hash = md5($tracePath.'.'.$trace[5]['line']);
$hash = md5(Str::random());

app('view')->pushSpladeTemplate($hash, $result);

return $pendingView->render($hash);
return $service->getPendingView()->render($hash);
}
}
23 changes: 3 additions & 20 deletions src/View/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

namespace ProtoneMedia\SpladeCore\View;

use App\View\Components\Layout;
use Illuminate\Support\Js;
use Illuminate\Support\Str;
use Illuminate\View\Component;
use Illuminate\View\ComponentAttributeBag;
use Illuminate\View\Factory as BaseFactory;
use ProtoneMedia\SpladeCore\AddSpladeToComponentData;

class Factory extends BaseFactory
{
protected static bool $trackSpladeComponents = false;

protected static array $spladeComponents = [];

protected static array $beforeStartComponentCallbacks = [];

public static function trackSpladeComponents(): void
{
static::$trackSpladeComponents = true;
Expand All @@ -33,35 +31,20 @@ public static function getSpladeComponent(string $key): ?string
return static::$spladeComponents[$key] ?? null;
}

/**
* Register a callback to be called before the component is started.
*/
public static function beforeStartComponent(callable $callback): void
{
static::$beforeStartComponentCallbacks[] = $callback;
}

/**
* Execute the callback before the component is started.
*/
public function startComponent($view, array $data = [], $component = null)
{
if ($component instanceof Layout) {
return parent::startComponent($view, $data);
}

if ($component instanceof Component && ! empty(static::$beforeStartComponentCallbacks)) {
if ($component instanceof Component) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);

// prevent leaking the full path
$path = Str::after($trace[0]['file'], base_path());

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

foreach (static::$beforeStartComponentCallbacks as $callback) {
$callback = $callback->bindTo($this, static::class);
$callback($component, $data, $hash, $view);
}
(new AddSpladeToComponentData)($component, $data, $hash, $view);
}

return parent::startComponent($view, $data);
Expand Down

0 comments on commit 516dad4

Please sign in to comment.