helpers.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<[email protected]>
  10. * @copyright walkor<[email protected]>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. use support\Request;
  15. use support\Response;
  16. use support\view\Raw;
  17. use support\bootstrap\Translation;
  18. use Webman\App;
  19. use Webman\Config;
  20. use Webman\Exception\ClassNotFoundException;
  21. define('BASE_PATH', realpath(__DIR__ . '/../'));
  22. /**
  23. * @return string
  24. */
  25. function base_path()
  26. {
  27. return BASE_PATH;
  28. }
  29. /**
  30. * @return string
  31. */
  32. function app_path()
  33. {
  34. return BASE_PATH . DIRECTORY_SEPARATOR . 'app';
  35. }
  36. /**
  37. * @return string
  38. */
  39. function public_path()
  40. {
  41. return BASE_PATH . DIRECTORY_SEPARATOR . 'public';
  42. }
  43. /**
  44. * @return string
  45. */
  46. function config_path()
  47. {
  48. return BASE_PATH . DIRECTORY_SEPARATOR . 'config';
  49. }
  50. /**
  51. * @return string
  52. */
  53. function runtime_path()
  54. {
  55. return BASE_PATH . DIRECTORY_SEPARATOR . 'runtime';
  56. }
  57. /**
  58. * @param int $status
  59. * @param array $headers
  60. * @param string $body
  61. * @return Response
  62. */
  63. function response($body = '', $status = 200, $headers = array())
  64. {
  65. return new Response($status, $headers, $body);
  66. }
  67. /**
  68. * @param $data
  69. * @param int $options
  70. * @return Response
  71. */
  72. function json($data, $options = JSON_UNESCAPED_UNICODE)
  73. {
  74. return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
  75. }
  76. /**
  77. * @param $xml
  78. * @return Response
  79. */
  80. function xml($xml)
  81. {
  82. if ($xml instanceof SimpleXMLElement) {
  83. $xml = $xml->asXML();
  84. }
  85. return new Response(200, ['Content-Type' => 'text/xml'], $xml);
  86. }
  87. /**
  88. * @param $data
  89. * @param string $callback_name
  90. * @return Response
  91. */
  92. function jsonp($data, $callback_name = 'callback')
  93. {
  94. if (!is_scalar($data) && null !== $data) {
  95. $data = json_encode($data);
  96. }
  97. return new Response(200, [], "$callback_name($data)");
  98. }
  99. /**
  100. * @param $location
  101. * @param int $status
  102. * @param array $headers
  103. * @return Response
  104. */
  105. function redirect($location, $status = 302, $headers = [])
  106. {
  107. $response = new Response($status, ['Location' => $location]);
  108. if (!empty($headers)) {
  109. $response->withHeaders($headers);
  110. }
  111. return $response;
  112. }
  113. /**
  114. * @param $template
  115. * @param array $vars
  116. * @param null $app
  117. * @return string
  118. */
  119. function view($template, $vars = [], $app = null)
  120. {
  121. static $handler;
  122. if (null === $handler) {
  123. $handler = config('view.handler');
  124. }
  125. return new Response(200, [], $handler::render($template, $vars, $app));
  126. }
  127. /**
  128. * @return Request
  129. */
  130. function request()
  131. {
  132. return App::request();
  133. }
  134. /**
  135. * @param $key
  136. * @param null $default
  137. * @return mixed
  138. */
  139. function config($key = null, $default = null)
  140. {
  141. return Config::get($key, $default);
  142. }
  143. if (!function_exists('env')) {
  144. /**
  145. * @param $key
  146. * @param null $default
  147. * @return array|bool|false|mixed|string
  148. */
  149. function env($key, $default = null)
  150. {
  151. $value = getenv($key);
  152. if ($value === false) {
  153. return $default;
  154. }
  155. switch (strtolower($value)) {
  156. case 'true':
  157. case '(true)':
  158. return true;
  159. case 'false':
  160. case '(false)':
  161. return false;
  162. case 'empty':
  163. case '(empty)':
  164. return '';
  165. case 'null':
  166. case '(null)':
  167. return null;
  168. }
  169. if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
  170. return substr($value, 1, -1);
  171. }
  172. return $value;
  173. }
  174. }
  175. /**
  176. * @param null|string $id
  177. * @param array $parameters
  178. * @param string|null $domain
  179. * @param string|null $locale
  180. * @return string
  181. */
  182. function trans(string $id, array $parameters = [], string $domain = null, string $locale = null)
  183. {
  184. return Translation::trans($id, $parameters, $domain, $locale);
  185. }
  186. /**
  187. * @param null|string $locale
  188. * @return string
  189. */
  190. function locale(string $locale)
  191. {
  192. if (!$locale) {
  193. return Translation::getLocale();
  194. }
  195. Translation::setLocale($locale);
  196. }
  197. /**
  198. * @param $worker
  199. * @param $class
  200. */
  201. function worker_bind($worker, $class) {
  202. $callback_map = [
  203. 'onConnect',
  204. 'onMessage',
  205. 'onClose',
  206. 'onError',
  207. 'onBufferFull',
  208. 'onBufferDrain',
  209. 'onWorkerStop',
  210. 'onWebSocketConnect'
  211. ];
  212. foreach ($callback_map as $name) {
  213. if (method_exists($class, $name)) {
  214. $worker->$name = [$class, $name];
  215. }
  216. }
  217. if (method_exists($class, 'onWorkerStart')) {
  218. call_user_func([$class, 'onWorkerStart'], $worker);
  219. }
  220. }
  221. /**
  222. * @return int
  223. */
  224. function cpu_count() {
  225. if (strtolower(PHP_OS) === 'darwin') {
  226. $count = shell_exec('sysctl -n machdep.cpu.core_count');
  227. } else {
  228. $count = shell_exec('nproc');
  229. }
  230. $count = (int)$count > 0 ? (int)$count : 4;
  231. return $count;
  232. }