generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
760503f
commit 6f37ef6
Showing
7 changed files
with
170 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters