pmjones/auto-shell
AutoShell auto-maps CLI command names to PHP command classes in a namespace. It reflects a command’s main method to parse args/options (scalars or arrays), then returns an Exec to run. Zero dependencies—add a class and it becomes a command.
artisan CLI but offers declarative command definition via PHP classes instead of XML/YAML.Project\Cli\Command\* namespace) with type-safe parameters (scalars, arrays, custom Options classes), reducing boilerplate compared to Laravel’s Command trait.Illuminate\Container) via the factory parameter, enabling testability and service binding (e.g., repositories, APIs).help:command) reduce documentation overhead, unlike Laravel’s manual --help implementations.Artisan for custom commands without forking core logic.Artisan commands (e.g., migrate, queue:work).bin/console.php setup (see Integration Approach).Command trait: Commands must use __invoke() or a custom method (e.g., execute()).Artisan commands live in Illuminate\Console\Command; AutoShell requires a separate namespace (e.g., App\Cli\Command).#[Option]), which aligns with Laravel 9+/10+.| Risk Area | Mitigation Strategy |
|---|---|
| Command Discovery | Use Laravel’s service provider to register AutoShell’s Console as a singleton, ensuring commands are discoverable via app('console'). |
| DI Integration | Bind AutoShell’s factory to Laravel’s container to resolve dependencies (e.g., DB, Cache). Example: factory: fn(string $class) => app($class). |
| Error Handling | AutoShell throws exceptions for invalid commands/options. Laravel’s exception handler can catch and log these (e.g., OptionAlreadyDefined). |
| Performance | Reflection overhead for command parsing. Benchmark against Laravel’s Command trait; consider caching parsed commands (e.g., via Symfony\Component\Cache). |
| Migration Path | Start with non-critical commands (e.g., app:clear-cache) before replacing core Artisan commands. |
CommandStarting, CommandFinished)?artisan (e.g., artisan app:hello) or standalone (e.g., php bin/console.php hello)?--env, --verbose) shared with Laravel’s Artisan?Console in PHPUnit for unit testing commands?Artisan::call)?bin/console.php is executable and discoverable in Laravel’s deployment pipeline?Artisan commands)?Command trait for internal tools (e.g., data imports, report generation).app:setup) with rich options/help.deploy:verify) or cron jobs.factory parameter maps to Laravel’s app() helper.config/console.php.command:executed) after AutoShell command completion.Phase 1: Pilot Commands
app:generate-key, app:log-cleanup).bin/console.php alongside Laravel’s artisan.app/Cli/Command/ with App\Cli\Command namespace.// app/Cli/Command/GenerateKey.php
namespace App\Cli\Command;
use AutoShell\Help;
#[Help("Generates a new application key.")]
class GenerateKey {
public function __invoke(): int { ... }
}
php bin/console.php generate-key).Phase 2: Artisan Integration
artisan.// app/Console/Commands/RunAutoShell.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RunAutoShell extends Command {
protected $signature = 'app:{command} {arguments}';
public function handle() {
$console = app('console');
return $console([...$_SERVER['argv']]);
}
}
app/Console/Kernel.php:
protected $commands = [
Commands\RunAutoShell::class,
];
php artisan app:generate-key.Phase 3: Full Replacement
Command trait for new commands.Command classes to AutoShell’s __invoke() pattern.use AutoShell\Options;
use Illuminate\Console\Command as LaravelCommand;
class HybridCommand extends LaravelCommand {
public function __invoke(Options $options) { ... }
}
| Feature | AutoShell Support | Laravel Artisan Support |
Notes |
|---|---|---|---|
| Options/Arguments | Yes (type-safe) | Yes | AutoShell uses #[Option] attributes. |
| Help System | Auto-generated manpages | Manual --help |
AutoShell’s help is more structured. |
| DI Integration | PSR-11 Container | Laravel Container | Requires factory binding. |
| Events | No | Yes (CommandStarting) |
Workaround: Trigger events post-exec. |
| Global Options | No | Yes (--env, --verbose) |
Use a GlobalOptions class. |
| Testing | Manual mocking | Artisan::call() |
Use Laravel’s Artisan facade. |
composer require pmjones/auto-shell.bin/console.php (see README).config/console.php:
return [
'namespace' => 'App\Cli\Command',
'directory' => app_path('Cli/Command'),
'factory' => fn(string $class) => app($class),
];
Console in a service provider:
$this->app->singleton('console', fn() => Console::new(
namespace: config('console.namespace'),
directory: config('console.directory'),
factory: config('console.factory'),
));
Hello.php).How can I help you explore Laravel packages today?