123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- /**
- * This file is part of webman.
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the MIT-LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @author walkor<[email protected]>
- * @copyright walkor<[email protected]>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- use support\Request;
- use support\Response;
- use support\view\Raw;
- use support\bootstrap\Translation;
- use Webman\App;
- use Webman\Config;
- use Webman\Exception\ClassNotFoundException;
- define('BASE_PATH', realpath(__DIR__ . '/../'));
- /**
- * @return string
- */
- function base_path()
- {
- return BASE_PATH;
- }
- /**
- * @return string
- */
- function app_path()
- {
- return BASE_PATH . DIRECTORY_SEPARATOR . 'app';
- }
- /**
- * @return string
- */
- function public_path()
- {
- return BASE_PATH . DIRECTORY_SEPARATOR . 'public';
- }
- /**
- * @return string
- */
- function config_path()
- {
- return BASE_PATH . DIRECTORY_SEPARATOR . 'config';
- }
- /**
- * @return string
- */
- function runtime_path()
- {
- return BASE_PATH . DIRECTORY_SEPARATOR . 'runtime';
- }
- /**
- * @param int $status
- * @param array $headers
- * @param string $body
- * @return Response
- */
- function response($body = '', $status = 200, $headers = array())
- {
- return new Response($status, $headers, $body);
- }
- /**
- * @param $data
- * @param int $options
- * @return Response
- */
- function json($data, $options = JSON_UNESCAPED_UNICODE)
- {
- return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
- }
- /**
- * @param $xml
- * @return Response
- */
- function xml($xml)
- {
- if ($xml instanceof SimpleXMLElement) {
- $xml = $xml->asXML();
- }
- return new Response(200, ['Content-Type' => 'text/xml'], $xml);
- }
- /**
- * @param $data
- * @param string $callback_name
- * @return Response
- */
- function jsonp($data, $callback_name = 'callback')
- {
- if (!is_scalar($data) && null !== $data) {
- $data = json_encode($data);
- }
- return new Response(200, [], "$callback_name($data)");
- }
- /**
- * @param $location
- * @param int $status
- * @param array $headers
- * @return Response
- */
- function redirect($location, $status = 302, $headers = [])
- {
- $response = new Response($status, ['Location' => $location]);
- if (!empty($headers)) {
- $response->withHeaders($headers);
- }
- return $response;
- }
- /**
- * @param $template
- * @param array $vars
- * @param null $app
- * @return string
- */
- function view($template, $vars = [], $app = null)
- {
- static $handler;
- if (null === $handler) {
- $handler = config('view.handler');
- }
- return new Response(200, [], $handler::render($template, $vars, $app));
- }
- /**
- * @return Request
- */
- function request()
- {
- return App::request();
- }
- /**
- * @param $key
- * @param null $default
- * @return mixed
- */
- function config($key = null, $default = null)
- {
- return Config::get($key, $default);
- }
- if (!function_exists('env')) {
- /**
- * @param $key
- * @param null $default
- * @return array|bool|false|mixed|string
- */
- function env($key, $default = null)
- {
- $value = getenv($key);
- if ($value === false) {
- return $default;
- }
- switch (strtolower($value)) {
- case 'true':
- case '(true)':
- return true;
- case 'false':
- case '(false)':
- return false;
- case 'empty':
- case '(empty)':
- return '';
- case 'null':
- case '(null)':
- return null;
- }
- if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
- return substr($value, 1, -1);
- }
- return $value;
- }
- }
- /**
- * @param null|string $id
- * @param array $parameters
- * @param string|null $domain
- * @param string|null $locale
- * @return string
- */
- function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
- {
- return Translation::trans($id, $parameters, $domain, $locale);
- }
- /**
- * @param null|string $locale
- * @return string
- */
- function locale(string $locale)
- {
- if (!$locale) {
- return Translation::getLocale();
- }
- Translation::setLocale($locale);
- }
- /**
- * @param $worker
- * @param $class
- */
- function worker_bind($worker, $class) {
- $callback_map = [
- 'onConnect',
- 'onMessage',
- 'onClose',
- 'onError',
- 'onBufferFull',
- 'onBufferDrain',
- 'onWorkerStop',
- 'onWebSocketConnect'
- ];
- foreach ($callback_map as $name) {
- if (method_exists($class, $name)) {
- $worker->$name = [$class, $name];
- }
- }
- if (method_exists($class, 'onWorkerStart')) {
- call_user_func([$class, 'onWorkerStart'], $worker);
- }
- }
- /**
- * @return int
- */
- function cpu_count() {
- if (strtolower(PHP_OS) === 'darwin') {
- $count = shell_exec('sysctl -n machdep.cpu.core_count');
- } else {
- $count = shell_exec('nproc');
- }
- $count = (int)$count > 0 ? (int)$count : 4;
- return $count;
- }
|