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 f35e992 commit a6d9b7e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 141 deletions.
63 changes: 7 additions & 56 deletions src/Scout/ComponentScout.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,16 @@
namespace Foxws\WireUse\Scout;

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

class ComponentScout
class ComponentScout extends 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,
): 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
public function register(): void
{
return str($class->namespace)
->after($this->namespace)
->match('/(.*)\\\\/')
->replace('\\', '.')
->slug('.')
->finish('.');
$this->get()->each(fn (array $component) => Blade::component(
class: $component['class'],
alias: $component['name'],
));
}

protected function getComponentStructures(): ComponentStructureScout
Expand Down
87 changes: 2 additions & 85 deletions src/Scout/LivewireScout.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,102 +3,19 @@
namespace Foxws\WireUse\Scout;

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

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

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

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

protected function buildCollection(): 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
protected function getComponentStructures(): LivewireStructureScout
{
return LivewireStructureScout::create()
->path($this->path)
->prefix("livewire-structures-{$this->prefix}");
}

protected function getCacheKey(): string
{
return "wireuse.scout.livewire.{$this->prefix}";
}

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

protected function getCacheLifetime(): ?int
{
return config('wireuse.scout.cache_lifetime');
}
}
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,
): self {
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');
}
}

0 comments on commit a6d9b7e

Please sign in to comment.