refactor: console improvements — auto-discovery, centralized bootstrap, per-command help
- ConsoleApplication.discoverCommands() scans lib/Console/Commands/ automatically; adding a new command no longer requires editing bin/console - Command base class provides bootstrapApp(), bootstrapModuleApp(), requireBinScript() and projectRoot() helpers — eliminates fragile 4-level relative require_once paths - Per-command --help support via optional usage() method on Command - Docker-compose scheduler updated to use bin/console scheduler:run - Old bin scripts now emit deprecation hint to stderr when called directly Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace MintyPHP\Console;
|
||||
|
||||
/**
|
||||
* Minimal CLI command dispatcher.
|
||||
* Minimal CLI command dispatcher with auto-discovery.
|
||||
*
|
||||
* Parses argv into command name, positional args, and --options,
|
||||
* then dispatches to the matching registered Command instance.
|
||||
@@ -18,6 +18,63 @@ final class ConsoleApplication
|
||||
$this->commands[$command->name()] = $command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Auto-discover and register all Command subclasses in a directory.
|
||||
*
|
||||
* Scans PHP files recursively, extracts the FQCN from namespace + class name,
|
||||
* and registers any concrete Command subclass found.
|
||||
*/
|
||||
public function discoverCommands(string $directory): void
|
||||
{
|
||||
if (!is_dir($directory)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$iterator = new \RecursiveIteratorIterator(
|
||||
new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS),
|
||||
\RecursiveIteratorIterator::LEAVES_ONLY
|
||||
);
|
||||
|
||||
foreach ($iterator as $file) {
|
||||
if ($file->getExtension() !== 'php') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$content = file_get_contents($file->getPathname());
|
||||
if ($content === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Extract namespace and class name from file
|
||||
$namespace = '';
|
||||
if (preg_match('/^namespace\s+(.+?);/m', $content, $nsMatch)) {
|
||||
$namespace = $nsMatch[1];
|
||||
}
|
||||
|
||||
$className = '';
|
||||
if (preg_match('/^(?:final\s+|abstract\s+)?class\s+(\w+)/m', $content, $classMatch)) {
|
||||
$className = $classMatch[1];
|
||||
}
|
||||
|
||||
if ($className === '' || $namespace === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fqcn = $namespace . '\\' . $className;
|
||||
|
||||
if (!class_exists($fqcn)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$reflection = new \ReflectionClass($fqcn);
|
||||
if ($reflection->isAbstract() || !$reflection->isSubclassOf(Command::class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->register($reflection->newInstance());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $argv Raw CLI arguments (including script name at [0])
|
||||
*/
|
||||
@@ -38,6 +95,18 @@ final class ConsoleApplication
|
||||
|
||||
[$args, $options] = self::parseArgv(array_slice($argv, 2));
|
||||
|
||||
// Per-command --help
|
||||
if ($options['help'] ?? false) {
|
||||
$command = $this->commands[$commandName];
|
||||
$usage = $command->usage();
|
||||
if ($usage !== '') {
|
||||
fwrite(STDOUT, $usage . "\n");
|
||||
} else {
|
||||
fwrite(STDOUT, sprintf("%s — %s\n", $command->name(), $command->description()));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $this->commands[$commandName]->execute($args, $options);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user