Skip to content

Commit

Permalink
chore: Applied format
Browse files Browse the repository at this point in the history
  • Loading branch information
joserick committed Nov 3, 2024
1 parent 8714b5b commit 58c29e2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
21 changes: 14 additions & 7 deletions src/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function handle()

if ($class_namespaces->isEmpty()) {
$this->error('There are no Livewire-Discover prefixes defined.');

return;
}

Expand All @@ -34,14 +35,15 @@ public function handle()
}
}

if (!$composerClassLoader) {
if (! $composerClassLoader) {
$this->error('Composer ClassLoader not found.');

return;
}

$this->prefixes = $composerClassLoader->getPrefixesPsr4();

$this->info('List of Livewire Discover components:');
$this->info('Livewire-Discover namespaces list:');

foreach ($class_namespaces as $prefix => $class_namespace) {
$this->newLine();
Expand All @@ -50,18 +52,20 @@ public function handle()
if (isset($class_namespace['class_path'])) {
$path = $class_namespace['class_path'];
} else {
$this->warn("There is no \"class path\" defined for the config for prefix $prefix");
$this->warn("There is no \"class path\" defined for the config for prefix \"$prefix\"");
$this->comment('Getting the "class path" from the composer autoload file');
$path = $this->getClassPathFromNamespace($class_namespace['class_namespace']);
}

if(!$path) {
if (! $path) {
$this->error("Not found the \"class path\" in the composer autoload file for the namespace {$class_namespace['class_namespace']}");

continue;
}

if (!File::isDirectory($path)) {
if (! File::isDirectory($path)) {
$this->error("The directory $path of '{$class_namespace['class_namespace']}' does not exist or is not a directory.");

continue;
}

Expand All @@ -71,6 +75,7 @@ public function handle()

if (empty($phpFiles)) {
$this->warn("There are no Class files in the directory $path of '{$class_namespace['class_namespace']}'");

continue;
}

Expand All @@ -92,11 +97,13 @@ public function handle()
}
}

protected function getClassPathFromNamespace(string $namespace): ?string {
protected function getClassPathFromNamespace(string $namespace): ?string
{
foreach ($this->prefixes as $prefix => $dirs) {
if (strpos($namespace, $prefix) === 0) {
$relativePath = str_replace('\\', DIRECTORY_SEPARATOR, substr($namespace, strlen($prefix)));
return realpath(rtrim($dirs[0], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $relativePath);

return realpath(rtrim($dirs[0], DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$relativePath);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/LaravelLivewireDiscoverData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Joserick\LaravelLivewireDiscover;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;

class LaravelLivewireDiscoverData
{
Expand Down
22 changes: 11 additions & 11 deletions tests/Feature/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
$classNamespaces = collect([
'app-livewire' => [
'class_namespace' => 'App\\Livewire',
'class_path' => base_path('app/Livewire')
]
'class_path' => base_path('app/Livewire'),
],
]);

LaravelLivewireDiscover::shouldReceive('getClassNamespaces')
Expand All @@ -33,16 +33,16 @@
->once()
->with(base_path('app/Livewire'))
->andReturn([
new SplFileInfo(base_path('app/Livewire/ExampleComponent.php'))
new SplFileInfo(base_path('app/Livewire/ExampleComponent.php')),
]);

$this->artisan('livewire-discover:list')
->expectsOutput('List of Livewire Discover components:')
->expectsTable(
['Alias', 'Paths'],
[
['app-livewire.example-component', base_path('app/Livewire/ExampleComponent.php')]
]
)
->assertExitCode(0);
->expectsOutput('List of Livewire Discover components:')
->expectsTable(
['Alias', 'Paths'],
[
['app-livewire.example-component', base_path('app/Livewire/ExampleComponent.php')],
]
)
->assertExitCode(0);
});

0 comments on commit 58c29e2

Please sign in to comment.