elliephp/console
最新稳定版本:1.0.0
Composer 安装命令:
composer require elliephp/console
包简介
Symfony Console-based CLI component for ElliePHP microframework
README 文档
README
A lightweight PSR-11 container-aware wrapper around Symfony Console.
Installation
composer require elliephp/console
Features
- Minimal wrapper around Symfony Console
- Optional PSR-11 container integration
- Framework agnostic
- No framework-specific commands
Usage
Basic Setup (No Container)
#!/usr/bin/env php <?php require __DIR__ . '/vendor/autoload.php'; use ElliePHP\Console\Application; $app = new Application(); $app->add(new YourCommand()); $app->run();
With Container (Optional)
#!/usr/bin/env php <?php require __DIR__ . '/vendor/autoload.php'; use ElliePHP\Console\Application; $container = require __DIR__ . '/bootstrap/container.php'; $app = new Application($container, 'My App', '1.0.0'); $app->addCommands([ YourCommand::class, // Resolved from container ]); $app->run();
Creating Commands
<?php namespace App\Console; use ElliePHP\Console\Command\BaseCommand; use Symfony\Component\Console\Input\InputArgument; class GreetCommand extends BaseCommand { protected function configure(): void { $this->setName('greet') ->setDescription('Greet someone') ->addArgument('name', InputArgument::OPTIONAL, 'Name to greet', 'World'); } protected function handle(): int { $name = $this->argument('name'); $this->success("Hello, {$name}!"); return self::SUCCESS; } }
Registering Commands
// Without container $app = new Application(); $app->add(new GreetCommand()); // With container (optional) $app = new Application($container); $app->addCommands([ new GreetCommand(), // Direct instance AnotherCommand::class, // Resolved from container if available ]); $app->run();
BaseCommand Helpers
Output Methods
// Basic output $this->success('Success message'); $this->error('Error message'); $this->info('Info message'); $this->warning('Warning message'); $this->note('Note message'); $this->comment('Comment message'); // Structured output $this->title('Section Title'); $this->section('Subsection'); $this->table(['Header1', 'Header2'], [['Row1', 'Row2']]); // Interactive $name = $this->ask('What is your name?', 'Guest'); $confirm = $this->confirm('Continue?', true); $choice = $this->choice('Select option', ['a', 'b', 'c'], 'a'); // Raw output $this->line('A line of text'); $this->write('Text without newline');
Accessing Input
// Get arguments $name = $this->argument('name'); // Get options $verbose = $this->option('verbose');
Container Access
// Access container (only if provided to Application) if ($this->container) { $service = $this->container->get('service'); }
Exit Codes
return self::SUCCESS; // Command succeeded return self::FAILURE; // Command failed return self::INVALID; // Invalid usage
Quick Reference
Minimal Command
class MyCommand extends BaseCommand { protected function configure(): void { $this->setName('my:command') ->setDescription('Does something'); } protected function handle(): int { $this->success('Done!'); return self::SUCCESS; } }
Common Patterns
// Get input $name = $this->argument('name'); $verbose = $this->option('verbose'); // Output $this->success('Success!'); $this->error('Error!'); $this->info('Info'); $this->table(['Col1', 'Col2'], $rows); // Interactive $answer = $this->ask('Question?', 'default'); $confirm = $this->confirm('Continue?', true);
More Examples
See USAGE.md for comprehensive examples including:
- Commands with arguments and options
- Interactive commands
- Table output
- Container integration
- Complete real-world examples
Requirements
- PHP 8.1+
- symfony/console ^6.0|^7.0
- psr/container ^2.0
License
MIT
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-17