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

Commit

Permalink
Improve component finder (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 authored Jan 9, 2024
1 parent 3646926 commit 582cde0
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 82 deletions.
15 changes: 10 additions & 5 deletions src/Auth/Forms/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ public function rules(): array
];
}

protected function handle()
protected function handle(): void
{
if (Auth::attempt($this->only('email', 'password'), $this->remember)) {
session()->regenerate();
if (! Auth::attempt($this->only('email', 'password'), $this->remember)) {
$this->addError('email', __('These credentials do not match our records'));

return redirect()->intended();
return;
}

$this->addError('email', __('These credentials do not match our records'));
session()->regenerate();
}

protected function afterHandle(): mixed
{
return redirect()->intended();
}
}
25 changes: 17 additions & 8 deletions src/Auth/Forms/RegisterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Foxws\LivewireUse\Forms\Components\Form;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use Livewire\Attributes\Validate;
Expand Down Expand Up @@ -39,25 +38,35 @@ public function rules(): array
];
}

protected function handle()
protected function handle(): void
{
$data = $this->only('email', 'password');

$data['password'] = Hash::make($data['password']);

$user = $this->getUserModel()::create($data);
$user = $this->getUserModel()::create(
$this->getUserData()
);

request()->session()->regenerate();

auth()->login($user);

event(new Registered($user));
}

redirect()->to('/');
protected function afterHandle(): mixed
{
return redirect()->to('/');
}

protected function getUserModel(): User
{
return app(config('auth.providers.users.model'));
}

protected function getUserData(): array
{
$data = $this->only('email', 'password');

$data['password'] = Hash::make($data['password']);

return $data;
}
}
13 changes: 0 additions & 13 deletions src/Facades/LivewireUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace Foxws\LivewireUse\Facades;

use Foxws\LivewireUse\Auth\Controllers\LoginController;
use Foxws\LivewireUse\Auth\Controllers\LogoutController;
use Foxws\LivewireUse\Auth\Controllers\RegisterController;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Facades\Route;

