Skip to content

Commit eefc29c

Browse files
committed
add some base helper
1 parent 4cbfa4a commit eefc29c

File tree

9 files changed

+488
-6
lines changed

9 files changed

+488
-6
lines changed

src/JsonHelper.php renamed to src/Helper/JsonHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @license MIT
88
*/
99

10-
namespace Toolkit\Stdlib;
10+
namespace Toolkit\Stdlib\Helper;
1111

1212
use InvalidArgumentException;
1313
use RuntimeException;

src/Helper/PhpHelper.php

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/phpkit.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/phpkit
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Stdlib\Helper;
11+
12+
use Toolkit\Stdlib\Obj\ObjectHelper;
13+
use function array_sum;
14+
use function explode;
15+
use function get_defined_constants;
16+
use function is_array;
17+
use function is_object;
18+
use function is_string;
19+
use function memory_get_peak_usage;
20+
use function memory_get_usage;
21+
use function method_exists;
22+
use function microtime;
23+
use function number_format;
24+
use function ob_get_clean;
25+
use function ob_start;
26+
use function preg_replace;
27+
use function strpos;
28+
use function strtoupper;
29+
use function var_dump;
30+
use function var_export;
31+
32+
/**
33+
* Class PhpHelper
34+
*
35+
* @package Toolkit\PhpKit
36+
*/
37+
class PhpHelper
38+
{
39+
/**
40+
* get $_SERVER value
41+
*
42+
* @param string $name
43+
* @param string $default
44+
*
45+
* @return mixed
46+
*/
47+
public static function serverParam(string $name, $default = '')
48+
{
49+
$name = strtoupper($name);
50+
51+
return $_SERVER[$name] ?? $default;
52+
}
53+
54+
/**
55+
* @param callable|mixed $cb
56+
* @param array ...$args
57+
*
58+
* @return mixed
59+
*/
60+
public static function call($cb, ...$args)
61+
{
62+
if (is_string($cb)) {
63+
// function
64+
if (strpos($cb, '::') === false) {
65+
return $cb(...$args);
66+
}
67+
68+
// ClassName::method
69+
$cb = explode('::', $cb, 2);
70+
} elseif (is_object($cb) && method_exists($cb, '__invoke')) {
71+
return $cb(...$args);
72+
}
73+
74+
if (is_array($cb)) {
75+
[$obj, $mhd] = $cb;
76+
77+
return is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
78+
}
79+
80+
return $cb(...$args);
81+
}
82+
83+
/**
84+
* @param callable $cb
85+
* @param array $args
86+
*
87+
* @return mixed
88+
*/
89+
public static function callByArray(callable $cb, array $args)
90+
{
91+
return self::call($cb, ...$args);
92+
}
93+
94+
/**
95+
* Set property values ​​for object
96+
* - 会先尝试用 setter 方法设置属性
97+
* - 再尝试直接设置属性
98+
*
99+
* @param mixed $object An object instance
100+
* @param array $options
101+
*
102+
* @return mixed
103+
*/
104+
public static function initObject($object, array $options)
105+
{
106+
return ObjectHelper::init($object, $options);
107+
}
108+
109+
/**
110+
* 获取资源消耗
111+
*
112+
* @param int $startTime
113+
* @param int|float $startMem
114+
* @param array $info
115+
* @param bool $realUsage
116+
*
117+
* @return array
118+
*/
119+
public static function runtime($startTime, $startMem, array $info = [], $realUsage = false): array
120+
{
121+
$info['startTime'] = $startTime;
122+
$info['endTime'] = microtime(true);
123+
$info['endMemory'] = memory_get_usage($realUsage);
124+
125+
// 计算运行时间
126+
$info['runtime'] = number_format(($info['endTime'] - $startTime) * 1000, 3) . 'ms';
127+
128+
if ($startMem) {
129+
$startMem = array_sum(explode(' ', $startMem));
130+
$endMem = array_sum(explode(' ', $info['endMemory']));
131+
132+
$info['memory'] = number_format(($endMem - $startMem) / 1024, 3) . 'kb';
133+
}
134+
135+
$peakMem = memory_get_peak_usage(true) / 1024 / 1024;
136+
// record
137+
$info['peakMemory'] = number_format($peakMem, 3) . 'Mb';
138+
139+
return $info;
140+
}
141+
142+
/**
143+
* dump vars
144+
*
145+
* @param array ...$args
146+
*
147+
* @return string
148+
*/
149+
public static function dumpVars(...$args): string
150+
{
151+
ob_start();
152+
var_dump(...$args);
153+
$string = ob_get_clean();
154+
155+
return preg_replace("/=>\n\s+/", '=> ', $string);
156+
}
157+
158+
/**
159+
* print vars
160+
*
161+
* @param array ...$args
162+
*
163+
* @return string
164+
*/
165+
public static function printVars(...$args): string
166+
{
167+
$string = '';
168+
169+
foreach ($args as $arg) {
170+
$string .= print_r($arg, 1) . PHP_EOL;
171+
}
172+
173+
return preg_replace("/Array\n\s+\(/", 'Array (', $string);
174+
}
175+
176+
/**
177+
* @param mixed $var
178+
*
179+
* @return string
180+
*/
181+
public static function exportVar($var): string
182+
{
183+
$string = var_export($var, true);
184+
185+
return preg_replace('/=>\s+\n\s+array \(/', '=> array (', $string);
186+
}
187+
}

