讓⿊畫⾯再次偉⼤
范聖佑 (Shengyou Fan)
JetBrains Developer Advocate
COSCUP 2022
2022/07/31
Photo by Dan LeFebvre on Unsplash
⽤ 寫 CLI ⼯具
PHP 程式語⾔
—
https://www.php.net/
• 誕⽣於 1995 年 (27 歲)
• ⽬前最新為 PHP 8.1
• 預計 11 ⽉推出 8.2 版
• ⼤多⽤於 Web 後端開發
擴增 PHP 的使⽤範圍
—
Web Console
(Web Server + PHP Interpreter) (PHP Interpreter)
⽤ PHP 寫 CLI 的好處
—
• Scripting Language 隨寫即跑
• 超過 80% 的伺服器上有 PHP
• 對 PHP 開發者來說已是熟悉的語⾔
• PHP 的開發⽣態系成熟且完整
知名案例
—
• Composer
• Psalm
• PHPStan
• PHP CS Fixer
• Valet
• Rector
!
⽜⼑⼩試
三步驟⽤ PHP 寫⼀個 CLI 應⽤程式
建立執⾏環境
—
# Linux (Ubuntu)
$ sudo apt-get install -y software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt install php8.1
# macOS
$ brew install php
# Windows
$ scoop bucket add php
$ scoop install php/php8.1
三步驟寫 CLI
step.1
—
# 建立專案
$ mkdir make-cli-great-again
$ cd make-cli-great-again
$ composer init
# 建立程式進入點
$ vim {index}.php
建立專案及進入點
三步驟寫 CLI
step.2
—
!" 前置準備
!#/usr/bin/php
<?php
if (php_sapi_name() !!$ 'cli') {
exit;
}
!" 撰寫程式內容
echo "Hello CLI".PHP_EOL;
前置準備+程式內容
三步驟寫 CLI
step.3
—
# 指定 PHP Interpreter 執⾏
$ php {index}.php
# 讓 PHP Script 變成可執⾏檔
$ mv {index}.php app
$ chmod +x app
$ ./app
執⾏ PHP Script
開發 CLI App 的幾個要素
—
• 畫⾯輸出
• 介⾯互動
• 打包發佈
"
畫⾯輸出
美化 CLI 應⽤程式的輸出
安裝 CLImate 套件
—
# 使⽤ PHP League CLImate 來美化輸出
$ composer require league/climate
autoload 及初始化
—
require_once !%DIR!% . '/vendor/autoload.php';
use LeagueCLImateCLImate;
$climate = new CLImate();
基本輸出
—
$climate!&out(‘Make CLI Great Again');
$climate!&inline('Make');
$climate!&inline(' ');
$climate!&inline('CLI');
$climate!&inline(' ');
$climate!&inline('Great');
$climate!&inline(' ');
$climate!&inline('Again');
$climate!&out('');
樣式
—
$climate!&bold('Make CLI Great Again');
$climate!&underline('in COSCUP 2022');
$climate!&bold()
!&underline()
!&dim('Make CLI Great Again');
上⾊
—
$climate!&red('Make CLI Great Again');
$climate!&green('Make CLI Great Again');
$climate!&yellow('Make CLI Great Again');
$climate!&blue('Make CLI Great Again');
$climate!&magenta('Make CLI Great Again');
$climate!&cyan('Make CLI Great Again');
$climate!&white('Make CLI Great Again');
$climate!&backgroundRed()
!&green('Make CLI Great Again');
Tag
—
$climate!&out('Make <bold>' .
'<background_red>CLI!'background_red>' .
'!'bold> Great Again’);
預設樣式
—
$climate!&error('休⼠頓,這兒出了點問題!');
$climate!&comment('針對這事我的評論是…');
$climate!&whisper('噓~聖誕老⼈正在爬煙囪');
$climate!&shout('天啊啊啊啊啊啊~~~');
$climate!&info('==== 這是分隔線 ====');
表格
—
$data = [
[
'time' !( '10:00-10:30',
'title' !( '讓⿊畫⾯再次偉⼤',
'speaker' !( '范聖佑',
], [
'time' !( '10:40-11:20',
'title' !( '淺談 PHP 的 Coroutine 發展',
'speaker' !( 'Albert Chen',
], [
'time' !( '11:30-12:10',
'title' !( 'Laravel Bagisto 電商架站框架',
'speaker' !( 'John Liu',
],
];
$climate!&table($data);
#$
介⾯互動
讓 CLI 應⽤程式動起來!
鍵盤輸入
—
$input = $climate!&input('最喜歡的語⾔?');
match (strtoupper($input!&prompt())) {
"PHP" !( $climate!&green("真是好孩⼦!"),
default !( $climate!&red("喔!不~~~~~~"),
};
是否判斷
—
$input = $climate!&confirm('明年再來 COSCUP?');
if ($input!&confirmed()) {
$climate!&white()!&backgroundGreen('明年⾒!');
} else {
$climate!&white()!&backgroundBlue('T.T');
}
多重選項
—
$input = $climate!&checkboxes(
'午餐要些什麼?',
[
'排骨便當',
'雞腿便當',
'珍珠奶茶',
'布朗尼',
'味噌湯'
]
);
$response = $input!&prompt();
$climate!&out('您的餐點有:');
foreach ($response as $item) {
$climate!&out($item);
}
進度條
—
$steps = [
'量體溫',
'消毒雙⼿',
'到 409-2',
'聽演講',
'到 309 攤位',
'解題',
'拿獎品',
];
$progress = $climate!&progress()!&total(count($steps));
foreach ($steps as $key !( $step) {
$progress!&current($key + 1, $step);
usleep(80000);
}
更花俏的進度條
—
!" 安裝 php-cli-snake 套件
$ composer require alecrabbit/php-cli-snake
!" 使⽤ spinner
use AlecRabbitSnakeSpinner;
$s = new Spinner();
$s!&begin();
foreach (range(1, 10000) as $second) {
$s!&spin();
usleep(100);
}
$s!&end();
%
打包與發佈
讓使⽤者更⽅便取得
把 PHP Script 打包成 Phar
—
安裝打包⼯具
—
# 使⽤ Compose 安裝 Box
$ composer global require humbug/box
# 使⽤ Homebrew 安裝 Box
$ brew tap humbug/box
$ brew install box
# 使⽤ Phive 安裝 Box
$ phive install humbug/box
打包
—
# 打包應⽤程式
$ box compile
# 執⾏應⽤程式
$ php {app}.phar
box.json 設定檔
—
{
"main": "app.php",
"output": "bin/app.phar",
"directories": ["src"],
"finder": [
{
"name": "*.php",
"exclude": ["test", "tests"],
"in": "vendor"
}
]
}
發佈 Phar App
—
把 Runtime ⼀起
打包進去
—
# 以 phpmicro 打包
$ cat /path/to/micro.sfx app.phar > app
$ chmod 0755 ./app
$./app
&
⽤框架來降低開發難度
架在知名專案的肩膀上
Symfony Console
—
Laravel Zero
—
• PHP 在 Web 之外的應⽤⽅式
• ⽤ PHP 寫 CLI 的好處
• PHP CLI 的知名案例
• 開發 CLI App 的要素
• 可使⽤的 CLI 框架
回顧
—
關注道場粉絲⾴及社團們
—
社團
• Laravel 台灣
• PHP 也有 Day
• PHP 台灣
週⼀⾄五每天⼀篇新聞分享!
—
Coding 職⼈塾
Kraftsman
范聖佑 (Shengyou Fan)
shengyou.fan@jetbrains.com
Q&A
—
讓⿊畫⾯再次偉⼤
⽤ PHP 寫 CLI ⼯具

[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具