Skip to content

Commit

Permalink
feat: Implementation of the 'singleton' functionality for storing cla…
Browse files Browse the repository at this point in the history
…sses and prefixes.
  • Loading branch information
joserick committed Feb 6, 2024
1 parent 7b35f07 commit 2c54cf4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/ComponentNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Joserick\LaravelLivewireDiscover\Exceptions;

use Livewire\Exceptions\ComponentNotFoundException as LivewireComponentNotFoundException;

class ComponentNotFoundException extends LivewireComponentNotFoundException
{
public function __construct(array $prefixes, LivewireComponentNotFoundException $previous = null) {
parent::__construct('(Also in the prefixes paths: '.implode(', ', array_keys($prefixes)).') '
. $previous->getMessage(), 0, $previous);
}
}
52 changes: 52 additions & 0 deletions src/LaravelLivewireDiscover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Joserick\LaravelLivewireDiscover;

use Illuminate\Support\Collection;
use Livewire\Mechanisms\ComponentRegistry;

class LaravelLivewireDiscover extends ComponentRegistry
{
private string $class_namespace;
private Collection $class_namespaces;

/**
* Create a new component resolver instance.
*/
public function __construct() {
$this->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;
}
}
13 changes: 6 additions & 7 deletions src/LaravelLivewireDiscoverComponentRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -25,15 +24,15 @@ 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);
}
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);
Expand Down
23 changes: 18 additions & 5 deletions src/LaravelLivewireDiscoverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion src/LaravelLivewireDiscoverServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Joserick\LaravelLivewireDiscover;

use Illuminate\Support\Facades\Blade;
use Livewire\LivewireManager;
use Livewire\Mechanisms\ComponentRegistry;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -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();
});
Expand Down

0 comments on commit 2c54cf4

Please sign in to comment.