12
12
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13
13
*/
14
14
15
+ use support \Request ;
16
+ use support \Response ;
17
+ use support \Translation ;
18
+ use support \Container ;
19
+ use support \view \Raw ;
20
+ use support \view \Blade ;
21
+ use support \view \ThinkPHP ;
22
+ use support \view \Twig ;
23
+ use Workerman \Worker ;
24
+ use Webman \App ;
25
+ use Webman \Config ;
26
+ use Webman \Route ;
27
+
15
28
// Phar support.
16
29
if (is_phar ()) {
17
30
define ('BASE_PATH ' , dirname (__DIR__ ));
20
33
}
21
34
define ('WEBMAN_VERSION ' , '1.3.0 ' );
22
35
23
-
24
36
/**
25
37
* @param $return_phar
26
38
* @return false|string
@@ -34,7 +46,6 @@ function base_path($return_phar = true)
34
46
return $ return_phar ? BASE_PATH : $ real_path ;
35
47
}
36
48
37
-
38
49
/**
39
50
* @return string
40
51
*/
@@ -79,11 +90,228 @@ function runtime_path()
79
90
}
80
91
81
92
/**
82
- * @return bool
93
+ * @param int $status
94
+ * @param array $headers
95
+ * @param string $body
96
+ * @return Response
83
97
*/
84
- function is_phar ( )
98
+ function response ( $ body = '' , $ status = 200 , $ headers = array () )
85
99
{
86
- return class_exists (\Phar::class, false ) && Phar::running ();
100
+ return new Response ($ status , $ headers , $ body );
101
+ }
102
+
103
+ /**
104
+ * @param $data
105
+ * @param int $options
106
+ * @return Response
107
+ */
108
+ function json ($ data , $ options = JSON_UNESCAPED_UNICODE )
109
+ {
110
+ return new Response (200 , ['Content-Type ' => 'application/json ' ], json_encode ($ data , $ options ));
111
+ }
112
+
113
+ /**
114
+ * @param $xml
115
+ * @return Response
116
+ */
117
+ function xml ($ xml )
118
+ {
119
+ if ($ xml instanceof SimpleXMLElement) {
120
+ $ xml = $ xml ->asXML ();
121
+ }
122
+ return new Response (200 , ['Content-Type ' => 'text/xml ' ], $ xml );
123
+ }
124
+
125
+ /**
126
+ * @param $data
127
+ * @param string $callback_name
128
+ * @return Response
129
+ */
130
+ function jsonp ($ data , $ callback_name = 'callback ' )
131
+ {
132
+ if (!is_scalar ($ data ) && null !== $ data ) {
133
+ $ data = json_encode ($ data );
134
+ }
135
+ return new Response (200 , [], "$ callback_name( $ data) " );
136
+ }
137
+
138
+ /**
139
+ * @param $location
140
+ * @param int $status
141
+ * @param array $headers
142
+ * @return Response
143
+ */
144
+ function redirect ($ location , $ status = 302 , $ headers = [])
145
+ {
146
+ $ response = new Response ($ status , ['Location ' => $ location ]);
147
+ if (!empty ($ headers )) {
148
+ $ response ->withHeaders ($ headers );
149
+ }
150
+ return $ response ;
151
+ }
152
+
153
+ /**
154
+ * @param $template
155
+ * @param array $vars
156
+ * @param null $app
157
+ * @return Response
158
+ */
159
+ function view ($ template , $ vars = [], $ app = null )
160
+ {
161
+ static $ handler ;
162
+ if (null === $ handler ) {
163
+ $ handler = config ('view.handler ' );
164
+ }
165
+ return new Response (200 , [], $ handler ::render ($ template , $ vars , $ app ));
166
+ }
167
+
168
+ /**
169
+ * @param $template
170
+ * @param array $vars
171
+ * @param null $app
172
+ * @return Response
173
+ */
174
+ function raw_view ($ template , $ vars = [], $ app = null )
175
+ {
176
+ return new Response (200 , [], Raw::render ($ template , $ vars , $ app ));
177
+ }
178
+
179
+ /**
180
+ * @param $template
181
+ * @param array $vars
182
+ * @param null $app
183
+ * @return Response
184
+ */
185
+ function blade_view ($ template , $ vars = [], $ app = null )
186
+ {
187
+ return new Response (200 , [], Blade::render ($ template , $ vars , $ app ));
188
+ }
189
+
190
+ /**
191
+ * @param $template
192
+ * @param array $vars
193
+ * @param null $app
194
+ * @return Response
195
+ */
196
+ function think_view ($ template , $ vars = [], $ app = null )
197
+ {
198
+ return new Response (200 , [], ThinkPHP::render ($ template , $ vars , $ app ));
199
+ }
200
+
201
+ /**
202
+ * @param $template
203
+ * @param array $vars
204
+ * @param null $app
205
+ * @return Response
206
+ */
207
+ function twig_view ($ template , $ vars = [], $ app = null )
208
+ {
209
+ return new Response (200 , [], Twig::render ($ template , $ vars , $ app ));
210
+ }
211
+
212
+ /**
213
+ * @return Request
214
+ */
215
+ function request ()
216
+ {
217
+ return App::request ();
218
+ }
219
+
220
+ /**
221
+ * @param $key
222
+ * @param null $default
223
+ * @return mixed
224
+ */
225
+ function config ($ key = null , $ default = null )
226
+ {
227
+ return Config::get ($ key , $ default );
228
+ }
229
+
230
+ /**
231
+ * @param $name
232
+ * @param ...$parameters
233
+ * @return string
234
+ */
235
+ function route ($ name , ...$ parameters )
236
+ {
237
+ $ route = Route::getByName ($ name );
238
+ if (!$ route ) {
239
+ return '' ;
240
+ }
241
+
242
+ if (!$ parameters ) {
243
+ return $ route ->url ();
244
+ }
245
+
246
+ if (is_array (current ($ parameters ))) {
247
+ $ parameters = current ($ parameters );
248
+ }
249
+
250
+ return $ route ->url ($ parameters );
251
+ }
252
+
253
+ /**
254
+ * @param mixed $key
255
+ * @param mixed $default
256
+ * @return mixed
257
+ */
258
+ function session ($ key = null , $ default = null )
259
+ {
260
+ $ session = request ()->session ();
261
+ if (null === $ key ) {
262
+ return $ session ;
263
+ }
264
+ if (\is_array ($ key )) {
265
+ $ session ->put ($ key );
266
+ return null ;
267
+ }
268
+ if (\strpos ($ key , '. ' )) {
269
+ $ key_array = \explode ('. ' , $ key );
270
+ $ value = $ session ->all ();
271
+ foreach ($ key_array as $ index ) {
272
+ if (!isset ($ value [$ index ])) {
273
+ return $ default ;
274
+ }
275
+ $ value = $ value [$ index ];
276
+ }
277
+ return $ value ;
278
+ }
279
+ return $ session ->get ($ key , $ default );
280
+ }
281
+
282
+ /**
283
+ * @param null|string $id
284
+ * @param array $parameters
285
+ * @param string|null $domain
286
+ * @param string|null $locale
287
+ * @return string
288
+ */
289
+ function trans (string $ id , array $ parameters = [], string $ domain = null , string $ locale = null )
290
+ {
291
+ $ res = Translation::trans ($ id , $ parameters , $ domain , $ locale );
292
+ return $ res === '' ? $ id : $ res ;
293
+ }
294
+
295
+ /**
296
+ * @param null|string $locale
297
+ * @return string
298
+ */
299
+ function locale (string $ locale = null )
300
+ {
301
+ if (!$ locale ) {
302
+ return Translation::getLocale ();
303
+ }
304
+ Translation::setLocale ($ locale );
305
+ }
306
+
307
+ /**
308
+ * 404 not found
309
+ *
310
+ * @return Response
311
+ */
312
+ function not_found ()
313
+ {
314
+ return new Response (404 , [], file_get_contents (public_path () . '/404.html ' ));
87
315
}
88
316
89
317
/**
@@ -127,3 +355,126 @@ function remove_dir($dir)
127
355
return rmdir ($ dir );
128
356
}
129
357
358
+ /**
359
+ * @param $worker
360
+ * @param $class
361
+ */
362
+ function worker_bind ($ worker , $ class )
363
+ {
364
+ $ callback_map = [
365
+ 'onConnect ' ,
366
+ 'onMessage ' ,
367
+ 'onClose ' ,
368
+ 'onError ' ,
369
+ 'onBufferFull ' ,
370
+ 'onBufferDrain ' ,
371
+ 'onWorkerStop ' ,
372
+ 'onWebSocketConnect '
373
+ ];
374
+ foreach ($ callback_map as $ name ) {
375
+ if (method_exists ($ class , $ name )) {
376
+ $ worker ->$ name = [$ class , $ name ];
377
+ }
378
+ }
379
+ if (method_exists ($ class , 'onWorkerStart ' )) {
380
+ call_user_func ([$ class , 'onWorkerStart ' ], $ worker );
381
+ }
382
+ }
383
+
384
+ /**
385
+ * @param $process_name
386
+ * @param $config
387
+ * @return void
388
+ */
389
+ function worker_start ($ process_name , $ config )
390
+ {
391
+ $ worker = new Worker ($ config ['listen ' ] ?? null , $ config ['context ' ] ?? []);
392
+ $ property_map = [
393
+ 'count ' ,
394
+ 'user ' ,
395
+ 'group ' ,
396
+ 'reloadable ' ,
397
+ 'reusePort ' ,
398
+ 'transport ' ,
399
+ 'protocol ' ,
400
+ ];
401
+ $ worker ->name = $ process_name ;
402
+ foreach ($ property_map as $ property ) {
403
+ if (isset ($ config [$ property ])) {
404
+ $ worker ->$ property = $ config [$ property ];
405
+ }
406
+ }
407
+
408
+ $ worker ->onWorkerStart = function ($ worker ) use ($ config ) {
409
+ require_once base_path () . '/support/bootstrap.php ' ;
410
+
411
+ foreach ($ config ['services ' ] ?? [] as $ server ) {
412
+ if (!class_exists ($ server ['handler ' ])) {
413
+ echo "process error: class {$ server ['handler ' ]} not exists \r\n" ;
414
+ continue ;
415
+ }
416
+ $ listen = new Worker ($ server ['listen ' ] ?? null , $ server ['context ' ] ?? []);
417
+ if (isset ($ server ['listen ' ])) {
418
+ echo "listen: {$ server ['listen ' ]}\n" ;
419
+ }
420
+ $ instance = Container::make ($ server ['handler ' ], $ server ['constructor ' ] ?? []);
421
+ worker_bind ($ listen , $ instance );
422
+ $ listen ->listen ();
423
+ }
424
+
425
+ if (isset ($ config ['handler ' ])) {
426
+ if (!class_exists ($ config ['handler ' ])) {
427
+ echo "process error: class {$ config ['handler ' ]} not exists \r\n" ;
428
+ return ;
429
+ }
430
+
431
+ $ instance = Container::make ($ config ['handler ' ], $ config ['constructor ' ] ?? []);
432
+ worker_bind ($ worker , $ instance );
433
+ }
434
+
435
+ };
436
+ }
437
+
438
+ /**
439
+ * Phar support.
440
+ * Compatible with the 'realpath' function in the phar file.
441
+ *
442
+ * @param string $file_path
443
+ * @return string
444
+ */
445
+ function get_realpath (string $ file_path ): string
446
+ {
447
+ if (strpos ($ file_path , 'phar:// ' ) === 0 ) {
448
+ return $ file_path ;
449
+ } else {
450
+ return realpath ($ file_path );
451
+ }
452
+ }
453
+
454
+ /**
455
+ * @return bool
456
+ */
457
+ function is_phar ()
458
+ {
459
+ return class_exists (\Phar::class, false ) && Phar::running ();
460
+ }
461
+
462
+ /**
463
+ * @return int
464
+ */
465
+ function cpu_count ()
466
+ {
467
+ // Windows does not support the number of processes setting.
468
+ if (\DIRECTORY_SEPARATOR === '\\' ) {
469
+ return 1 ;
470
+ }
471
+ $ count = 4 ;
472
+ if (is_callable ('shell_exec ' )) {
473
+ if (strtolower (PHP_OS ) === 'darwin ' ) {
474
+ $ count = (int )shell_exec ('sysctl -n machdep.cpu.core_count ' );
475
+ } else {
476
+ $ count = (int )shell_exec ('nproc ' );
477
+ }
478
+ }
479
+ return $ count > 0 ? $ count : 4 ;
480
+ }
0 commit comments