Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5746e19

Browse files
committed
format codes, update readme
1 parent 6809673 commit 5746e19

File tree

9 files changed

+116
-77
lines changed

9 files changed

+116
-77
lines changed

‎libs/php-utils/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# php utils
22

3+
Contains:
4+
5+
- simple autoloader
6+
- php env info
7+
- dot env load `.env`
8+
- error and exception info
9+
- common php helper
10+
- and more ...
11+
312
## install
413

514
```bash

‎libs/php-utils/src/AutoLoader.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,19 @@ class AutoLoader
6464
private $missingClasses = [];
6565

6666
/**
67+
* @param array $files
6768
* @return self
6869
*/
69-
public static function getLoader(): self
70+
public static function getLoader(array$files = []): self
7071
{
7172
if (null !== self::$loader) {
7273
return self::$loader;
7374
}
7475

76+
if ($files) {
77+
self::addFiles($files);
78+
}
79+
7580
self::$loader = $loader = new self();
7681

7782
$loader->register(true);
@@ -235,11 +240,10 @@ public function unRegister()
235240
* @param string $class The name of the class
236241
* @return bool|null True if loaded, null otherwise
237242
*/
238-
public function loadClass($class)
243+
public function loadClass(string$class)
239244
{
240245
if ($file = $this->findFile($class)) {
241246
_includeClassFile($file);
242-
243247
return true;
244248
}
245249

@@ -251,7 +255,7 @@ public function loadClass($class)
251255
* @param string $class The name of the class
252256
* @return string|false The path if found, false otherwise
253257
*/
254-
public function findFile($class)
258+
public function findFile(string$class)
255259
{
256260
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
257261
if ('\\' === $class[0]) {
@@ -281,25 +285,25 @@ public function findFile($class)
281285
private function findFileWithExtension(string $class, string $ext)
282286
{
283287
// PSR-4 lookup
284-
$logicalPathPsr4 = \str_replace('\\', DIRECTORY_SEPARATOR, $class) . $ext;
288+
$logicalPathPsr4 = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . $ext;
285289

286290
// PSR-4
287291
foreach ($this->psr4Map as $prefix => $dir) {
288292
if (0 === \strpos($class, $prefix)) {
289293
$length = \strlen($prefix);
290294

291-
if (\file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
295+
if (\file_exists($file = $dir . \DIRECTORY_SEPARATOR . \substr($logicalPathPsr4, $length))) {
292296
return $file;
293297
}
294298
}
295299
}
296300

297301
// PEAR-like class name
298-
$logicalPathPsr0 = \str_replace('_', DIRECTORY_SEPARATOR, $class) . $ext;
302+
$logicalPathPsr0 = \str_replace('_', \DIRECTORY_SEPARATOR, $class) . $ext;
299303

300304
foreach ($this->psr0Map as $prefix => $dir) {
301305
if (0 === \strpos($class, $prefix)) {
302-
$file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0;
306+
$file = $dir . \DIRECTORY_SEPARATOR . $logicalPathPsr0;
303307

304308
if (\file_exists($file)) {
305309
return $file;

‎libs/php-utils/src/PhpDoc.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,40 @@ class PhpDoc
2121
/**
2222
* Parses the comment block into tags.
2323
* @param string $comment The comment block text
24-
* @param array $ignored
24+
* @param array $options
25+
* - 'allow' // only allowed tags
26+
* - 'ignore' // ignored tags
27+
* - 'default' => 'description', // default tag name, first line text will attach to it.
2528
* @return array The parsed tags
2629
*/
27-
public static function getTags(string $comment, array $ignored = ['param', 'return']): array
30+
public static function getTags(string $comment, array $options = []): array
2831
{
29-
$comment = (string)\str_replace("\r\n", "\n", \trim($comment, "/ \n"));
30-
$comment = "@description \n" . \str_replace("\r", '',
32+
if (!$comment = \trim($comment, "/ \n")) {
33+
return [];
34+
}
35+
36+
$options = \array_merge([
37+
'allow' => [], // only allowed tags
38+
'ignore' => ['param', 'return'], // ignore tags
39+
'default' => 'description', // default tag name, first line text will attach to it.
40+
], $options);
41+
42+
$allow = (array)$options['allow'];
43+
$ignored = (array)$options['ignore'];
44+
45+
// always allow default tag
46+
if ($default = (string)$options['default']) {
47+
$allow[] = $default;
48+
}
49+
50+
$comment = \str_replace("\r\n", "\n", $comment);
51+
$comment = "@{$default}\n" .
52+
\str_replace("\r", '',
3153
\trim(\preg_replace('/^\s*\**( |\t)?/m', '', $comment))
3254
);
3355

34-
$tags = [];
35-
$parts = \preg_split('/^\s*@/m', $comment, -1, PREG_SPLIT_NO_EMPTY);
56+
$tags = [];
57+
$parts = \preg_split('/^\s*@/m', $comment, -1, \PREG_SPLIT_NO_EMPTY);
3658

3759
foreach ($parts as $part) {
3860
if (\preg_match('/^(\w+)(.*)/ms', \trim($part), $matches)) {
@@ -41,6 +63,10 @@ public static function getTags(string $comment, array $ignored = ['param', 'retu
4163
continue;
4264
}
4365

66+
if ($allow && !\in_array($name, $allow, true)) {
67+
continue;
68+
}
69+
4470
if (!isset($tags[$name])) {
4571
$tags[$name] = \trim($matches[2]);
4672
} elseif (\is_array($tags[$name])) {
@@ -56,7 +82,6 @@ public static function getTags(string $comment, array $ignored = ['param', 'retu
5682

5783
/**
5884
* Returns the first line of docBlock.
59-
*
6085
* @param string $comment
6186
* @return string
6287
*/
@@ -74,15 +99,14 @@ public static function firstLine(string $comment): string
7499
/**
75100
* Returns full description from the doc-block.
76101
* If have multi line text, will return multi line.
77-
*
78102
* @param string $comment
79103
* @return string
80104
*/
81105
public static function description(string $comment): string
82106
{
83-
$comment = (string)\str_replace("\r", '', \trim(\preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))));
107+
$comment = \str_replace("\r", '', \trim(\preg_replace('/^\s*\**( |\t)?/m', '', trim($comment, '/'))));
84108

85-
if (\preg_match('/^\s*@\w+/m', $comment, $matches, PREG_OFFSET_CAPTURE)) {
109+
if (\preg_match('/^\s*@\w+/m', $comment, $matches, \PREG_OFFSET_CAPTURE)) {
86110
$comment = \trim(\substr($comment, 0, $matches[0][1]));
87111
}
88112

‎libs/php-utils/src/PhpDotEnv.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @package Toolkit\PhpUtil
1414
*
1515
* in local config file `.env` (must is 'ini' format):
16+
*
1617
* ```ini
1718
* APP_ENV=dev
1819
* DEBUG=true
@@ -22,7 +23,7 @@
2223
* IN CODE:
2324
*
2425
* ```php
25-
* PhpDotEnv::load(__DIE__);
26+
* PhpDotEnv::load(__DIR__);
2627
* env('DEBUG', false);
2728
* env('APP_ENV', 'pdt');
2829
* ```

