From 2c54cf4c650331e70b9d26b74194f631c6a232c0 Mon Sep 17 00:00:00 2001 From: Jose Erick Carreon Date: Tue, 6 Feb 2024 15:17:29 -0600 Subject: [PATCH] feat: Implementation of the 'singleton' functionality for storing classes and prefixes. --- README.md | 2 +- src/Exceptions/ComponentNotFoundException.php | 13 +++++ src/LaravelLivewireDiscover.php | 52 +++++++++++++++++++ ...ravelLivewireDiscoverComponentRegistry.php | 13 +++-- src/LaravelLivewireDiscoverManager.php | 23 ++++++-- ...LaravelLivewireDiscoverServiceProvider.php | 5 +- 6 files changed, 94 insertions(+), 14 deletions(-) create mode 100644 src/Exceptions/ComponentNotFoundException.php create mode 100644 src/LaravelLivewireDiscover.php diff --git a/README.md b/README.md index 4ee1dce..0863a2a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ php artisan vendor:publish --tag livewire-discover-config ``` php // Load the namespace to Livewire components. 'class_namespaces' => [ - // 'prefix' => '//namespace//', + // 'prefix' => 'namespace\\package', 'my-components' => 'Namespace\\Livewire', 'new-components' => 'User\\Repository\\Livewire', ], diff --git a/src/Exceptions/ComponentNotFoundException.php b/src/Exceptions/ComponentNotFoundException.php new file mode 100644 index 0000000..456fa39 --- /dev/null +++ b/src/Exceptions/ComponentNotFoundException.php @@ -0,0 +1,13 @@ +getMessage(), 0, $previous); + } +} diff --git a/src/LaravelLivewireDiscover.php b/src/LaravelLivewireDiscover.php new file mode 100644 index 0000000..40f19e2 --- /dev/null +++ b/src/LaravelLivewireDiscover.php @@ -0,0 +1,52 @@ +class_namespace = config('livewire.class_namespace'); + $this->class_namespaces = collect(config('laravel-livewire-discover.class_namespaces')); + } + + /** + * Add a class / namespace prefix to the resolver. + * + * @param string $prefix + * @param string $namespace + * @return void + */ + public function add(string $prefix, string $namespace) : void + { + $this->class_namespaces->put($prefix, $namespace); + } + + /** + * Get the livewire class namespace. + * + * @return string + */ + public function getClassNamespace() : string + { + return $this->class_namespace; + } + + /** + * Get the collection of class namespaces to discover. + * + * @return string + */ + public function getClassNamespaces() : Collection + { + return $this->class_namespaces; + } +} diff --git a/src/LaravelLivewireDiscoverComponentRegistry.php b/src/LaravelLivewireDiscoverComponentRegistry.php index 45e720f..afe9d20 100644 --- a/src/LaravelLivewireDiscoverComponentRegistry.php +++ b/src/LaravelLivewireDiscoverComponentRegistry.php @@ -4,19 +4,18 @@ use ArrayIterator; use Illuminate\Support\Str; -use Livewire\Exceptions\ComponentNotFoundException; +use Joserick\LaravelLivewireDiscover\Exceptions\ComponentNotFoundException; +use Livewire\Exceptions\ComponentNotFoundException as LivewireComponentNotFoundException; use Livewire\Mechanisms\ComponentRegistry; class LaravelLivewireDiscoverComponentRegistry extends ComponentRegistry { protected function getNameAndClass($nameComponentOrClass) { - $class_namespace = config('livewire.class_namespace'); - $name_class = $this->getNameAndClassDiscovered($nameComponentOrClass, - collect(config('laravel-livewire-discover.class_namespaces'))->getIterator()); + app(LaravelLivewireDiscover::class)->getClassNamespaces()->getIterator()); - config(['livewire.class_namespace' => $class_namespace]); + config(['livewire.class_namespace' => app(LaravelLivewireDiscover::class)->getClassNamespace()]); return $name_class; } @@ -25,7 +24,7 @@ private function getNameAndClassDiscovered($nameComponentOrClass, ArrayIterator { try { return parent::getNameAndClass($nameComponentOrClass); - } catch (ComponentNotFoundException $th) { + } catch (LivewireComponentNotFoundException $th) { if ($class_namespaces->valid()) { if (Str::startsWith($nameComponentOrClass, $class_namespaces->key())) { $nameComponentOrClass = Str::replaceFirst($class_namespaces->key().'-', '', $nameComponentOrClass); @@ -33,7 +32,7 @@ private function getNameAndClassDiscovered($nameComponentOrClass, ArrayIterator config(['livewire.class_namespace' => $class_namespaces->current()]); $class_namespaces->next(); } else { - throw $th; + throw new ComponentNotFoundException($class_namespaces->getArrayCopy(), $th); } return $this->getNameAndClassDiscovered($nameComponentOrClass, $class_namespaces); diff --git a/src/LaravelLivewireDiscoverManager.php b/src/LaravelLivewireDiscoverManager.php index 55a9b11..ea50498 100644 --- a/src/LaravelLivewireDiscoverManager.php +++ b/src/LaravelLivewireDiscoverManager.php @@ -6,13 +6,26 @@ class LaravelLivewireDiscoverManager extends LivewireManager { - public function componentNamespace(string $namespace, string $prefix) { - $class_namespaces = collect(config('laravel-livewire-discover.class_namespaces')); - $class_namespaces->put($prefix, $namespace); - config(['laravel-livewire-discover.class_namespaces' => $class_namespaces->toArray()]); + /** + * Add a class / namespace prefix to the resolver. + * + * @param string $namespace + * @param string $prefix + * @return void + */ + public function componentNamespace(string $namespace, string $prefix): void + { + app(LaravelLivewireDiscover::class)->add($prefix, $namespace); } - public function discover(string $namespace, string $prefix) + /** + * Discover the components of the given namespace. + * + * @param string $namespace + * @param string $prefix + * @return void + */ + public function discover(string $namespace, string $prefix): void { $this->componentNamespace($namespace, $prefix); } diff --git a/src/LaravelLivewireDiscoverServiceProvider.php b/src/LaravelLivewireDiscoverServiceProvider.php index 53a58d0..40379bf 100644 --- a/src/LaravelLivewireDiscoverServiceProvider.php +++ b/src/LaravelLivewireDiscoverServiceProvider.php @@ -2,7 +2,6 @@ namespace Joserick\LaravelLivewireDiscover; -use Illuminate\Support\Facades\Blade; use Livewire\LivewireManager; use Livewire\Mechanisms\ComponentRegistry; use Spatie\LaravelPackageTools\Package; @@ -22,6 +21,10 @@ public function configurePackage(Package $package): void ->name('laravel-livewire-discover') ->hasConfigFile('laravel-livewire-discover'); + $this->app->alias(LaravelLivewireDiscover::class, 'laravel-livewire-discover'); + + $this->app->singleton(LaravelLivewireDiscover::class); + $this->app->extend(LivewireManager::class, function () { return new LaravelLivewireDiscoverManager(); });