platformsh/console-form

一个轻量级的 Symfony Console 表单系统。

v0.0.37 2023-05-05 09:24 UTC

README

为 Symfony Console 命令提供轻量级表单系统。

命令可以定义可以用于命令行选项和交互式输入的表单。

Build Status

示例

<?php
namespace MyApplication;

use Platformsh\ConsoleForm\Field\EmailAddressField;
use Platformsh\ConsoleForm\Field\Field;
use Platformsh\ConsoleForm\Form;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MyCommand extends Command
{
    protected function configure()
    {
        $this->setName('my:command')
             ->setDescription('An example command');
        $this->form = Form::fromArray([
            'name' => new Field('Name', ['description' => 'Your full name']),
            'mail' => new EmailAddressField('Email', ['description' => 'Your email address']),
        ]);
        $this->form->configureInputDefinition($this->getDefinition());
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $questionHelper = $this->getHelper('question');
        $result = $this->form->resolveOptions($input, $output, $questionHelper);

        $output->writeln("Your name: " . $result['name']);
        $output->writeln("Your email address: " . $result['mail']);
    }
}

替代方案