定制 baethon/symfony-console-input 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

baethon/symfony-console-input

最新稳定版本:v1.0.0

Composer 安装命令:

composer require baethon/symfony-console-input

包简介

Generates command signature based on an input DTO

README 文档

README

This project provides a streamlined way to define Symfony Console commands using PHP attributes and Data Transfer Objects (DTOs). It simplifies the configuration of arguments and options by leveraging metadata annotations, making it easier to maintain and extend console commands.

Features

  • Automatically generates command arguments and options from DTO properties.
  • Supports metadata annotations (#[Argument], #[Option], etc.) to define command inputs.
  • Easily integrates with Symfony's Console component.
  • Provides a UsesInputData trait to configure and initialize input data automatically.

Requirements

  • PHP >= 8.4

Installation

To install the plugin, use Composer:

composer require baethon/symfony-console-input

Getting Started

1. Define Your Input DTO

Create a class with properties annotated using provided attributes like #[Argument] or #[Option].

use Baethon\Symfony\Console\Input\Attributes\Argument;
use Baethon\Symfony\Console\Input\Attributes\Option;

readonly class MyCommandInput
{
    public function __construct(
        #[Argument]
        public string $name,   // A required argument

        #[Option]
        public int $age = 18,  // An optional option with a default value
    ) {}
}

Important

The input class must define all arguments and options in the constructor.

2. Create a Command

Use the UsesInputData trait in your Symfony Console command class to leverage the input configuration:

use Baethon\Symfony\Console\Input\Attributes\InputData;
use Baethon\Symfony\Console\Input\UsesInputData;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'my-command')]
class MyCommand extends Command
{
    use UsesInputData;

    #[InputData]
    private MyCommandInput $input;

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $output->writeln("Name: {$this->input->name}");
        $output->writeln("Age: {$this->input->age}");

        return Command::SUCCESS;
    }
}

Attributes

#[Argument]

Marks a property as a required/optional argument.

#[Option]

Marks a property as an option.

#[Shortcut]

Defines a shortcut for an option.

#[Description]

Adds a description for the argument or option.

#[Name]

Sets a different name for the argument or option.

Testing

Run tests using Pest:

./vendor/bin/pest

License

This project is licensed under the MIT License. See the LICENSE file for details.

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-23