‎libs/php-utils/src/PhpEnv.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static function setStrict()
121121
*/
122122
public static function setMuted()
123123
{
124-
error_reporting(0);
124+
\error_reporting(0);
125125
}
126126

127127
/**
@@ -186,7 +186,6 @@ public static function checkExtList(array $extensions = [])
186186

187187
foreach ($extensions as $extension) {
188188
if (!\extension_loaded($extension)) {
189-
# 没有加载此扩展,记录
190189
$allTotal['no'][] = $extension;
191190
} else {
192191
$allTotal['yes'][] = $extension;

‎libs/php-utils/src/PhpError.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class PhpError
1616
{
1717
/** @var array */
18-
public static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
18+
public static $fatalErrors = [\E_ERROR, \E_PARSE, \E_CORE_ERROR, \E_COMPILE_ERROR, \E_USER_ERROR];
1919

2020
/**
2121
* $lastError = error_get_last();
@@ -48,35 +48,35 @@ public static function toArray(array $lastError, $catcher = null): array
4848
public static function codeToString(int $code): string
4949
{
5050
switch ($code) {
51-
case E_ERROR:
51+
case \E_ERROR:
5252
return 'E_ERROR';
53-
case E_WARNING:
53+
case \E_WARNING:
5454
return 'E_WARNING';
55-
case E_PARSE:
55+
case \E_PARSE:
5656
return 'E_PARSE';
57-
case E_NOTICE:
57+
case \E_NOTICE:
5858
return 'E_NOTICE';
59-
case E_CORE_ERROR:
59+
case \E_CORE_ERROR:
6060
return 'E_CORE_ERROR';
61-
case E_CORE_WARNING:
61+
case \E_CORE_WARNING:
6262
return 'E_CORE_WARNING';
63-
case E_COMPILE_ERROR:
63+
case \E_COMPILE_ERROR:
6464
return 'E_COMPILE_ERROR';
65-
case E_COMPILE_WARNING:
65+
case \E_COMPILE_WARNING:
6666
return 'E_COMPILE_WARNING';
67-
case E_USER_ERROR:
67+
case \E_USER_ERROR:
6868
return 'E_USER_ERROR';
69-
case E_USER_WARNING:
69+
case \E_USER_WARNING:
7070
return 'E_USER_WARNING';
71-
case E_USER_NOTICE:
71+
case \E_USER_NOTICE:
7272
return 'E_USER_NOTICE';
73-
case E_STRICT:
73+
case \E_STRICT:
7474
return 'E_STRICT';
75-
case E_RECOVERABLE_ERROR:
75+
case \E_RECOVERABLE_ERROR:
7676
return 'E_RECOVERABLE_ERROR';
77-
case E_DEPRECATED:
77+
case \E_DEPRECATED:
7878
return 'E_DEPRECATED';
79-
case E_USER_DEPRECATED:
79+
case \E_USER_DEPRECATED:
8080
return 'E_USER_DEPRECATED';
8181
}
8282

‎libs/php-utils/src/PhpException.php

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function toString($e, $getTrace = true, $catcher = null): string
3131
* @param null|string $catcher
3232
* @return string the string representation of the exception.
3333
*/
34-
public static function toHtml($e, $getTrace = true, $catcher = null, $clearHtml = false): string
34+
public static function toHtml($e, bool$getTrace = true, string$catcher = null,bool $clearHtml = false): string
3535
{
3636
if (!$getTrace) {
3737
$message = "Error: {$e->getMessage()}";
@@ -48,7 +48,7 @@ public static function toHtml($e, $getTrace = true, $catcher = null, $clearHtml
4848
);
4949
}
5050

51-
return $clearHtml ? strip_tags($message) : "<div class=\"exception-box\">{$message}</div>";
51+
return $clearHtml ? \strip_tags($message) : "<div class=\"exception-box\">{$message}</div>";
5252
}
5353

