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

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Nov 28, 2023
1 parent 9d30fae commit 8240430
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/AddSpladeToComponentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ public function __invoke(Component $component, array &$data, string $hash, mixed
$componentHelper = app(ComponentHelper::class);

$viewContents = $componentHelper->filesystem->get(
$componentHelper->getPath($view)
$path = $componentHelper->getPath($view)
);

if (SpladePlugin::bladeComponentIsRegistered($component)) {
SpladePlugin::dontGenerateVueComponentForPath($path);
}

if (! str_starts_with(trim($viewContents), '<script setup')) {
// No Vue 3 script setup, so no need to add the Splade bridge.
return;
Expand All @@ -30,7 +34,6 @@ public function __invoke(Component $component, array &$data, string $hash, mixed
$key = 'spladeBridge';

$data[$key] = ComponentSerializer::make($component)->toArray([
'is_registered' => SpladePlugin::bladeComponentIsRegistered($component),
'template_hash' => $hash,
'original_url' => url()->current(),
'original_verb' => request()->method(),
Expand Down
14 changes: 10 additions & 4 deletions src/BladeViewExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
use InvalidArgumentException;
use ProtoneMedia\SpladeCore\Facades\SpladePlugin;

class BladeViewExtractor
{
Expand All @@ -17,6 +18,8 @@ class BladeViewExtractor

protected string $viewWithoutScriptTag;

protected ComponentHelper $componentHelper;

protected ScriptParser $scriptParser;

public function __construct(
Expand All @@ -27,6 +30,8 @@ public function __construct(
if (! Str::endsWith($bladePath, '.blade.php')) {
throw new InvalidArgumentException("The Blade Path must end with '.blade.php'.");
}

$this->componentHelper = app(ComponentHelper::class);
}

/**
Expand Down Expand Up @@ -109,6 +114,10 @@ public function handle(Filesystem $filesystem): string
$this->viewWithoutScriptTag = $this->replaceComponentMethodLoadingStates($this->viewWithoutScriptTag);
$this->viewWithoutScriptTag = $this->replaceElementRefs($this->viewWithoutScriptTag);

if (SpladePlugin::dontGenerateVueComponentForPath($this->bladePath)) {
return $this->viewWithoutScriptTag;
}

// Adjust the current defineProps, or generate a new one if it didn't exist yet.
[$script, $defineProps] = $this->extractDefinePropsFromScript();

Expand Down Expand Up @@ -171,10 +180,7 @@ protected function getTag(): string
return $this->data['spladeBridge']['tag'];
}

/** @var ComponentHelper */
$componentHelper = app(ComponentHelper::class);

return $componentHelper->getTag($this->bladePath);
return $this->componentHelper->getTag($this->bladePath);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Facades/SpladePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use ProtoneMedia\SpladeCore\SpladePluginRepository;

/**
* @method static bool isRefreshingComponent()
* @method static void registerPluginProvider(SpladePluginProvider $provider)
* @method static void registerBladeComponent(string|Component $component)
* @method static bool bladeComponentIsRegistered(string|Component $component)
* @method static void dontGenerateVueComponentForPath(string $path)
* @method static bool shouldGenerateVueComponentForPath(string $path)
*
* @see \ProtoneMedia\SpladeCore\SpladePluginRepository
*/
Expand Down
24 changes: 15 additions & 9 deletions src/SpladePluginRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,23 @@ class SpladePluginRepository
*/
private array $bladeComponents = [];

private array $dontGenerateVueComponentForPaths = [];

public function registerPluginProvider(SpladePluginProvider $provider): void
{
$this->plugins[] = $provider;
$this->plugins[get_class($provider)] = $provider;
}

public function registerBladeComponent(string|Component $component): void
{
$this->bladeComponents[] = is_string($component) ? $component : get_class($component);
$componentClass = is_string($component) ? $component : get_class($component);

$this->bladeComponents[$componentClass] = true;
}

/**
* @return SpladePluginProvider[]
*/
public function allPlugins(): array
public function dontGenerateVueComponentForPath(string $path): void
{
return $this->plugins;
$this->dontGenerateVueComponentForPaths[$path] = true;
}

/**
Expand All @@ -41,13 +42,18 @@ public function bladeComponentIsRegistered(string|Component $component): bool
{
$componentClass = is_string($component) ? $component : get_class($component);

return in_array($componentClass, $this->bladeComponents);
return array_key_exists($componentClass, $this->bladeComponents);
}

public function shouldGenerateVueComponentForPath(string $path): bool
{
return ! array_key_exists($path, $this->dontGenerateVueComponentForPaths);
}

public function generateManifest(): void
{

$imports = collect($this->allPlugins())->mapWithKeys(function (SpladePluginProvider $provider) {
$imports = collect($this->plugins)->mapWithKeys(function (SpladePluginProvider $provider) {
$hash = 'S'.md5($provider->getLibraryBuildFilename());

$import = base_path('vendor/'.$provider->getComposerPackageName().'/dist/'.$provider->getLibraryBuildFilename());
Expand Down

0 comments on commit 8240430

Please sign in to comment.