Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time

鍛戒护琛

浣跨敤PHP寮鍙戦潪http鏈嶅姟鏃讹紝濡傚畾鏃朵换鍔$瓑锛屽父闇瑕侀氳繃鍛戒护琛屾ā寮忓惎鍔╬hp鑴氭湰锛孭hpBoot鐨凜LI鏀寔鍙互璁╀綘蹇熷畬鎴愯繖鏂归潰宸ヤ綔銆

1. 瀹炵幇鍛戒护琛

/**
 * @command test    //鍙 @command鎸囧畾鍛戒护鐨勫墠缂
 */
namespace App\Commands

class TestCommand
{
    /**
     * run test
     *
     * the run test
     * @command run    // 鍛戒护鍞竴鏍囪瘑
     *
     * @param int $arg0 arg 0
     * @param string $arg1 arg 1
     * @param string[] $arg2 arg 2
     */
    public function runTest($arg0, $arg1, $arg2){
        print_r([$arg0, $arg1, $arg2]);
        return 0; // 杩斿洖杩涚▼鐨別xit code
    }
}

2. 缂栧啓鍏ュ彛鏂囦欢 cli.php

use \PhpBoot\Application;
use \PhpBoot\Console;

ini_set('date.timezone','Asia/Shanghai');
require __DIR__.'/../vendor/autoload.php';

// 鍔犺浇閰嶇疆
$app = Application::createByDefault(__DIR__ . '/../config/config.php');
// 鍔犺浇鍛戒护琛
$console = Console::create($app);
$console->loadCommandsFromPath(__DIR__.'/../App/Commands', 'App\\Commands');
// 鎵ц鍛戒护琛
$console->run();

3. 鎵ц鍛戒护

鎵ц php ./cli.php

$ php ./cli.php 
Console Tool

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:
  help     Displays help for a command
  list     Lists commands
  my.test   : run test

鎵ц php ./cli.php my.test 1 2 a b

$ php ./cli.php my.test 1 2 a b 
array(3) {
  [0]=>
  int(1)
  [1]=>
  string(1) "2"
  [2]=>
  array(2) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
  }
}