【问题标题】:How to hide or delete the defaults available console commands?如何隐藏或删除默认可用的控制台命令?
【发布时间】:2019-10-28 23:57:51
【问题描述】:

我通过symfony new my_project_name 创建了一个新的 Symfony 4 项目。

目前当我执行./bin/console时,输出显示

我将创建一些自定义控制台命令,并且我只想在执行./bin/console 时显示我的自定义命令

也许我应该从头开始创建一个自定义的可执行“控制台”,但我不知道该怎么做。

【问题讨论】:

    标签: php symfony symfony4 symfony-console


    【解决方案1】:

    您正在创建一个完整的 Symfony 应用程序,因此包含的软件包提供的所有命令都可用。

    与其从一个框架开始并试图削减你不想要的部分,你需要从更远的地方开始,才能拥有一个真正的准系统项目。

    引导项目:

    首先,不要使用symfony 命令,没必要。普通的老作曲家就可以了。

    目录上,执行:

    composer require symfony/console
    

    这将导入控制台项目所需的唯一依赖项,并为您的自动加载器执行基本引导。

    composer.json 中,添加以下内容:

    "autoload": {
        "psr-4": {
          "App\\": "src/"
        }
      }
    

    命令类

    您需要一个或多个命令才能实际添加到您的应用程序中。让我们从一个成熟的问候应用程序开始。在您的项目中创建文件src/Greet.php

    declare(strict_types=1);
    
    namespace App;
    
    use Symfony\Component\Console\Input\InputArgument;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Output\OutputInterface;
    
    class Greet extends Symfony\Component\Console\Command\Command
    {
        protected function configure()
        {
            $this->addArgument('person', InputArgument::OPTIONAL, 'Name of the Entity being greeted', 'World');
            $this->addOption('greeting', 'g', InputOption::VALUE_OPTIONAL, 'How to greet the entity', 'Hello');
        }
    
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $greeting = $input->getOption('greeting');
            $entity   = $input->getArgument('person');
    
            $output->writeln("<info>$greeting $entity!</info>");
    
        }
    }
    

    这是一个打印“Hello World!”的基本命令。如果在没有任何选项或参数的情况下执行,将接受一个参数使用而不是“World”,以及一个设置问候语而不是“Hello”的选项。

    控制台应用程序

    在项目的根目录上,创建一个文件app.php

    require __DIR__ . '/vendor/autoload.php';
    $app = new Symfony\Component\Console\Application('Hello World App', '1.0.0');
    $app->add((new App\Greet('greet')));
    $app->run();
    

    这是一个很短的脚本,让我们逐行进行

    1. 需要自动加载器。该脚本是应用程序的入口点,因此我们需要执行自动加载器。
    2. 实例化应用程序。 这将创建 Symfony 控制台应用程序的新实例,设置应用程序的名称和版本。这将用于应用程序的“帮助”打印输出。
    3. 添加命令。默认情况下,应用程序根本没有命令。我们需要添加刚刚创建的命令。 add() 需要一个 Command 实例。我们实例化刚刚创建的命令,并将其设置为名称为“greet”。
    4. 运行应用程序

    生成自动加载器。

    执行composer dump-autoload,以便为您的应用程序生成自动加载器。

    执行脚本。

    如果你现在执行php app.php,你会得到:

    Hello World App 1.0.0
    
    Usage:
      command [options] [arguments]
    
    Options:
      -h, --help            Display this help message
      -q, --quiet           Do not output any message
      -V, --version         Display this application version
          --ansi            Force ANSI output
          --no-ansi         Disable ANSI output
      -n, --no-interaction  Do not ask any interactive question
      -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    Available commands:
      greet
      help   Displays help for a command
      list   Lists commands
    

    请注意,唯一可用的命令是greet。你可以这样使用:

    # php app.php greet
    Hello World!
    # php app.php greet Alice
    Hello Alice!
    # php app.php greet Bob -g "Good morning"
    Good morning Bob!
    # php app.php help greet
    Usage:
      greet [options] [--] [<person>]
    
    Arguments:
      person                     Name of the Entity being greeted [default: "World"]
    
    Options:
      -g, --greeting[=GREETING]  How to greet the entity [default: "Hello"]
    [... default generic options omitted]
    

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 2020-12-12
      • 2017-08-24
      • 2011-05-25
      • 2014-02-04
      相关资源
      最近更新 更多