如果你是一名 web 开发工程师,那么你一定使用 PHP 开发过很多 Web 应用程序。但是你知道如何使用 PHP 快速构建一个命令行应用程序 (工具) 吗? 下面我将向您展示如何使用 PHP 和一个著名的的 Composer 扩展包 -- Symphony/Console 构建一个命令行应用。
Symphony/Console 是一个使用 Composer 管理的 PHP 扩展包,它简化了创建一个漂亮的、可测试的 PHP 命令行应用的过程,它提供了开箱即用的诸如 (可选 / 必选的) 参数规范和选项规范 (使用 - 符号) 等功能。那么,我们来一起开始构建我们的应用。
按照惯例,我们将构建一个 "Hello World" 的控制台应用程序,但是要稍微修改一下它,让它支持自定义问候语(代替 Hello), 并且可以随意的去问候一个人(代替 world)。
- mkdir hello - world - app && cd hello - world - app
- composer require symfony / console
- touch HelloWorld
- chmod +X HelloWorld
- #!/usr/bin/env php
- <?php
- require __DIR__.'/vendor/autoload.php';
- use Symfony\Component\Console\Application;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
- (new Application('Hello World', '1.0.0'))
- ->register('greet')
- ->addArgument('name', InputArgument::OPTIONAL, 'Name of the person')
- ->addOption('say', null, InputOption::VALUE_REQUIRED, 'Custom greeting')
- ->setCode(function (InputInterface $input, OutputInterface $output) {
- $name = $input->getArgument('name');
- $greeting = $input->getOption('say');
- if (!empty($name) && !empty($greeting)) {
- return $output->writeln("<info>$greeting $name!</info>");
- } else if (!empty($name)) {
- return $output->writeln("<info>Hello $name!</info>");
- } else if (!empty($greeting)) {
- return $output->writeln("<info>$greeting World!</info>");
- } else {
- return $output->writeln("<info>Hello World!</info>");
- }
- })
- ->getApplication()
- ->run();
看,就这样,你拥有了自己的 HelloWorld 控制台程序
当没有指定命令时,HelloWorld 默认输出一屏信息提示
Symfony Console 组件给我们提供的应用程序有几个开箱可用的选项的和命令,比如 help,list 和 --version
OK,让我们来看看我们的 HelloWorld 文件中的代码。
InputInterface 和 OutputInterface 将使应用程序的输入和输出功能变得简单,InputArgument 和 InputOption 将帮助我们处理传递给我们 HelloWorld 应用程序的选项和参数。
- require __DIR__.'/vendor/autoload.php';
- use Symfony\Component\Console\Application;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Input\InputOption;
- use Symfony\Component\Console\Output\OutputInterface;
, 并注册我们的 greet 命令。
- HelloWorld (v1.0.0)
- (new Application('Hello World', '1.0.0'))
- ->register('greet')
- ->addArgument('name', InputArgument::OPTIONAL, 'Name of the person')
- ->addOption('say', null, InputOption::VALUE_REQUIRED, 'Custom greeting')
- - >setCode(function(InputInterface $input, OutputInterface $output) {
- $name = $input - >getArgument('name');
- $greeting = $input - >getOption('say');
- if (!empty($name) && !empty($greeting)) {
- return $output - >writeln("<info>$greeting $name!</info>");
- } else if (!empty($name)) {
- return $output - >writeln("<info>Hello $name!</info>");
- } else if (!empty($greeting)) {
- return $output - >writeln("<info>$greeting World!</info>");
- } else {
- return $output - >writeln("<info>Hello World!</info>");
- }
- })
- ->getApplication()
- ->run();
来源: https://juejin.im/entry/5a3795a051882572ed55af00