Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve-scout #11

Merged
merged 11 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

All notable changes to `wireuse` will be documented in this file.

## v2.1.3 - 2024-07-24

**Full Changelog**: https://github.com/foxws/wireuse/compare/v2.1.2...v2.1.3

## v2.1.2 - 2024-07-20

**Full Changelog**: https://github.com/foxws/wireuse/compare/v2.1.1...v2.1.2
Expand Down
17 changes: 17 additions & 0 deletions config/wireuse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@
],
],

/*
|--------------------------------------------------------------------------
| Structure Discovery
|--------------------------------------------------------------------------
|
| This controls structure discovery.
|
| @doc https://github.com/spatie/php-structure-discoverer
|
*/

'scout' => [
'cache_store' => null,

'cache_lifetime' => 60 * 60 * 24 * 7,
],

];
24 changes: 24 additions & 0 deletions src/Scout/ComponentScout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Foxws\WireUse\Scout;

use Foxws\WireUse\Support\Discover\ComponentStructureScout;
use Illuminate\Support\Facades\Blade;

class ComponentScout extends Scout
{
public function register(): void
{
$this->get()->each(fn (array $component) => Blade::component(
class: $component['class'],
alias: $component['name'],
));
}

protected function getComponentStructures(): ComponentStructureScout
{
return ComponentStructureScout::create()
->path($this->path)
->prefix("blade-structures-{$this->prefix}");
}
}
21 changes: 21 additions & 0 deletions src/Scout/LivewireScout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Foxws\WireUse\Scout;

use Foxws\WireUse\Support\Discover\LivewireStructureScout;
use Livewire\Livewire;

class LivewireScout extends Scout
{
public function register(): void
{
$this->get()->each(fn (array $component) => Livewire::component(...$component));
}

protected function getComponentStructures(): LivewireStructureScout
{
return LivewireStructureScout::create()
->path($this->path)
->prefix("livewire-structures-{$this->prefix}");
}
}
95 changes: 95 additions & 0 deletions src/Scout/Scout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Foxws\WireUse\Scout;

use Foxws\WireUse\Support\Discover\ComponentStructureScout;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Spatie\StructureDiscoverer\Data\DiscoveredStructure;

abstract class Scout
{
public function __construct(
public ?string $path = null,
public ?string $namespace = null,
public ?string $prefix = null,
) {}

public static function create(
string $path,
string $namespace = 'App\\',
?string $prefix = null,
): static {
return new static(
path: $path,
namespace: $namespace,
prefix: $prefix,
);
}

public function get(): Collection
{
return Cache::store($this->getCacheStore())->remember(
$this->getCacheKey(),
$this->getCacheLifetime(),
fn () => $this->buildCollection()
);
}

abstract protected function getComponentStructures(): ComponentStructureScout;

protected function buildCollection(): Collection
{
$scout = $this->getComponentStructures();

return Collection::make($scout->get())
->map(fn (DiscoveredStructure $class) => [
'class' => $class->getFcqn(),
'name' => $this->componentName($class),
]);
}

protected function componentName(DiscoveredStructure $class): string
{
return str($class->name)
->kebab()
->prepend(
$this->componentPrefix(),
$this->componentNamespace($class)
);
}

protected function componentPrefix(): string
{
return str($this->prefix)
->replace('\\', '.')
->kebab();
}

protected function componentNamespace(DiscoveredStructure $class): string
{
return str($class->namespace)
->after($this->namespace)
->match('/(.*)\\\\/')
->replace('\\', '.')
->slug('.')
->finish('.');
}

protected function getCacheKey(): string
{
return str('wireuse')
->append(class_basename(static::class), $this->prefix)
->snake();
}

protected function getCacheStore(): ?string
{
return config('wireuse.scout.cache_store');
}

protected function getCacheLifetime(): ?int
{
return config('wireuse.scout.cache_lifetime');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Spatie\StructureDiscoverer\Discover;
use Spatie\StructureDiscoverer\StructureScout;

class ComponentScout extends StructureScout
class ComponentStructureScout extends StructureScout
{
public ?string $path = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Livewire\Component;
use Spatie\StructureDiscoverer\Discover;

class LivewireScout extends ComponentScout
class LivewireStructureScout extends ComponentStructureScout
{
protected function definition(): Discover
{
Expand Down
75 changes: 1 addition & 74 deletions src/WireUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,7 @@

namespace Foxws\WireUse;

use Foxws\WireUse\Support\Discover\ComponentScout;
use Foxws\WireUse\Support\Discover\LivewireScout;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Stringable;
use Livewire\LivewireManager;
use Spatie\StructureDiscoverer\Data\DiscoveredStructure;

class WireUse
{
public static function registerComponents(
string $path,
string $namespace = 'App\\',
string $prefix = '',
): void {
$scout = ComponentScout::create()
->path($path)
->prefix("laravel-components-{$prefix}")
->get();

collect($scout)
->each(function (DiscoveredStructure $class) use ($namespace, $prefix) {
$name = static::componentName($class, $namespace, $prefix);

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

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

$manager = app(LivewireManager::class);

collect($scout)
->each(function (DiscoveredStructure $class) use ($manager, $namespace, $prefix) {
$name = static::componentName($class, $namespace, $prefix);

$fcqn = $class->getFcqn();

if ($manager->isDiscoverable($fcqn)) {
$manager->component($name->value(), $fcqn);
}
});
}

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

public static function componentPrefix(string $prefix): Stringable
{
return str($prefix)
->kebab()
->finish('::');
}

public static function componentNamespace(DiscoveredStructure $class, string $namespace): Stringable
{
return str($class->namespace)
->after($namespace)
->match('/(.*)\\\\/')
->kebab()
->finish('-');
}
//
}
18 changes: 10 additions & 8 deletions src/WireUseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Foxws\WireUse;

use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Foxws\WireUse\Scout\ComponentScout;
use Foxws\WireUse\Scout\LivewireScout;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -12,21 +13,22 @@ public function configurePackage(Package $package): void
{
$package
->name('wireuse')
->hasConfigFile()
->hasViews()
->hasInstallCommand(function (InstallCommand $command) {
$command
->publishConfigFile();
});
->hasConfigFile();
}

public function bootingPackage(): void
public function packageBooted(): void
{
$this
->registerFeatures()
->registerMixins();
}

public function packageRegistered(): void
{
$this->app->singleton(ComponentScout::class, fn () => new ComponentScout);
$this->app->singleton(LivewireScout::class, fn () => new LivewireScout);
}

protected function registerFeatures(): static
{
foreach ([
Expand Down