/**
* @see \Foxws\LivewireUse\LivewireUse
Expand All @@ -17,13 +13,4 @@ protected static function getFacadeAccessor()
{
return \Foxws\LivewireUse\LivewireUse::class;
}

public static function routes(): void
{
Route::name('auth.')->group(function () {
Route::get('/login', LoginController::class)->name('login');
Route::get('/logout', LogoutController::class)->name('logout');
Route::get('/register', RegisterController::class)->name('register');
});
}
}
2 changes: 1 addition & 1 deletion src/Forms/Components/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function submit(): void
}
}

protected function handle()
protected function handle(): void
{
//
}
Expand Down
75 changes: 74 additions & 1 deletion src/LivewireUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,80 @@

namespace Foxws\LivewireUse;

use Foxws\LivewireUse\Auth\Controllers\LoginController;
use Foxws\LivewireUse\Auth\Controllers\LogoutController;
use Foxws\LivewireUse\Auth\Controllers\RegisterController;
use Foxws\LivewireUse\Support\Discover\ComponentScout;
use Foxws\LivewireUse\Support\Discover\LivewireScout;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Stringable;
use Livewire\Livewire;
use Spatie\StructureDiscoverer\Data\DiscoveredClass;


class LivewireUse
{
//
public static function routes(): void
{
Route::name('auth.')->group(function () {
Route::get('/login', LoginController::class)->name('login');
Route::get('/logout', LogoutController::class)->name('logout');
Route::get('/register', RegisterController::class)->name('register');
});
}

public static function registerComponents(
string $path,
string $namespace = 'App\\',
?string $prefix = null
): void
{
$components = ComponentScout::create()
->path($path)
->prefix($prefix)
->get();

collect($components)
->each(function (DiscoveredClass $class) use ($namespace) {
$name = static::componentName($class, $namespace);

Blade::component($class->getFcqn(), $name->value());
});
}

public static function registerLivewireComponents(
string $path,
string $namespace = 'App\\',
?string $prefix = null
): void
{
$components = LivewireScout::create()
->path($path)
->prefix($prefix)
->get();

collect($components)
->each(function (DiscoveredClass $class) use ($namespace) {
$name = static::componentName($class, $namespace);

Livewire::component($name->value(), $class->getFcqn());
});
}

public static function componentName(DiscoveredClass $class, string $namespace): Stringable
{
return str($class->name)
->kebab()
->prepend(static::componentPrefix($class, $namespace));
}

public static function componentPrefix(DiscoveredClass $class, string $namespace): Stringable
{
return str($class->namespace)
->after($namespace)
->match('/(.*)\\\\/')
->kebab()
->finish('-');
}
}
69 changes: 19 additions & 50 deletions src/LivewireUseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

namespace Foxws\LivewireUse;

use Foxws\LivewireUse\Support\Discover\ComponentScout;
use Foxws\LivewireUse\Support\Discover\LivewireScout;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Stringable;
use Livewire\Livewire;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\StructureDiscoverer\Data\DiscoveredClass;

class LivewireUseServiceProvider extends PackageServiceProvider
{
Expand All @@ -24,34 +18,11 @@ public function configurePackage(Package $package): void
public function bootingPackage(): void
{
$this
->registerComponents()
->registerFeatures()
->registerComponents()
->registerLivewire();
}

protected function registerComponents(): static
{
if (config('livewire-use.components_enabled') === false) {
return $this;
}

$components = ComponentScout::create()
->path(__DIR__)
->prefix('lw-use-blade')
->get();

collect($components)
->each(function (DiscoveredClass $class) {
$name = str($class->name)
->kebab()
->prepend(static::getComponentPrefix($class));

Blade::component($class->getFcqn(), $name->value());
});

return $this;
}

protected function registerFeatures(): static
{
foreach ([
Expand All @@ -65,32 +36,30 @@ protected function registerFeatures(): static
return $this;
}

protected function registerLivewire(): static
{
$components = LivewireScout::create()
->path(__DIR__)
->prefix('lw-use-livewire')
->get();

collect($components)
->each(function (DiscoveredClass $class) {
$name = str($class->name)
->kebab()
->prepend(static::getComponentPrefix($class));
protected function registerComponents(): static
{
if (config('livewire-use.components_enabled') === false) {
return $this;
}

Livewire::component($name->value(), $class->getFcqn());
});
LivewireUse::registerComponents(
path: __DIR__,
namespace: 'Foxws\\LivewireUse\\',
prefix: 'lw-components'
);

return $this;
}

protected static function getComponentPrefix(DiscoveredClass $class): Stringable
protected function registerLivewire(): static
{
return str($class->namespace)
->after('Foxws\\LivewireUse\\')
->match('/(.*)\\\\/')
->kebab()
->prepend(config('livewire-use.components_prefix'))
->finish('-');
LivewireUse::registerLivewireComponents(
path: __DIR__,
namespace: 'Foxws\\LivewireUse\\',
prefix: 'lw-livewire'
);

return $this;
}
}
11 changes: 7 additions & 4 deletions src/Support/Discover/ComponentScout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Foxws\LivewireUse\Support\Discover;

use Illuminate\Support\Stringable;
use Illuminate\View\Component;
use Spatie\StructureDiscoverer\Cache\DiscoverCacheDriver;
use Spatie\StructureDiscoverer\Cache\LaravelDiscoverCacheDriver;
use Spatie\StructureDiscoverer\Data\DiscoveredClass;
use Spatie\StructureDiscoverer\Discover;
use Spatie\StructureDiscoverer\StructureScout;

Expand All @@ -28,16 +30,17 @@ public function cacheDriver(): DiscoverCacheDriver
);
}

public function path(string $path): static

public function prefix(string $prefix): static
{
$this->path = $path;
$this->prefix = $prefix;

return $this;
}

public function prefix(string $prefix): static
public function path(string $path): static
{
$this->prefix = $prefix;
$this->path = $path;

return $this;
}
Expand Down

0 comments on commit 582cde0

Please sign in to comment.