laswitchtech/php-cli

PHP应用程序的命令行界面

v2.0.9 2024-06-11 12:42 UTC

README

GitHub repo logo

phpCLI - [已弃用] - 请使用 coreCLI 代替

License GitHub repo size GitHub top language Version

特性

  • 命令行界面

为什么你可能需要它

如果你正在寻找一个易于开始的PHP CLI。那么这个PHP类就是为你准备的。

我能使用这个吗?

当然可以!

许可证

本软件根据GNU通用公共许可证v3.0许可发行。请阅读LICENSE了解软件可用性和分发信息。

要求

  • PHP >= 8.0
  • MySQL或MariaDB(可选)

安全

请负责任地披露发现的任何漏洞 - 私下向维护者报告安全问题。

安装

使用Composer

composer require laswitchtech/php-cli

如何使用它?

骨架

让我们从你的CLI项目目录的骨架开始。

├── cli
├── config
│   └── database.cfg
├── Command
│   └── HelloCommand.php
└── Model
    └── HelloModel.php
  • cli: The cli file is the entry-point of our application. It will initiate the Command being called in our application.
  • config/database.cfg: The config file holds the configuration information of our CLI. Mainly, it will hold the database credentials. But you could use it to store other configurations.
  • Command/: This directory will contain all of your Commands.
  • Command/HelloCommand.php: the Hello Command file which holds the necessary application code to entertain CLI calls. Mainly the methods that can be called.
  • Model/: This directory will contain all of your models.
  • Model/HelloModel.php: the Hello model file which implements the necessary methods to interact with a table in the MySQL database.

模型

Model files implements the necessary methods to interact with a table in the MySQL database. These model files needs to extend the Database class in order to access the database.

命名规范

The name of your model file should start with a capital character and be followed by Model.php. If not, the bootstrap will not load it. The class name in your Model files should match the name of the model file.

示例

//Import BaseModel class into the global namespace
use LaswitchTech\phpCLI\BaseModel;

class HelloModel extends BaseModel {
  public function getHellos($limit) {
    return $this->select("SELECT * FROM Hellos ORDER BY id ASC LIMIT ?", ["i", $limit]);
  }
}

命令

Command files holds the necessary application code to entertain CLI calls. Mainly the methods that can be called. These Command files needs to extend the BaseCommand class in order to access the basic methods.

命名规范

The name of your Command file should start with a capital character and be followed by Command.php. If not, the bootstrap will not load it. The class name in your Command files should match the name of the command file.

最后,可调用的方法需要以 Action 结尾。

示例

//Import BaseCommand class into the global namespace
use LaswitchTech\phpCLI\BaseCommand;

class HelloCommand extends BaseCommand {

  public function worldAction($argv) {
    if(count($argv) > 0){
      foreach($argv as $name){
        $this->sendOutput('Hello ' . $name . '!');
      }
    } else {
      $this->sendOutput('Hello World!');
    }
  }
}

方法

output()

This method is used to output content into the terminal.

$this->output('Hello Wolrd!');
set()

This method is used to color content for the terminal.

可用颜色

  • 默认
  • 黑色
  • 红色
  • 绿色
  • 黄色
  • 蓝色
  • 品红色
  • 青色
  • 浅灰色
  • 深灰色
  • 浅红色
  • 浅绿色
  • 浅黄色
  • 浅蓝色
  • 浅品红色
  • 浅青色
  • 白色
$this->set('Hello Wolrd!', 'magenta');
error()

This method is used to output content in red into the terminal.

$this->error('Hello Wolrd!');
success()

This method is used to output content in green into the terminal.

$this->success('Hello Wolrd!');
warning()

This method is used to output content in yellow into the terminal.

$this->warning('Hello Wolrd!');
info()

此方法用于在终端输出青色内容。

$this->info('Hello Wolrd!');
input($string, $options = null, $default = null)

此方法用于从终端请求输入。您可以选择三种类型的请求:字符串、选择和文本。

// String
$this->input('What is your name?');

// String with default value
$this->input('What is your name?', 'John Doe');
// Select
$this->input('Are you a?',['dog','cat','person']);

// Select with default value
$this->input('Are you a?',['dog','cat','person'],'person');
// Note that you can type (END/EXIT/QUIT/EOF/:Q/) to exit this request
// Text with a limit of lines
$this->input('What kind of person are you?',5);

// Text without a limit of lines
$this->input('What kind of person are you?',0);

// Text request and prompt exits
$this->input('What kind of person are you?',5,true);

命令行界面(CLI)

cli 文件是应用程序的入口点。它将启动应用程序中调用的命令。该文件可以命名为任何您想要的名称。

示例

session_start();

//Import phpCLI class into the global namespace
//These must be at the top of your script, not inside a function
use LaswitchTech\phpCLI\phpCLI;

//Load Composer's autoloader
require 'vendor/autoload.php';

// Interpret Standard Input
if(defined('STDIN') && !empty($argv[1])){

  // Start Command
  new phpCLI($argv);
}

调用 CLI

一旦您设置了第一个命令,您就可以开始调用您的命令行。

示例

./cli [COMMAND] [ACTION]