5454
/**
@@ -58,7 +58,7 @@ public static function toHtml($e, $getTrace = true, $catcher = null, $clearHtml
5858
* @param null|string $catcher
5959
* @return array
6060
*/
61-
public static function toArray($e, $getTrace = true, $catcher = null): array
61+
public static function toArray($e, bool$getTrace = true,string $catcher = null): array
6262
{
6363
$data = [
6464
'class' => \get_class($e),
@@ -85,35 +85,33 @@ public static function toArray($e, $getTrace = true, $catcher = null): array
8585
* @param null|string $catcher
8686
* @return string the string representation of the exception.
8787
*/
88-
public static function toJson($e, $getTrace = true, $catcher = null): string
88+
public static function toJson($e, bool$getTrace = true,string $catcher = null): string
8989
{
9090
if (!$getTrace) {
91-
$message = json_encode(['msg' => "Error: {$e->getMessage()}"]);
92-
} else {
93-
$map = [
94-
'code' => $e->getCode() ?: 500,
95-
'msg' => sprintf(
96-
'%s(%d): %s, File: %s(Line %d)',
97-
\get_class($e),
98-
$e->getCode(),
99-
$e->getMessage(),
100-
$e->getFile(),
101-
$e->getLine()
102-
),
103-
'data' => $e->getTrace()
104-
];
91+
return \json_encode(['msg' => "Error: {$e->getMessage()}"]);
92+
}
10593

106-
if ($catcher) {
107-
$map['catcher'] = $catcher;
108-
}
94+
$map = [
95+
'code' => $e->getCode() ?: 500,
96+
'msg' => sprintf(
97+
'%s(%d): %s, File: %s(Line %d)',
98+
\get_class($e),
99+
$e->getCode(),
100+
$e->getMessage(),
101+
$e->getFile(),
102+
$e->getLine()
103+
),
104+
'data' => $e->getTrace()
105+
];
109106

110-
if ($getTrace) {
111-
$map['trace'] = $e->getTrace();
112-
}
107+
if ($catcher) {
108+
$map['catcher'] = $catcher;
109+
}
113110

114-
$message = json_encode($map);
111+
if ($getTrace) {
112+
$map['trace'] = $e->getTrace();
115113
}
116114

117-
return $message;
115+
return \json_encode($map);
118116
}
119117
}

‎libs/php-utils/src/PhpHelper.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,22 @@ public static function initObject($object, array $options)
102102
public static function runtime($startTime, $startMem, array $info = [], $realUsage = false): array
103103
{
104104
$info['startTime'] = $startTime;
105-
$info['endTime'] = microtime(true);
106-
$info['endMemory'] = memory_get_usage($realUsage);
105+
$info['endTime'] = \microtime(true);
106+
$info['endMemory'] = \memory_get_usage($realUsage);
107107

108108
// 计算运行时间
109-
$info['runtime'] = number_format(($info['endTime'] - $startTime) * 1000, 3) . 'ms';
109+
$info['runtime'] = \number_format(($info['endTime'] - $startTime) * 1000, 3) . 'ms';
110110

111111
if ($startMem) {
112-
$startMem = array_sum(explode('', $startMem));
113-
$endMem = array_sum(explode('', $info['endMemory']));
112+
$startMem = \array_sum(\explode('', $startMem));
113+
$endMem = \array_sum(\explode('', $info['endMemory']));
114114

115-
$info['memory'] = number_format(($endMem - $startMem) / 1024, 3) . 'kb';
115+
$info['memory'] = \number_format(($endMem - $startMem) / 1024, 3) . 'kb';
116116
}
117117

118-
$peakMem = memory_get_peak_usage(true) / 1024 / 1024;
119-
$info['peakMemory'] = number_format($peakMem, 3) . 'Mb';
118+
$peakMem = \memory_get_peak_usage(true) / 1024 / 1024;
119+
// record
120+
$info['peakMemory'] = \number_format($peakMem, 3) . 'Mb';
120121

121122
return $info;
122123
}
@@ -162,12 +163,14 @@ public static function printVars(...$args): string
162163
}
163164

164165
/**
165-
* @param $var
166-
* @return mixed
166+
* @param mixed $var
167+
* @return string
167168
*/
168-
public static function exportVar($var)
169+
public static function exportVar($var): string
169170
{
170-
return \var_export($var, true);
171+
$string = \var_export($var, true);
172+
173+
return \preg_replace('/=>\s+\n\s+array \(/', '=> array (', $string);
171174
}
172175

173176
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /