Skip to content

Commit 8520aeb

Browse files
committed
repleace some util methods
1 parent 3924621 commit 8520aeb

File tree

6 files changed

+21
-74
lines changed

6 files changed

+21
-74
lines changed

examples/alone

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use Toolkit\Cli\Color;
1111

1212
define('BASE_PATH', dirname(__DIR__));
1313

14-
require dirname(__DIR__) . '/test/boot.php';
14+
require dirname(__DIR__) . '/test/bootstrap.php';
1515

1616
$input = new Input();
1717
$ctrl = new HomeController($input, new Output());

examples/alone-app

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use Toolkit\Cli\Color;
1111

1212
define('BASE_PATH', dirname(__DIR__));
1313

14-
require dirname(__DIR__) . '/test/boot.php';
14+
require dirname(__DIR__) . '/test/bootstrap.php';
1515

1616
try {
1717
$input = new Input();

examples/app

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use Inhere\Console\Application;
1010

1111
define('BASE_PATH', dirname(__DIR__));
1212

13-
require dirname(__DIR__) . '/test/boot.php';
13+
require dirname(__DIR__) . '/test/bootstrap.php';
1414

1515
// create app instance
1616
$app = new Application([

src/Concern/ApplicationHelpTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use function str_replace;
3636
use function strpos;
3737
use function strtr;
38+
use function vdump;
3839
use const PHP_EOL;
3940
use const PHP_OS;
4041
use const PHP_VERSION;

src/Util/FormatUtil.php

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,20 @@
99
namespace Inhere\Console\Util;
1010

1111
use Toolkit\Cli\ColorTag;
12+
use Toolkit\Stdlib\Helper\Format;
1213
use Toolkit\Stdlib\Helper\JsonHelper;
1314
use Toolkit\Stdlib\Str;
1415
use Toolkit\Sys\Sys;
1516
use function array_keys;
1617
use function array_merge;
17-
use function count;
18-
use function date;
1918
use function explode;
20-
use function floor;
2119
use function implode;
2220
use function is_array;
2321
use function is_bool;
2422
use function is_int;
2523
use function is_numeric;
2624
use function is_scalar;
2725
use function rtrim;
28-
use function sprintf;
2926
use function str_repeat;
3027
use function str_replace;
3128
use function strpos;
@@ -169,28 +166,18 @@ public static function alignOptions(array $options): array
169166
}
170167

171168
/**
172-
* @param float $memory
173-
*
174-
* @return string
175169
* ```
176170
* FormatUtil::memoryUsage(memory_get_usage(true));
177171
* ```
172+
*
173+
* @param float|int $memory
174+
*
175+
* @return string
176+
* @deprecated use Format::memory($secs);
178177
*/
179178
public static function memoryUsage($memory): string
180179
{
181-
if ($memory >= 1024 * 1024 * 1024) {
182-
return sprintf('%.2f Gb', $memory / 1024 / 1024 / 1024);
183-
}
184-
185-
if ($memory >= 1024 * 1024) {
186-
return sprintf('%.2f Mb', $memory / 1024 / 1024);
187-
}
188-
189-
if ($memory >= 1024) {
190-
return sprintf('%.2f Kb', $memory / 1024);
191-
}
192-
193-
return sprintf('%d B', $memory);
180+
return Format::memory($memory);
194181
}
195182

196183
/**
@@ -199,36 +186,11 @@ public static function memoryUsage($memory): string
199186
* @param int $secs
200187
*
201188
* @return string
189+
* @deprecated use Format::howLongAgo($secs);
202190
*/
203191
public static function howLongAgo(int $secs): string
204192
{
205-
static $timeFormats = [
206-
[0, '< 1 sec'],
207-
[1, '1 sec'],
208-
[2, 'secs', 1],
209-
[60, '1 min'],
210-
[120, 'mins', 60],
211-
[3600, '1 hr'],
212-
[7200, 'hrs', 3600],
213-
[86400, '1 day'],
214-
[172800, 'days', 86400],
215-
];
216-
217-
foreach ($timeFormats as $index => $format) {
218-
if ($secs >= $format[0]) {
219-
$next = $timeFormats[$index + 1] ?? false;
220-
221-
if (($next && $secs < $next[0]) || $index === count($timeFormats) - 1) {
222-
if (2 === count($format)) {
223-
return $format[1];
224-
}
225-
226-
return floor($secs / $format[2]) . ' ' . $format[1];
227-
}
228-
}
229-
}
230-
231-
return date('Y-m-d H:i:s', $secs);
193+
return Format::howLongAgo($secs);
232194
}
233195

234196
/**

src/Util/Helper.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use RecursiveIteratorIterator;
2020
use RuntimeException;
2121
use Swoole\Coroutine;
22+
use Toolkit\Stdlib\Arr\ArrayHelper;
2223
use function class_exists;
2324
use function file_exists;
2425
use function is_dir;
@@ -173,29 +174,19 @@ public static function findSimilar(string $need, $iterator, int $similarPercent
173174
/**
174175
* get key Max Width
175176
*
176-
* @param array $data
177-
* [
177+
* [
178178
* 'key1' => 'value1',
179179
* 'key2-test' => 'value2',
180-
* ]
181-
* @param bool $expectInt
180+
* ]
181+
*
182+
* @param array $data
183+
* @param bool $excludeInt
182184
*
183185
* @return int
184186
*/
185-
public static function getKeyMaxWidth(array $data, bool $expectInt = false): int
187+
public static function getKeyMaxWidth(array $data, bool $excludeInt = true): int
186188
{
187-
$keyMaxWidth = 0;
188-
189-
foreach ($data as $key => $value) {
190-
// key is not a integer
191-
if (!$expectInt || !is_numeric($key)) {
192-
$width = mb_strlen((string)$key, 'UTF-8');
193-
194-
$keyMaxWidth = $width > $keyMaxWidth ? $width : $keyMaxWidth;
195-
}
196-
}
197-
198-
return $keyMaxWidth;
189+
return ArrayHelper::getKeyMaxWidth($data, $excludeInt);
199190
}
200191

201192
/**
@@ -206,11 +197,4 @@ public static function throwInvalidArgument(string $format, ...$args): void
206197
{
207198
throw new InvalidArgumentException(sprintf($format, ...$args));
208199
}
209-
210-
/**
211-
* @param string $optsStr
212-
*/
213-
public static function formatOptions(string $optsStr): void
214-
{
215-
}
216200
}

0 commit comments

Comments
 (0)