Skip to content

Commit

Permalink
Merge pull request #274 from dissto/add-todos-helper-command
Browse files Browse the repository at this point in the history
Add little internal helper command to output todos
  • Loading branch information
dissto authored Jun 15, 2024
2 parents 0ee2fbd + 780c6b9 commit 295bbe4
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src/Commands/ListTodosCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace CodeWithDennis\FilamentTests\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use ReflectionClass;

class ListTodosCommand extends Command
{
protected $signature = 'make:filament-test-todo-list';

protected $description = '(internal) Output a list of todos for Filament tests';

protected Filesystem $files;

public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}

public function handle(): int
{
$files = $this->files->allFiles(__DIR__.'/../Stubs');
$todos = collect($files)
->map(fn ($file) => $this->getClassFromFile($file->getPathname()))
->filter(fn ($class) => $class && $this->hasTodoProperty($class))
->values()
->all();

$this->outputTodos($todos);

return self::SUCCESS;
}

protected function getClassFromFile(string $filePath): ?string
{
$contents = $this->files->get($filePath);
$namespace = $this->getNamespaceFromFile($contents);
$className = $this->getClassNameFromFile($contents);

return $namespace && $className ? $namespace.'\\'.$className : null;
}

protected function getNamespaceFromFile(string $fileContents): ?string
{
return str($fileContents)->match('/namespace\s+(.+?);/') ?: null;
}

protected function getClassNameFromFile(string $fileContents): ?string
{
return str($fileContents)->match('/class\s+(\w+)/') ?: null;
}

protected function hasTodoProperty(string $class): bool
{
if (! class_exists($class)) {
return false;
}

$reflection = new ReflectionClass($class);
if ($reflection->hasProperty('isTodo')) {
$property = $reflection->getProperty('isTodo');
$instance = $reflection->newInstanceWithoutConstructor();

return $property->getValue($instance) === true;
}

return false;
}

protected function outputTodos(array $todos): void
{
$this->table(['Todos '.count($todos)], collect($todos)->map(function ($todo) {
$todo = str($todo)->trim('CodeWithDennis\FilamentTests\Stubs\\');

return [$todo];
})->all());
}
}
6 changes: 5 additions & 1 deletion src/FilamentTestsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodeWithDennis\FilamentTests;

use CodeWithDennis\FilamentTests\Commands\FilamentTestsCommand;
use CodeWithDennis\FilamentTests\Commands\ListTodosCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

Expand All @@ -13,7 +14,10 @@ public function configurePackage(Package $package): void
$package
->name('filament-tests')
->hasConfigFile()
->hasCommand(FilamentTestsCommand::class);
->hasCommands([
FilamentTestsCommand::class,
ListTodosCommand::class,
]);
}

public function boot(): void
Expand Down

0 comments on commit 295bbe4

Please sign in to comment.