nickbeen/php-cli-progress-bar
Composer 安装命令:
composer require nickbeen/php-cli-progress-bar
包简介
For creating minimal progress bars in PHP CLI scripts
关键字:
README 文档
README
For creating minimal progress bars in PHP CLI scripts. It has no dependencies, but requires PHP 8.0 or higher. This library is mainly built for iterating to countable variables, but also works easily with ticking through less structured step-by-step scripts.
Many PHP CLI progress bars have been written in the past, but most haven't been updated in years. This library uses the latest PHP features such as return types, named arguments and constructor property promotions. For creating richer, more customizable progress bars, check alternatives such as the progress bar helper included in the symfony/console package.
Requirements
- PHP 8.0 or higher
Installation
Install the library into your project with Composer.
composer require nickbeen/php-cli-progress-bar
Usage
With this library you can display a progress bar in a PHP CLI script to indicate the script is doing its work and how far it has progressed. All you need to do is start displaying the progress bar, tick through the steps the script goes through and finish the display of the progress bar.
1/100 [#...........................] 1% (00:00:16)
64/100 [##################..........] 64% (00:00:07)
100/100 [############################] 100%
Manually progressing
It is possible to tick through the steps of your scripts manually when the steps in your script cannot be looped.
Each tick adds one progression, but you can override the progression made by including an integer in tick().
You do need to set maxProgress for the progress bar to display the correct numbers by including it in the constructor.
If you don't know the maxProgress during initialization, you can set it later with the setMaxProgress() method.
$progressBar = new \NickBeen\ProgressBar\ProgressBar(maxProgress: 62); $progressBar->start(); doSomething(); $progressBar->tick(); doSomethingElse(); $progressBar->tick(); doSixtyTasks(); $progressBar->tick(60); $progressBar->finish();
If you have a little more structure in your step-by-step code, you can easily place tick() in a for loop.
There is however a more convenient method when dealing with e.g. arrays.
Iterating through arrays or traversable instances
This class method works with anything of the pseudo-type iterable which includes any array or any instance of Traversable.
The iterate() method automatically handles starting the progress bar, managing ticking through the iteration and finally finish displaying the progress bar.
$array = [ 1 => 'A', 2 => 'B', 3 => 'C', ]; $progressBar = new \NickBeen\ProgressBar\ProgressBar(); foreach ($progressBar->iterate($array) as $key => $value) { echo "$key: $value" . PHP_EOL; }
Interact with the progress bar
It is possible to interact with the progress bar during its run. You can retrieve the estimated time to finish, the progress it has made, the maximum progress that has been set and the amount of completion in percentage. You can use this information e.g. for notifications or other tasks in the background.
foreach ($progressBar->iterate($array);) { // Some custom notification sendToDiscord($progressBar->getEstimatedTime()); // Some custom task application syncWithCloud($progressBar->getPercentage()) // Some other custom application sendToRaspberryPiDisplay($progressBar->getProgress(), $progressBar->getMaxProgress()) }
License
This library is licensed under the MIT License (MIT). See the LICENSE for more details.
nickbeen/php-cli-progress-bar 适用场景与选型建议
nickbeen/php-cli-progress-bar 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48.07k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 06 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「cli」 「progress bar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nickbeen/php-cli-progress-bar 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nickbeen/php-cli-progress-bar 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nickbeen/php-cli-progress-bar 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PACE (Progress Automatically Certain to Entertain) - Automatic page load progress bar for Yii PHP Framework
Reusable progress bar components for Filament 5 tables and infolists.
Slim starter / Slim skeleton package to boost your development with Slim framework
Generates a trait to help ease the access of custom repo methods
Alfabank REST API integration
Eco CLI for Laravel .env syncing
统计信息
- 总下载量: 48.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-18