Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit f361c65

Browse files
committed
some class modify
1 parent 13c4da7 commit f361c65

File tree

13 files changed

+369
-341
lines changed

13 files changed

+369
-341
lines changed
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace Inhere\Library\Helpers;
1010

1111
/**
12-
* Class CliHelper
12+
* Class Cli
1313
* @package Inhere\Library\Helpers
1414
*/
15-
class CliHelper
15+
class Cli
1616
{
1717
const NORMAL = 0;
1818

@@ -79,7 +79,7 @@ public static function color($text, $style = self::NORMAL)
7979
}
8080

8181
if (is_string($style)) {
82-
$out = isset(self::$styles[$style]) ? self::$styles[$style] : '0';
82+
$out = self::$styles[$style] ?? '0';
8383
} elseif (is_int($style)) {
8484
$out = $style;
8585

@@ -152,7 +152,7 @@ public static function isInteractive($fileDescriptor)
152152
* @param bool $mergeOpts
153153
* @return array
154154
*/
155-
public static function parseOptArgs($noValues = [], $mergeOpts = false)
155+
public static function parseOptArgs(array $noValues = [], $mergeOpts = false)
156156
{
157157
$params = $GLOBALS['argv'];
158158
reset($params);
@@ -186,7 +186,7 @@ public static function parseOptArgs($noValues = [], $mergeOpts = false)
186186
// check if next parameter is a descriptor or a value
187187
$nxp = current($params);
188188

189-
if (!in_array($opt, $noValues) && $value === true && $nxp !== false && $nxp{0} !== '-') {
189+
if ($value === true && $nxp !== false && $nxp{0} !== '-' && !in_array($opt, $noValues, true)) {
190190
list(, $value) = each($params);
191191

192192
// short-opt: bool opts. like -e -abc
@@ -257,6 +257,19 @@ public static function stderr($text, $nl = true, $quit = -200)
257257
}
258258
}
259259

260+
/**
261+
* run a command in background
262+
* @param string $cmd
263+
*/
264+
public static function execInBackground($cmd)
265+
{
266+
if (strpos(PHP_OS, 'Windows') === 0) {
267+
pclose(popen('start /B ' . $cmd, 'r'));
268+
} else {
269+
exec($cmd . ' > /dev/null &');
270+
}
271+
}
272+
260273
/**
261274
* Method to execute a command in the terminal
262275
* Uses :
@@ -267,7 +280,7 @@ public static function stderr($text, $nl = true, $quit = -200)
267280
* @param $command
268281
* @return array
269282
*/
270-
public static function terminal($command)
283+
public static function exec($command)
271284
{
272285
$return_var = 1;
273286

src/Helpers/EnvHelper.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function getVersion(): string
3333
*/
3434
public static function isUnix(): bool
3535
{
36-
$uNames = array('CYG', 'DAR', 'FRE', 'HP-', 'IRI', 'LIN', 'NET', 'OPE', 'SUN', 'UNI');
36+
$uNames = ['CYG', 'DAR', 'FRE', 'HP-', 'IRI', 'LIN', 'NET', 'OPE', 'SUN', 'UNI'];
3737

3838
return in_array(strtoupper(substr(PHP_OS, 0, 3)), $uNames, true);
3939
}
@@ -138,4 +138,30 @@ public static function isPHP(): bool
138138
}
139139

140140

141+
/**
142+
* setStrict
143+
* @return void
144+
*/
145+
public static function setStrict(): void
146+
{
147+
error_reporting(32767);
148+
}
149+
150+
/**
151+
* setMuted
152+
* @return void
153+
*/
154+
public static function setMuted(): void
155+
{
156+
error_reporting(0);
157+
}
158+
159+
/**
160+
* Returns true when the runtime used is PHP and Xdebug is loaded.
161+
* @return boolean
162+
*/
163+
public static function hasXDebug(): bool
164+
{
165+
return static::isPHP() && extension_loaded('xdebug');
166+
}
141167
}

src/Helpers/OpenSSL.php

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/Helpers/PhpException.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017/10/9
6+
* Time: 下午11:15
7+
*/
8+
9+
namespace Inhere\Library\Helpers;
10+
11+
/**
12+
* Class PhpException
13+
* @package Inhere\Library\Helpers
14+
*/
15+
class PhpException
16+
{
17+
/**
18+
* @see PhpException::toHtml()
19+
* {@inheritdoc}
20+
*/
21+
public static function toString($e, $getTrace = true, $catcher = null): string
22+
{
23+
return self::toHtml($e, $getTrace, $catcher, true);
24+
}
25+
26+
/**
27+
* Converts an exception into a simple string.
28+
* @param \Exception|\Throwable $e the exception being converted
29+
* @param bool $clearHtml
30+
* @param bool $getTrace
31+
* @param null|string $catcher
32+
* @return string the string representation of the exception.
33+
*/
34+
public static function toHtml($e, $getTrace = true, $catcher = null, $clearHtml = false): string
35+
{
36+
if (!$getTrace) {
37+
$message = "Error: {$e->getMessage()}";
38+
} else {
39+
$message = sprintf(
40+
"<h3>%s(%d): %s</h3>\n<pre><strong>File: %s(Line %d)</strong>%s \n\n%s</pre>",
41+
get_class($e),
42+
$e->getCode(),
43+
$e->getMessage(),
44+
$e->getFile(),
45+
$e->getLine(),
46+
$catcher ? "\nCatch By: $catcher" : '',
47+
$e->getTraceAsString()
48+
);
49+
}
50+
51+
if ($clearHtml) {
52+
$message = strip_tags($message);
53+
} else {
54+
$message = "<div class=\"exception-box\">{$message}</div>";
55+
}
56+
57+
return $message;
58+
}
59+
60+
/**
61+
* Converts an exception into a json string.
62+
* @param \Exception|\Throwable $e the exception being converted
63+
* @param bool $getTrace
64+
* @param null|string $catcher
65+
* @return string the string representation of the exception.
66+
*/
67+
public static function toJson($e, $getTrace = true, $catcher = null)
68+
{
69+
if (!$getTrace) {
70+
$message = "Error: {$e->getMessage()}";
71+
} else {
72+
$map = [
73+
'code' => $e->getCode() ?: 500,
74+
'msg' => sprintf(
75+
'%s(%d): %s, File: %s(Line %d)%s',
76+
get_class($e),
77+
$e->getCode(),
78+
$e->getMessage(),
79+
$e->getFile(),
80+
$e->getLine(),
81+
$catcher ? ", Catch By: $catcher" : ''
82+
),
83+
'data' => $e->getTrace()
84+
];
85+
$message = json_encode($map);
86+
}
87+
88+
return $message;
89+
}
90+
}

0 commit comments

Comments
 (0)