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
f35e992
commit a6d9b7e
Showing
3 changed files
with
104 additions
and
141 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
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,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'); | ||
} | ||
} |