-
Notifications
You must be signed in to change notification settings - Fork 12
/
Async.php
199 lines (172 loc) · 3.94 KB
/
Async.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
<?php namespace ZN\Console;
/**
* ZN PHP Web Framework
*
* "Simplicity is the ultimate sophistication." ~ Da Vinci
*
* @package ZN
* @license MIT [http://opensource.org/licenses/MIT]
* @author Ozan UYKUN [ozan@znframework.com]
*/
use Throwable;
use ZN\Config;
use ZN\Protection\Json;
/**
* @codeCoverageIgnore
*/
class Async
{
/**
* Keeps process ID.
*
* @var string $procId
*/
protected static $procId = '';
/**
* Keeps process directory.
*
* @var string $procDir
*/
protected static $procDir = FILES_DIR;
/**
* Set process directory.
*
* @param string $directory
*
* @return self
*/
public static function setProcDirectory(string $directory) : Async
{
self::$procDir = $directory;
return new self;
}
/**
* Get process data.
*
* @param string $procId
*
* @return array
*/
public static function getData(string $procId) : array
{
$return = Json::decodeArray(file_get_contents($procId));
return $return;
}
/**
* Get process Id.
*/
public static function getProcId() : string
{
return self::$procId;
}
/**
* Run command
*
* @param string $command
* @param array $data = []
*/
public static function run(string $command, array $data = []) : string
{
self::$procId = $procId = self::$procDir . uniqid();
$processor = Config::default('ZN\Prompt\PromptDefaultConfiguration')::get('Services', 'processor');
if( ! file_exists($processor['path']) )
{
$path = 'php';
}
else
{
$path = $processor['path'];
}
$open = proc_open($path . ' zerocore ' . $command . ' "' . $procId . '"', [], $arr);
$data['status'] = proc_get_status($open);
file_put_contents($procId, Json::encode($data));
return $procId;
}
/**
* Command process
*
* @param string $procId
* @param callback $callable
* @param bool $report = false
*/
public static function process(string $procId, callable $callable, bool $report = false) : void
{
self::$procId = $procId;
$data = self::getData($procId);
try
{
$callable($data, $procId);
}
catch( Throwable $e )
{
if( $report )
{
$error =
[
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTrace()
];
self::report($error);
}
}
self::remove($procId);
}
/**
* Remove process ID
*
* @param string $procId
*
* @return bool
*/
public static function remove(string $procId) : bool
{
if( is_file($procId) )
{
return unlink($procId);
}
return false;
}
/**
* Create report
*
* @param array $data
*
* @return int
*/
public static function report(array $data) : int
{
return file_put_contents(self::$procId . '-report', Json::encode($data));
}
/**
* Status
*
* @param string ...$procIds
*
* @return array
*/
public static function status(string ...$procIds) : array
{
$pending = [];
foreach( $procIds as $procId )
{
if( is_file($procId) )
{
$pending[$procId] = 1; # pending.
}
}
return $pending;
}
/**
* Is finish
*
* @param string ...$procIds
*
* @return bool
*/
public static function isFinish(string ...$procIds) : bool
{
return ! in_array(1, self::status(...$procIds));
}
}