reedsys/leap

PHP 命令行应用程序框架

v1.1 2021-10-15 09:35 UTC

This package is auto-updated.

Last update: 2024-09-15 16:04:21 UTC


README

简介

Leap 是一个用于 Unix/Linux 的 PHP 命令行应用程序框架。我们知道市面上有很多 CLI 框架。我们创建这个框架是为了满足以下需求:

  1. 简化访问应用程序参数
  2. 提供简单且可扩展的输入/输出处理器附加方法
  3. 将应用程序转换为 Unix/Linux 环境中的守护进程
  4. 使 "帮助" 输出易于阅读
  5. 面向对象化您的代码

需求

此库使用 PHP 7.4 或更高版本。

安装

此包可以通过 composer 安装

composer install leap

用法

cli 应用程序的简单示例 (helloworld.php)

<?php

// path to where Leap.php is located
require_once("Leap.php");

class HelloWorld
    implements \leap\Leapable
{
    public function options() : array {
        // follow getopt() naming convention
        return [
            'h'   => [ $this, "print_hello" ],  // option that do not accept value
            'w::' => [ $this, "print_world" ],  // optional value
            'm:'  => [ $this, "print_my" ]      // requires value
        ];
    }

    public function main(\leap\Argument $arg = null) : int {
        $arg->invokeFunction( $this->options() );
        return 0;
    }
    
    public function print_hello($v) {
        echo "\nHello ";
    }
    
    public function print_world($v) {
        echo "World\n";
    }
    
    public function print_my($v) {
        echo $v . "\n";
    }
}

\leap\Leap::run(
    \leap\Leap::wrap(
        new \leap\cli\Wrapper(),
        new HelloWorld()
    )
);

运行应用程序

> php helloworld.php -h

Hello >
> php helloworld.php -h -w

Hello World
>
> php helloworld.php -h -w -m '!!'

Hello World
!!
>

更多示例,您可以在测试文件夹中运行脚本文件 runtest.sh