Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 committed Jul 29, 2024
1 parent 760503f commit 6f37ef6
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 15 deletions.
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
74 changes: 74 additions & 0 deletions src/Scout/ComponentScout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Foxws\WireUse\Scout;

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

class ComponentScout
{
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,
): self
{
return new self(
path: $path,
namespace: $namespace,
prefix: $prefix,
);
}

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

namespace Foxws\WireUse\Scout;

use Foxws\WireUse\Support\Discover\LivewireStructureScout;
use Illuminate\Support\Collection;
use Livewire\Livewire;
use Spatie\StructureDiscoverer\Data\DiscoveredStructure;

class LivewireScout
{
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,
): self
{
return new self(
path: $path,
namespace: $namespace,
prefix: $prefix,
);
}

public function register(): void
{
$components = $this->get();

$components->each(fn (array $component) => Livewire::component(...$component));
}

public function get(): Collection
{
$scout = $this->getLivewireStructures();

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 getLivewireStructures(): LivewireStructureScout
{
return LivewireStructureScout::create()
->path($this->path)
->prefix("livewire-structures-{$this->prefix}");
}
}
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
2 changes: 1 addition & 1 deletion src/WireUse.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function componentName(DiscoveredStructure $class, string $namespa
);
}

public static function componentPrefix(string $prefix): Stringable
public static function componentPrefix(string $prefix): string
{
return str($prefix)
->kebab()
Expand Down
19 changes: 11 additions & 8 deletions src/WireUseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Foxws\WireUse;

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

Expand All @@ -12,21 +12,24 @@ 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()
);
}

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

0 comments on commit 6f37ef6

Please sign in to comment.