turbolabit/php-symfony-basecommand

该软件包最新版本(dev-main)没有可用的许可证信息。

一个最小的Symfony扩展包,用于更快地构建自己的CLI命令

安装: 240

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 1

开放问题: 0

类型:symfony-bundle

dev-main 2024-09-07 12:43 UTC

This package is auto-updated.

Last update: 2024-09-07 12:43:30 UTC


README

扩展Symfony控制台命令,以更好地、更快地构建自己的CLI命令。

🚀 开始您的项目(无需Symfony)

如果您正在构建一个简单的命令,并且不想使用整个Symfony框架

composer init

安装此包(请参阅: 使用composer安装它

使用此模板生成 MyApp.php 启动文件

<?php
use MyVendorName\MyApp\MyAppNameCommand;
use TurboLabIt\BaseCommand\Command\AbstractBaseCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;

require __DIR__ . '/vendor/autoload.php';

$arrCmdArguments = [
    MyAppNameCommand::CLI_ARG_MY_ARG => $argv[1],
    // 💡 https://github.com/TurboLabIt/php-symfony-basecommand/blob/main/src/Traits/CliOptionsTrait.php
    "--" . \TurboLabIt\BaseCommand\Service\Options::CLI_OPT_DRY_RUN         => true,
    //"--" . \TurboLabIt\BaseCommand\Service\Options::CLI_OPT_BLOCK_MESSAGES  => true,
];

( new MyAppNameCommand() )
    ->setName('MyAppName')
    ->run(new ArrayInput($arrCmdArguments), new ConsoleOutput());

添加一个 run.sh 以便于执行

#!/usr/bin/env bash

## https://github.com/TurboLabIt/webstackup/blob/master/script/base.sh
source "/usr/local/turbolab.it/webstackup/script/base.sh"
fxHeader "🚀 My App"
EXPECTED_USER=$(logname)

cd $PROJECT_DIR

wsuComposer install

php MyApp.php MyArg1

fxEndFooter

📦 使用composer安装

symfony composer config repositories.turbolabit/php-symfony-basecommand git https://github.com/TurboLabIt/php-symfony-basecommand.git
symfony composer require turbolabit/php-symfony-basecommand:dev-main

🚀 您自己的Command模板

您现在可以使用此模板来构建自己的CLI应用程序。

<?php declare(strict_types=1);
namespace App\Command;

use TurboLabIt\BaseCommand\Command\AbstractBaseCommand;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


#[AsCommand(name: 'MyCommand')]
class MyCommand extends AbstractBaseCommand
{
    // 💡 define your own specific --option(s)
    const CLI_OPT_MY_OPT = "my-opt";

    // 💡 set your `$allow` options: https://github.com/TurboLabIt/php-symfony-basecommand/blob/main/src/Traits/CliOptionsTrait.php
    protected bool $allowParallelExec     = ????;
    protected bool $allowDryRunOpt        = ????;
    protected bool $allowBlockMessagesOpt = ????;
    protected bool $allowIdOpt            = ????;
    protected bool $allowNoDownloadOpt    = ????;
    protected bool $allowLangOpt          = ????;
    protected bool $langOptIsMandatory    = ????;
    
    
    public function __construct(array $arrConfig = [])
    {
        parent::__construct($arrConfig);
        // 💡 inject your own dependencies
    }


    protected function configure() : void
    {
        parent::configure();
        // 💡 add your own specific option(s)
        $this->addOption(static::CLI_OPT_MY_OPT, null, InputOption::VALUE_NONE, 'Text description');
    }


    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        parent::execute($input, $output);
        // 💡 see https://github.com/TurboLabIt/php-symfony-basecommand/blob/main/src/Command/AbstractBaseCommand.php
        
        
        $this->fxTitle("Doing things...");
        // ...
        $this->fxOK();
        // 💡 see other output functions here: https://github.com/TurboLabIt/php-symfony-basecommand/blob/main/src/Traits/BashFxDirectTrait.php
        
        
        // 💡 the **smallest** section of data-changing ops must be wrapped like this
        $this->fxTitle("Changing some data...");
        if( $this->isNotDryRun() ) {
          
          // ...
          $this->fxInfo("Some minor detail you should know");
          $this->fxOK();
        }


        // 💡 the **smallest** section of email/message-sending ops must be wrapped like this
        $this->fxTitle("Sending the report to the manager...");
        if( $this->isSendingMessageAllowed() ) {
          
          // ...
          $this->fxInfo("Some minor detail you should know");
          $this->fxOK();
        }
        
        
        $this->fxTitle("Processing data...");
        foreach($this->arrData as $item) {
          
          $id = $item->getId();
          
          // 💡 use a guard clause to exclude IDs
          if ( !$this->isIdFilterMatch() ){
            continue;
          }
          
          // ...
        }


        // 💡 you can fail-exit the application like this
        if('something\'s wrong') {
          return $this->endWithError();
        }


        // 💡 you can access your own option(s) like this
        $myCustomAdditionalOpt = $this->getCliOptionstatic::CLI_OPT_....);
        if($myCustomAdditionalOpt) {
          // ...
        }


        // 💡 the last op must be this
        return $this->endWithSuccess();
    }
}

🧪 测试它

git clone git@github.com:TurboLabIt/php-symfony-basecommand.git
cd php-symfony-basecommand
clear && bash scripts/test-runner.sh