Skip to content
This repository was archived by the owner on Apr 24, 2021. It is now read-only.

Commit 7742775

Browse files
committed
update some class. move some class to mylib/errors
1 parent e45340e commit 7742775

File tree

7 files changed

+215
-313
lines changed

7 files changed

+215
-313
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
}
1414
],
1515
"require": {
16-
"php": ">=7.0.0"
16+
"php": ">7.0.0"
1717
},
1818
"autoload": {
1919
"psr-4": {
2020
"MyLib\\PhpUtil\\" : "src/"
2121
}
2222
},
2323
"suggest": {
24-
"inhere/simple-print-tool": "Very lightweight data printing tools",
2524
"inhere/php-validate": "Very lightweight data validate tool",
2625
"inhere/console": "a lightweight php console application library."
2726
}

src/AutoLoader.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static function setFiles(array $files)
109109
public static function addFiles(array $files)
110110
{
111111
if (self::$files) {
112-
self::$files = array_merge(self::$files, $files);
112+
self::$files = \array_merge(self::$files, $files);
113113
} else {
114114
self::$files = $files;
115115
}
@@ -134,7 +134,7 @@ public function addPsr0($prefix, $path)
134134
public function addPsr0Map(array $psr0Map)
135135
{
136136
if ($this->psr0Map) {
137-
$this->psr0Map = array_merge($this->psr0Map, $psr0Map);
137+
$this->psr0Map = \array_merge($this->psr0Map, $psr0Map);
138138
} else {
139139
$this->psr0Map = $psr0Map;
140140
}
@@ -163,7 +163,7 @@ public function addPsr4($prefix, $path)
163163
public function addPsr4Map(array $psr4Map)
164164
{
165165
if ($this->psr4Map) {
166-
$this->psr4Map = array_merge($this->psr4Map, $psr4Map);
166+
$this->psr4Map = \array_merge($this->psr4Map, $psr4Map);
167167
} else {
168168
$this->psr4Map = $psr4Map;
169169
}
@@ -207,7 +207,7 @@ public function setClassMap(array $classMap)
207207
public function addClassMap(array $classMap)
208208
{
209209
if ($this->classMap) {
210-
$this->classMap = array_merge($this->classMap, $classMap);
210+
$this->classMap = \array_merge($this->classMap, $classMap);
211211
} else {
212212
$this->classMap = $classMap;
213213
}
@@ -219,15 +219,15 @@ public function addClassMap(array $classMap)
219219
*/
220220
public function register($prepend = false)
221221
{
222-
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
222+
\spl_autoload_register(array($this, 'loadClass'), true, $prepend);
223223
}
224224

225225
/**
226226
* Un-registers this instance as an autoloader.
227227
*/
228228
public function unRegister()
229229
{
230-
spl_autoload_unregister(array($this, 'loadClass'));
230+
\spl_autoload_unregister(array($this, 'loadClass'));
231231
}
232232

233233
/**
@@ -255,7 +255,7 @@ public function findFile($class)
255255
{
256256
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
257257
if ('\\' === $class[0]) {
258-
$class = (string)substr($class, 1);
258+
$class = (string)\substr($class, 1);
259259
}
260260

261261
// class map lookup
@@ -276,25 +276,25 @@ public function findFile($class)
276276
private function findFileWithExtension($class, $ext)
277277
{
278278
// PSR-4 lookup
279-
$logicalPathPsr4 = str_replace('\\', DIRECTORY_SEPARATOR, $class) . $ext;
279+
$logicalPathPsr4 = \str_replace('\\', DIRECTORY_SEPARATOR, $class) . $ext;
280280

281281
// PSR-4
282282
foreach ($this->psr4Map as $prefix => $dir) {
283-
if (0 === strpos($class, $prefix)) {
283+
if (0 === \strpos($class, $prefix)) {
284284
$length = \strlen($prefix);
285285

286-
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
286+
if (\file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
287287
return $file;
288288
}
289289
}
290290
}
291291

292292
// PEAR-like class name
293-
$logicalPathPsr0 = str_replace('_', DIRECTORY_SEPARATOR, $class) . $ext;
293+
$logicalPathPsr0 = \str_replace('_', DIRECTORY_SEPARATOR, $class) . $ext;
294294

295295
foreach ($this->psr0Map as $prefix => $dir) {
296-
if (0 === strpos($class, $prefix)) {
297-
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
296+
if (0 === \strpos($class, $prefix)) {
297+
if (\file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
298298
return $file;
299299
}
300300
}

src/PhpEnv.php

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2018/2/8 0008
6+
* Time: 19:20
7+
*/
8+
9+
namespace MyLib\PhpUtil;
10+
11+
/**
12+
* Class PhpEnv
13+
* @package MyLib\PhpUtil
14+
*/
15+
final class PhpEnv
16+
{
17+
/**************************************************************************
18+
* php env
19+
*************************************************************************/
20+
21+
/**
22+
* Get PHP version
23+
* @return string
24+
*/
25+
public static function getVersion(): string
26+
{
27+
return \defined('HHVM_VERSION') ? HHVM_VERSION : PHP_VERSION;
28+
}
29+
30+
/**
31+
* isEmbed
32+
* @return boolean
33+
*/
34+
public static function isEmbed(): bool
35+
{
36+
return 'embed' === PHP_SAPI;
37+
}
38+
39+
/**
40+
* @return bool
41+
*/
42+
public static function isCgi(): bool
43+
{
44+
return stripos(PHP_SAPI, 'cgi') !== false; # cgi环境
45+
}
46+
47+
/**
48+
* is Cli
49+
* @return boolean
50+
*/
51+
public static function isCli(): bool
52+
{
53+
return PHP_SAPI === 'cli';
54+
}
55+
56+
/**
57+
* is Build In Server
58+
* run server use like: `php -S 127.0.0.1:8085`
59+
* @return boolean
60+
*/
61+
public static function isBuiltInServer(): bool
62+
{
63+
return PHP_SAPI === 'cli-server';
64+
}
65+
66+
/**
67+
* @return bool
68+
*/
69+
public static function isDevServer(): bool
70+
{
71+
return PHP_SAPI === 'cli-server';
72+
}
73+
74+
/**
75+
* isWeb
76+
* @return boolean
77+
*/
78+
public static function isWeb(): bool
79+
{
80+
return \in_array(PHP_SAPI, [
81+
'apache',
82+
'cgi',
83+
'fast-cgi',
84+
'cgi-fcgi',
85+
'fpm-fcgi',
86+
'srv',
87+
'cli-server'
88+
], true);
89+
}
90+
91+
/**
92+
* isHHVM
93+
* @return boolean
94+
*/
95+
public static function isHHVM(): bool
96+
{
97+
return \defined('HHVM_VERSION');
98+
}
99+
100+
/**
101+
* isPHP
102+
* @return boolean
103+
*/
104+
public static function isPHP(): bool
105+
{
106+
return !static::isHHVM();
107+
}
108+
109+
/**
110+
* setStrict
111+
* @return void
112+
*/
113+
public static function setStrict(): void
114+
{
115+
error_reporting(32767);
116+
}
117+
118+
/**
119+
* setMuted
120+
* @return void
121+
*/
122+
public static function setMuted(): void
123+
{
124+
error_reporting(0);
125+
}
126+
127+
/**
128+
* @param string $name
129+
* @return bool
130+
*/
131+
public static function hasExtension(string $name): bool
132+
{
133+
return \extension_loaded($name);
134+
}
135+
136+
/**
137+
* Returns true when the runtime used is PHP and Xdebug is loaded.
138+
* @return boolean
139+
*/
140+
public static function hasXDebug(): bool
141+
{
142+
return static::isPHP() && \extension_loaded('xdebug');
143+
}
144+
145+
/**
146+
* @param $name
147+
* @param bool|false $throwException
148+
* @return bool
149+
* @throws \RuntimeException
150+
*/
151+
public static function extIsLoaded(string $name, $throwException = false): bool
152+
{
153+
$result = \extension_loaded($name);
154+
155+
if (!$result && $throwException) {
156+
throw new \RuntimeException("Extension [$name] is not loaded.");
157+
}
158+
159+
return $result;
160+
}
161+
162+
/**
163+
* 检查多个扩展加载情况
164+
* @param array $extensions
165+
* @return array|bool
166+
*/
167+
public static function checkExtList(array $extensions = array())
168+
{
169+
$allTotal = [];
170+
171+
foreach ($extensions as $extension) {
172+
if (!\extension_loaded($extension)) {
173+
# 没有加载此扩展,记录
174+
$allTotal['no'][] = $extension;
175+
} else {
176+
$allTotal['yes'][] = $extension;
177+
}
178+
}
179+
180+
return $allTotal;
181+
}
182+
183+
/**
184+
* 返回加载的扩展
185+
* @param bool $zend_extensions
186+
* @return array
187+
*/
188+
public static function getLoadedExtension($zend_extensions = false): array
189+
{
190+
return get_loaded_extensions($zend_extensions);
191+
}
192+
}

src/PhpError.php

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

0 commit comments

Comments
 (0)