3

我通过symfony new my_project_name.

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

控制台输出

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

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

4

1 回答 1

1

You are creating a complete Symfony application, so all the commands provided by the included packages are available.

Instead of starting from a framework and trying to trim down the parts you do not want, you need to start from further down to have a really barebones project.

Bootstrapping the project:

First, do not use symfony command, no need. Plain old composer will do the trick.

On an empty directory, execute:

composer require symfony/console

This will import the only dependency needed for a console project, and do the basic bootstrapping for your autoloader.

In composer.json, add the following:

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

Command class

You'll need one or more commands to actually add to your application. Let's start with a fully fledged greeting application. Create the file src/Greet.php within your project:

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>");

    }
}

This is a basic command that will print "Hello World!" if executed without any option or argument, will accept one argument use instead of "World", and one option to set the greeting instead of "Hello".

Console Application

On the project's root, create a file 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();

This is be a very short script, so let's go line by line

  1. Require the autoloader. This script is the entry point for the application, so we need to execute the autoloader.
  2. Instantiate the application. This creates a new instance of the Symfony Console application, sets a name and version for the app. This will be used in the "help" printouts for the app.
  3. Add the command. By default, the application has no commands at all. We'll need to add the command we have just created. add() expects a Command instance. We instantiate the command we just created, and we set it to be called by the name "greet".
  4. Run the application

Generate autoloader.

Execute composer dump-autoload so the autoloader for your application is generated.

Execute the script.

If you now execute php app.php you'll get:

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

Note that the only available command is greet. Which you can use like this:

# 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]
于 2019-10-29T09:10:31.993 回答