src/Json.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
namespace Toolkit\Stdlib;
1111

12+
use Toolkit\Stdlib\Helper\JsonHelper;
13+
1214
/**
1315
* Class Json
16+
*
1417
* @package Toolkit\Stdlib
1518
*/
1619
final class Json extends JsonHelper

src/OS.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/phpkit.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/phpkit
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Stdlib;
11+
12+
use function defined;
13+
use function function_exists;
14+
use function getmyuid;
15+
use function in_array;
16+
use function php_uname;
17+
use function posix_getuid;
18+
use function stripos;
19+
use const PHP_OS;
20+
use const PHP_OS_FAMILY;
21+
22+
/**
23+
* Class OsEnv
24+
*
25+
* @package Toolkit\PhpKit\OS
26+
*/
27+
class OS
28+
{
29+
/**
30+
* @return string
31+
*/
32+
public static function name(): string
33+
{
34+
if (defined('PHP_OS_FAMILY')) {
35+
return PHP_OS_FAMILY;
36+
}
37+
38+
return PHP_OS;
39+
}
40+
41+
/**************************************************************************
42+
* system env
43+
*************************************************************************/
44+
45+
/**
46+
* @return bool
47+
*/
48+
public static function isUnix(): bool
49+
{
50+
$uNames = ['CYG', 'DAR', 'FRE', 'HP-', 'IRI', 'LIN', 'NET', 'OPE', 'SUN', 'UNI'];
51+
52+
return in_array(strtoupper(substr(PHP_OS, 0, 3)), $uNames, true);
53+
}
54+
55+
/**
56+
* @return bool
57+
*/
58+
public static function isLinux(): bool
59+
{
60+
return stripos(self::name(), 'LIN') !== false;
61+
}
62+
63+
/**
64+
* @return bool
65+
*/
66+
public static function isWin(): bool
67+
{
68+
return self::isWindows();
69+
}
70+
71+
/**
72+
* @return bool
73+
*/
74+
public static function isWindows(): bool
75+
{
76+
return stripos(self::name(), 'WIN') !== false;
77+
}
78+
79+
/**
80+
* @return bool
81+
*/
82+
public static function isMac(): bool
83+
{
84+
return self::isMacOS();
85+
}
86+
87+
/**
88+
* @return bool
89+
*/
90+
public static function isMacOS(): bool
91+
{
92+
return stripos(self::name(), 'Darwin') !== false;
93+
}
94+
95+
/**
96+
* @return bool
97+
*/
98+
public static function isRoot(): bool
99+
{
100+
return self::isRootUser();
101+
}
102+
103+
/**
104+
* @return bool
105+
*/
106+
public static function isRootUser(): bool
107+
{
108+
if (function_exists('posix_getuid')) {
109+
return posix_getuid() === 0;
110+
}
111+
112+
return getmyuid() === 0;
113+
}
114+
115+
/**
116+
* Get unix user of current process.
117+
*
118+
* @return array
119+
*/
120+
public static function getCurrentUser(): array
121+
{
122+
return posix_getpwuid(posix_getuid());
123+
}
124+
125+
/**
126+
* @return string
127+
*/
128+
public static function tempDir(): string
129+
{
130+
return self::getTempDir();
131+
}
132+
133+
/**
134+
* @return string
135+
*/
136+
public static function getTempDir(): string
137+
{
138+
// @codeCoverageIgnoreStart
139+
if (function_exists('sys_get_temp_dir')) {
140+
$tmp = sys_get_temp_dir();
141+
} elseif (!empty($_SERVER['TMP'])) {
142+
$tmp = $_SERVER['TMP'];
143+
} elseif (!empty($_SERVER['TEMP'])) {
144+
$tmp = $_SERVER['TEMP'];
145+
} elseif (!empty($_SERVER['TMPDIR'])) {
146+
$tmp = $_SERVER['TMPDIR'];
147+
} else {
148+
$tmp = getcwd();
149+
}
150+
// @codeCoverageIgnoreEnd
151+
152+
return $tmp;
153+
}
154+
155+
/**
156+
* @return string
157+
*/
158+
public static function getHostname(): string
159+
{
160+
return php_uname('n');
161+
}
162+
163+
/**
164+
* @return string
165+
*/
166+
public static function getNullDevice(): string
167+
{
168+
if (self::isUnix()) {
169+
return '/dev/null';
170+
}
171+
172+
return 'NUL';
173+
}
174+
175+
/**
176+
* Returns if the file descriptor is an interactive terminal or not.
177+
*
178+
* @param int|resource $fileDescriptor
179+
*
180+
* @return boolean
181+
*/
182+
public static function isInteractive($fileDescriptor): bool
183+
{
184+
return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
185+
}
186+
}

0 commit comments

Comments
 (0)