Files
michaelschiemer/src/Framework/Console/Examples/CreateUserCommand.php
Michael Schiemer 5050c7d73a docs: consolidate documentation into organized structure
- Move 12 markdown files from root to docs/ subdirectories
- Organize documentation by category:
  • docs/troubleshooting/ (1 file)  - Technical troubleshooting guides
  • docs/deployment/      (4 files) - Deployment and security documentation
  • docs/guides/          (3 files) - Feature-specific guides
  • docs/planning/        (4 files) - Planning and improvement proposals

Root directory cleanup:
- Reduced from 16 to 4 markdown files in root
- Only essential project files remain:
  • CLAUDE.md (AI instructions)
  • README.md (Main project readme)
  • CLEANUP_PLAN.md (Current cleanup plan)
  • SRC_STRUCTURE_IMPROVEMENTS.md (Structure improvements)

This improves:
 Documentation discoverability
 Logical organization by purpose
 Clean root directory
 Better maintainability
2025-10-05 11:05:04 +02:00

127 lines
4.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Framework\Console\Examples;
use App\Framework\Console\ArgumentParser;
use App\Framework\Console\ConsoleCommand;
use App\Framework\Console\ConsoleInput;
use App\Framework\Console\ConsoleOutputInterface;
use App\Framework\Console\ExitCode;
use App\Framework\Console\HelpGenerator;
use App\Framework\Core\ValueObjects\Email;
/**
* Example command demonstrating the enhanced parameter parsing system
*/
final readonly class CreateUserCommand
{
private ArgumentParser $parser;
public function __construct()
{
// Define command arguments using the fluent builder
$this->parser = ArgumentParser::create()
->requiredString('email', 'User email address')
->requiredString('name', 'Full name of the user')
->choice('role', ['user', 'admin', 'moderator'], false, 'user', 'User role')
->flag('force', 'f', 'Skip confirmation prompts')
->flag('notify', null, 'Send welcome email to user')
->integer('quota', false, 100, 'Storage quota in MB')
->build();
}
#[ConsoleCommand('user:create', 'Create a new user account')]
public function execute(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
{
try {
// Set the enhanced parser for this command
$input->setArgumentParser($this->parser);
// Validate all required arguments
$input->validate();
// Get typed arguments with automatic validation
$email = new Email($input->getString('email'));
$name = $input->getString('name');
$role = $input->getString('role');
$force = $input->getBool('force');
$notify = $input->getBool('notify');
$quota = $input->getInt('quota');
// Display what we're about to do
$output->writeLine("Creating user with the following details:");
$output->writeLine(" Email: {$email->toString()}");
$output->writeLine(" Name: {$name}");
$output->writeLine(" Role: {$role}");
$output->writeLine(" Quota: {$quota} MB");
$output->writeLine(" Notify: " . ($notify ? 'Yes' : 'No'));
$output->newLine();
// Confirm unless forced
if (! $force && ! $input->confirm('Create this user?', false)) {
$output->writeInfo('User creation cancelled.');
return ExitCode::SUCCESS;
}
// Simulate user creation
$output->writeSuccess("User '{$name}' created successfully!");
if ($notify) {
$output->writeInfo("Welcome email sent to {$email->toString()}");
}
return ExitCode::SUCCESS;
} catch (\InvalidArgumentException $e) {
$output->writeError($e->getMessage());
$this->showHelp($output);
return ExitCode::INVALID_INPUT;
} catch (\Exception $e) {
$output->writeError("Failed to create user: " . $e->getMessage());
return ExitCode::GENERAL_ERROR;
}
}
#[ConsoleCommand('user:create:help', 'Show help for user:create command')]
public function showHelp(ConsoleInput $input, ConsoleOutputInterface $output): ExitCode
{
$helpGenerator = new HelpGenerator($output);
$helpGenerator->display(
'user:create',
$this->parser->getDefinitions(),
'Create a new user account with email, name, and optional settings.'
);
return ExitCode::SUCCESS;
}
/**
* Example usage methods to demonstrate all features
*/
public function getExampleUsages(): array
{
return [
// Basic usage
'php console user:create john@example.com "John Doe"',
// With role
'php console user:create john@example.com "John Doe" --role=admin',
// With short flags
'php console user:create john@example.com "John Doe" -f --notify',
// With quota
'php console user:create john@example.com "John Doe" --quota=500 --role=moderator',
// All options
'php console user:create john@example.com "John Doe" --role=admin --force --notify --quota=1000',
];
}
}