-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystem.php
324 lines (291 loc) · 7.36 KB
/
System.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* Arikaim
*
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <[email protected]>
* @license http://www.arikaim.com/license
*
*/
namespace Arikaim\Core\System;
use Arikaim\Core\Utils\Utils;
use Arikaim\Core\System\NodeJs;
use Arikaim\Core\System\Process;
use Arikaim\Core\Packages\Composer;
/**
* Core system helper class
*/
class System
{
const UNKNOWN = 1;
const WINDOWS = 2;
const LINUX = 3;
const OSX = 4;
/**
* Call static methods from instance
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
return Utils::callStatic(System::class,$name,$arguments);
}
/**
* Get apache current user
* @return string
*/
public static function getApacheUser(): string
{
return exec('whoami');
}
/**
* Get nodejs version
*
* @return string:null
*/
public static function getNodeJsVersion(): ?string
{
return NodeJs::getVersion();
}
/**
* Get registered socket transports
*
* @return array
*/
public static function getStreamTransports()
{
return \stream_get_transports();
}
/**
* Get php console ver
*
* @return mixed
*/
public static function getPhpConsoleVersion()
{
$php = (Process::findPhp() === false) ? 'php' : Process::findPhp();
$command = $php . ' --version';
return Process::run($command);
}
/**
* Get system info
*
* @return array
*/
public function getInfo(): array
{
return Self::getSystemInfo();
}
/**
* Get system info
*
* @return array
*/
public static function getSystemInfo(): array
{
$os = \posix_uname();
return [
'core' => Composer::getInstalledPackageVersion('arikaim/core'),
'php_version' => Self::getPhpVersion(),
'os_name' => \explode(' ',$os['sysname'])[0],
'os_version' => $os['release'],
'os_machine' => $os['machine'],
'apache_version' => Self::getApacheVersion(),
'apache_modules' => Self::getApacheModules(),
'current_user' => \get_current_user(),
'user_id' => \getmyuid(),
'os_node' => $os['nodename']
];
}
/**
* Get apache version
*
* @return string|null
*/
public static function getApacheVersion(): ?string
{
return (\function_exists('apache_get_version') == true) ? \apache_get_version() : null;
}
/**
* Get apache modules
*
* @return array
*/
public static function getApacheModules(): array
{
return (\function_exists('apache_get_modules') == true) ? \apache_get_modules() : [];
}
/**
* Set script execution tile limit (0 - unlimited)
*
* @param integer $time
* @return void
*/
public static function setTimeLimit($time): void
{
\set_time_limit($time);
}
/**
* Return php version
*
* @return string
*/
public static function getPhpVersion(): string
{
return \substr(\phpversion(),0,6);
}
/**
* Return php extensions list
*
* @return array
*/
public function getPhpExtensions(): array
{
$data = [];
$items = \get_loaded_extensions(false);
foreach ($items as $item) {
$version = Utils::formatVersion(Self::getPhpExtensionVersion($item));
$data[] = [
'name' => $item,
'version' => $version
];
}
return $data;
}
/**
* Return php extension version
*
* @param string $phpExtensionName
* @return string|null
*/
public static function getPhpExtensionVersion(string $phpExtensionName): ?string
{
$ext = new \ReflectionExtension($phpExtensionName);
return \substr($ext->getVersion(),0,6);
}
/**
* Return true if php extension is instaed
*
* @param string $phpExtensionName
* @return boolean
*/
public static function hasPhpExtension(string $phpExtensionName)
{
return \extension_loaded($phpExtensionName);
}
/**
* Return true if PDO driver is installed
*
* @param string $driverName
* @return boolean
*/
public static function hasPdoDriver(string $driverName): bool
{
$drivers = Self::getPdoDrivers();
return (\is_array($drivers) == true) ? \in_array($driverName,$drivers) : false;
}
/**
* Get opcache status
*
* @return array
*/
public static function getOpcache(): array
{
return [
'config' => \opcache_get_configuration(),
'status' => \opcache_get_status()
];
}
/**
* Return PDO drivers list
*
* @return array
*/
public static function getPdoDrivers()
{
return (Self::hasPhpExtension('PDO') == false) ? [] : \PDO::getAvailableDrivers();
}
/**
* Return Stream wrappers
*
* @return array
*/
public static function getStreamWrappers(): array
{
return \stream_get_wrappers();
}
/**
* Return true if stream wrapper are installed
*
* @param string $protocol
* @return boolean
*/
public static function hasStreamWrapper(string $protocol): bool
{
return \in_array($protocol,Self::getStreamWrappers());
}
/**
* Get debug backtrace
*
* @return array
*/
public static function getBacktrace()
{
return \debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT);
}
/**
* Return true if script is run in console
*
* @return boolean
*/
public static function isConsole(): bool
{
return (\php_sapi_name() == 'cli');
}
/**
* Output text
*
* @param string|null $text
* @param string|null $eof
* @return void
*/
public static function writeLine(?string $text, $eof = null): void
{
$eof = $eof ?? "\n";
$text = $text ?? '';
echo $text . $eof;
}
/**
* Return OS
*
* @return integer
*/
public static function getOS(): int
{
switch (true) {
case \stristr(PHP_OS,'DAR'): {
return Self::OSX;
}
case \stristr(PHP_OS,'WIN'): {
return Self::WINDOWS;
}
case \stristr(PHP_OS,'LINUX'): {
return Self::LINUX;
}
default: {
return Self::UNKNOWN;
}
}
}
/**
* Get default output
*
* @return string
*/
public static function getDefaultOutput(): string
{
return (DIRECTORY_SEPARATOR == '\\') ? 'NUL' : '/dev/null';
}
}