diff --git a/src/AddSpladeToComponentData.php b/src/AddSpladeToComponentData.php index 2cb7180..4b639c0 100644 --- a/src/AddSpladeToComponentData.php +++ b/src/AddSpladeToComponentData.php @@ -5,6 +5,7 @@ use Illuminate\View\AnonymousComponent; use Illuminate\View\Component; use Illuminate\View\View; +use ProtoneMedia\SpladeCore\Facades\SpladePlugin; class AddSpladeToComponentData { @@ -29,6 +30,7 @@ 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(), diff --git a/src/Facades/SpladePlugin.php b/src/Facades/SpladePlugin.php index c34424d..c706405 100644 --- a/src/Facades/SpladePlugin.php +++ b/src/Facades/SpladePlugin.php @@ -3,13 +3,15 @@ namespace ProtoneMedia\SpladeCore\Facades; use Illuminate\Support\Facades\Facade; +use Illuminate\View\Component; use ProtoneMedia\SpladeCore\SpladePluginProvider; use ProtoneMedia\SpladeCore\SpladePluginRepository; /** * @method static bool isRefreshingComponent() - * @method static void register(SpladePluginProvider $provider) - * @method static array all() + * @method static void registerPluginProvider(SpladePluginProvider $provider) + * @method static void registerBladeComponent(string|Component $component) + * @method static bool bladeComponentIsRegistered(string|Component $component) * * @see \ProtoneMedia\SpladeCore\SpladePluginRepository */ diff --git a/src/SpladePluginRepository.php b/src/SpladePluginRepository.php index ce3754c..3852401 100644 --- a/src/SpladePluginRepository.php +++ b/src/SpladePluginRepository.php @@ -2,6 +2,8 @@ namespace ProtoneMedia\SpladeCore; +use Illuminate\View\Component; + class SpladePluginRepository { /** @@ -9,23 +11,43 @@ class SpladePluginRepository */ private array $plugins = []; - public function register(SpladePluginProvider $provider): void + /** + * @var Component[] + */ + private array $bladeComponents = []; + + public function registerPluginProvider(SpladePluginProvider $provider): void { $this->plugins[] = $provider; } + public function registerBladeComponent(string|Component $component): void + { + $this->bladeComponents[] = is_string($component) ? $component : get_class($component); + } + /** * @return SpladePluginProvider[] */ - public function all(): array + public function allPlugins(): array { return $this->plugins; } + /** + * @return Component[] + */ + public function bladeComponentIsRegistered(string|Component $component): bool + { + $componentClass = is_string($component) ? $component : get_class($component); + + return in_array($componentClass, $this->bladeComponents); + } + public function generateManifest(): void { - $imports = collect($this->all())->mapWithKeys(function (SpladePluginProvider $provider) { + $imports = collect($this->allPlugins())->mapWithKeys(function (SpladePluginProvider $provider) { $hash = 'S'.md5($provider->getLibraryBuildFilename()); $import = base_path('vendor/'.$provider->getComposerPackageName().'/dist/'.$provider->getLibraryBuildFilename());