othyn / php-time-remaining
一个小巧的库,帮助进行简单的进度输出,专注于剩余时间。
Requires
- php: ^8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.61.1
- phpunit/phpunit: ^9.6.20
README
这是一个用于 Composer 的 PHP 包,它添加了一个非常简单的进度跟踪器,侧重于估计完成时间。
<?php use Othyn\TimeRemaining\TimeRemaining; $timeRemaining = new TimeRemaining(100); sleep(30); // Simulate some work being done. $formattedProgress = $timeRemaining->getFormattedProgress(50); echo $formattedProgress; // Output: [50% - 50 / 100][~ 0h 0m 30s remaining]
该包在 Packagist 上作为 othyn/php-time-remaining 提供。
安装
进入你希望在其中安装项目的项目,并运行以下 Composer 命令以获取最新版本
composer require othyn/php-time-remaining
用法
有关更全面的用法示例,您可以查看测试套件。但以下我将展示一些基本用法示例。
初始化
要开始使用 TimeRemaining 包,请用所需项目的总数进行初始化
<?php require 'vendor/autoload.php'; use Othyn\TimeRemaining\TimeRemaining; $totalItems = 100; $timeRemaining = new TimeRemaining($totalItems);
您也可以稍后更新项目总数,如果您无法在初始化点定义它
<?php require 'vendor/autoload.php'; use Othyn\TimeRemaining\TimeRemaining; $timeRemaining = new TimeRemaining(); // Code that fetches total items as $totalItems $timeRemaining->setTotalItems($totalItems);
格式化输出
获取显示进度和剩余时间的格式化字符串
<?php $formattedProgress = $timeRemaining->getFormattedProgress($currentItem); echo $formattedProgress; // Output: [50% - 50 / 100][~ 0h 0m 30s remaining]
您还可以自定义格式
<?php $customFormat = '[%d%% - %d / %d items][~ %dh %dm %ds left]'; $formattedProgressCustom = $timeRemaining->getFormattedProgress($currentItem, $customFormat); echo $formattedProgressCustom; // Output: [50% - 50 / 100 items][~ 0h 0m 30s left]
获取已过时间
获取自初始化以来已过的时间
<?php $elapsedTime = $timeRemaining->getElapsedTime(); echo "Elapsed time: {$elapsedTime} seconds\n";
获取进度
根据当前项目获取进度
<?php $currentItem = 50; $progress = $timeRemaining->getPercentageProgress($currentItem); echo "Progress: {$progress}%\n";
获取估计的总时间
获取进程的估计总时间
<?php $estimatedTotalTime = $timeRemaining->getEstimatedTotalTime($currentItem); echo "Estimated total time: {$estimatedTotalTime} seconds\n";
获取剩余时间
获取进程的剩余时间
<?php $remainingTime = $timeRemaining->getRemainingTime($currentItem); echo "Remaining time: {$remainingTime} seconds\n";
开发
大多数开发过程都封装在一个易于使用的 Docker 容器中。
强制风格
项目的 .php-cs-fixer.dist.php 配置包含该存储库符合的规则,并将运行在 ./src 和 ./tests 目录。
对于远程风格强制,已配置 GitHub Action 以自动运行 phpcsfixer。
对于本地风格强制,已配置 composer 脚本 composer style 以运行 phpcsfixer。
测试
对于远程测试,已设置 GitHub Action 以自动在 main 分支或 PR 分支上运行测试套件。
对于本地测试,有一个预先构建的 Docker 容器,其中包含 Alpine CLI 版本的 PHP + PHPUnit + xdebug。这是用于测试项目的设置,可以通过以下方式设置
composer docker-build
这将触发 Docker Compose 构建镜像。然后您可以通过以下方式启动容器
composer docker-up
对于所有编写的代码,都有测试,可以通过以下方式运行
# PHPUnit with code coverage report composer test # PHPUnit with code coverage report, using local phpunit and xdebug composer test-local
在这些测试中,有针对包生产就绪实现的特性测试。目前没有单元测试。
您也可以通过使用以下命令轻松在测试容器中打开 shell
composer docker-shell