Skip to content

Commit 1d10887

Browse files
authored
Update helpers.php
1 parent aa9b662 commit 1d10887

File tree

1 file changed

+356
-5
lines changed

1 file changed

+356
-5
lines changed

support/helpers.php

+356-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1313
*/
1414

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+
1528
// Phar support.
1629
if (is_phar()) {
1730
define('BASE_PATH', dirname(__DIR__));
@@ -20,7 +33,6 @@
2033
}
2134
define('WEBMAN_VERSION', '1.3.0');
2235

23-
2436
/**
2537
* @param $return_phar
2638
* @return false|string
@@ -34,7 +46,6 @@ function base_path($return_phar = true)
3446
return $return_phar ? BASE_PATH : $real_path;
3547
}
3648

37-
3849
/**
3950
* @return string
4051
*/
@@ -79,11 +90,228 @@ function runtime_path()
7990
}
8091

8192
/**
82-
* @return bool
93+
* @param int $status
94+
* @param array $headers
95+
* @param string $body
96+
* @return Response
8397
*/
84-
function is_phar()
98+
function response($body = '', $status = 200, $headers = array())
8599
{
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'));
87315
}
88316

89317
/**
@@ -127,3 +355,126 @@ function remove_dir($dir)
127355
return rmdir($dir);
128356
}
129357

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

Comments
 (0)