123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- <?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<walkor@workerman.net>
- * @copyright walkor<walkor@workerman.net>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- use support\Request;
- use support\Response;
- use support\Translation;
- use support\Container;
- use support\view\Raw;
- use support\view\Blade;
- use support\view\ThinkPHP;
- use support\view\Twig;
- use Workerman\Worker;
- use Webman\App;
- use Webman\Config;
- use Webman\Route;
- // Phar support.
- if (is_phar()) {
- define('BASE_PATH', dirname(__DIR__));
- } else {
- define('BASE_PATH', realpath(__DIR__ . '/../'));
- }
- define('WEBMAN_VERSION', '1.3.0');
- /**
- * @param $return_phar
- * @return false|string
- */
- function base_path($return_phar = true)
- {
- static $real_path = '';
- if (!$real_path) {
- $real_path = is_phar() ? dirname(Phar::running(false)) : BASE_PATH;
- }
- return $return_phar ? BASE_PATH : $real_path;
- }
- /**
- * @return string
- */
- function app_path()
- {
- return BASE_PATH . DIRECTORY_SEPARATOR . 'app';
- }
- /**
- * @return string
- */
- function public_path()
- {
- static $path = '';
- if (!$path) {
- $path = config('app.public_path', BASE_PATH . DIRECTORY_SEPARATOR . 'public');
- }
- return $path;
- }
- /**
- * @return string
- */
- function config_path()
- {
- return BASE_PATH . DIRECTORY_SEPARATOR . 'config';
- }
- /**
- * Phar support.
- * Compatible with the 'realpath' function in the phar file.
- *
- * @return string
- */
- function runtime_path()
- {
- static $path = '';
- if (!$path) {
- $path = config('app.runtime_path', BASE_PATH . DIRECTORY_SEPARATOR . 'runtime');
- }
- return $path;
- }
- /**
- * @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));
- }
- /**
- * Notes:api成功返回参数
- * @param $result
- * @param string $message
- * @param int $options
- * @return Response
- * User: yym
- * Date: 2022/7/26
- */
- function json_success($result, $message = '成功', $options = JSON_UNESCAPED_UNICODE)
- {
- $data = array(
- 'code' => 200,
- 'msg' => $message,
- 'data' => $result
- );
- 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 Response
- */
- function view($template, $vars = [], $app = null)
- {
- static $handler;
- if (null === $handler) {
- $handler = config('view.handler');
- }
- return new Response(200, [], $handler::render($template, $vars, $app));
- }
- /**
- * @param $template
- * @param array $vars
- * @param null $app
- * @return Response
- */
- function raw_view($template, $vars = [], $app = null)
- {
- return new Response(200, [], Raw::render($template, $vars, $app));
- }
- /**
- * @param $template
- * @param array $vars
- * @param null $app
- * @return Response
- */
- function blade_view($template, $vars = [], $app = null)
- {
- return new Response(200, [], Blade::render($template, $vars, $app));
- }
- /**
- * @param $template
- * @param array $vars
- * @param null $app
- * @return Response
- */
- function think_view($template, $vars = [], $app = null)
- {
- return new Response(200, [], ThinkPHP::render($template, $vars, $app));
- }
- /**
- * @param $template
- * @param array $vars
- * @param null $app
- * @return Response
- */
- function twig_view($template, $vars = [], $app = null)
- {
- return new Response(200, [], Twig::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);
- }
- /**
- * @param $name
- * @param ...$parameters
- * @return string
- */
- function route($name, ...$parameters)
- {
- $route = Route::getByName($name);
- if (!$route) {
- return '';
- }
- if (!$parameters) {
- return $route->url();
- }
- if (is_array(current($parameters))) {
- $parameters = current($parameters);
- }
- return $route->url($parameters);
- }
- /**
- * @param mixed $key
- * @param mixed $default
- * @return mixed
- */
- function session($key = null, $default = null)
- {
- $session = request()->session();
- if (null === $key) {
- return $session;
- }
- if (\is_array($key)) {
- $session->put($key);
- return null;
- }
- if (\strpos($key, '.')) {
- $key_array = \explode('.', $key);
- $value = $session->all();
- foreach ($key_array as $index) {
- if (!isset($value[$index])) {
- return $default;
- }
- $value = $value[$index];
- }
- return $value;
- }
- return $session->get($key, $default);
- }
- /**
- * @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)
- {
- $res = Translation::trans($id, $parameters, $domain, $locale);
- return $res === '' ? $id : $res;
- }
- /**
- * @param null|string $locale
- * @return string
- */
- function locale(string $locale = null)
- {
- if (!$locale) {
- return Translation::getLocale();
- }
- Translation::setLocale($locale);
- }
- /**
- * 404 not found
- *
- * @return Response
- */
- function not_found()
- {
- return new Response(404, [], file_get_contents(public_path() . '/404.html'));
- }
- /**
- * Copy dir.
- * @param $source
- * @param $dest
- * @param bool $overwrite
- * @return void
- */
- function copy_dir($source, $dest, $overwrite = false)
- {
- if (is_dir($source)) {
- if (!is_dir($dest)) {
- mkdir($dest);
- }
- $files = scandir($source);
- foreach ($files as $file) {
- if ($file !== "." && $file !== "..") {
- copy_dir("$source/$file", "$dest/$file");
- }
- }
- } else if (file_exists($source) && ($overwrite || !file_exists($dest))) {
- copy($source, $dest);
- }
- }
- /**
- * Remove dir.
- * @param $dir
- * @return bool
- */
- function remove_dir($dir)
- {
- if (is_link($dir) || is_file($dir)) {
- return unlink($dir);
- }
- $files = array_diff(scandir($dir), array('.', '..'));
- foreach ($files as $file) {
- (is_dir("$dir/$file") && !is_link($dir)) ? remove_dir("$dir/$file") : unlink("$dir/$file");
- }
- return rmdir($dir);
- }
- /**
- * @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);
- }
- }
- /**
- * @param $process_name
- * @param $config
- * @return void
- */
- function worker_start($process_name, $config)
- {
- $worker = new Worker($config['listen'] ?? null, $config['context'] ?? []);
- $property_map = [
- 'count',
- 'user',
- 'group',
- 'reloadable',
- 'reusePort',
- 'transport',
- 'protocol',
- ];
- $worker->name = $process_name;
- foreach ($property_map as $property) {
- if (isset($config[$property])) {
- $worker->$property = $config[$property];
- }
- }
- $worker->onWorkerStart = function ($worker) use ($config) {
- require_once base_path() . '/support/bootstrap.php';
- foreach ($config['services'] ?? [] as $server) {
- if (!class_exists($server['handler'])) {
- echo "process error: class {$server['handler']} not exists\r\n";
- continue;
- }
- $listen = new Worker($server['listen'] ?? null, $server['context'] ?? []);
- if (isset($server['listen'])) {
- echo "listen: {$server['listen']}\n";
- }
- $instance = Container::make($server['handler'], $server['constructor'] ?? []);
- worker_bind($listen, $instance);
- $listen->listen();
- }
- if (isset($config['handler'])) {
- if (!class_exists($config['handler'])) {
- echo "process error: class {$config['handler']} not exists\r\n";
- return;
- }
- $instance = Container::make($config['handler'], $config['constructor'] ?? []);
- worker_bind($worker, $instance);
- }
- };
- }
- /**
- * Phar support.
- * Compatible with the 'realpath' function in the phar file.
- *
- * @param string $file_path
- * @return string
- */
- function get_realpath(string $file_path): string
- {
- if (strpos($file_path, 'phar://') === 0) {
- return $file_path;
- } else {
- return realpath($file_path);
- }
- }
- /**
- * @return bool
- */
- function is_phar()
- {
- return class_exists(\Phar::class, false) && Phar::running();
- }
- /**
- * @return int
- */
- function cpu_count()
- {
- // Windows does not support the number of processes setting.
- if (\DIRECTORY_SEPARATOR === '\\') {
- return 1;
- }
- $count = 4;
- if (is_callable('shell_exec')) {
- if (strtolower(PHP_OS) === 'darwin') {
- $count = (int)shell_exec('sysctl -n machdep.cpu.core_count');
- } else {
- $count = (int)shell_exec('nproc');
- }
- }
- return $count > 0 ? $count : 4;
- }
- if (!function_exists('events')) {
- /**
- * 触发事件
- * @param string $list_name 事件名(
- * @param array $args 参数
- * @return mixed
- */
- function events($list_name, $args = array())
- {
- $config = config('plugin.tinywan.event.app.event.listener');
- if(!isset($config[$list_name]))
- {
- return false;
- }
- $event = new $config[$list_name]($args);
- return \Tinywan\Event::trigger($event, $list_name);
- }
- }
- /**
- * Notes:图片路径处理
- * @param string $path 图片路径
- * @param string $default //默认显示图片
- * @return string
- * User: yym
- * Date: 2022/8/5
- */
- function get_image_path(string $path, string $default = '')
- {
- if(empty($path))
- {
- return $default;
- }
- if(strstr($path, 'http') !== false || strstr($path, 'https') !== false)
- {
- return $path;
- }
- // if(!file_exists(public_path() . $path))
- // {
- // return $default;
- // }
- $config = config('plugin.tinywan.storage.app.storage');
- $host_url = $config[$config['default']]['domain'];
- return $host_url . $path;
- }
- /**解密
- * @param string $string 需要解密的字符串
- * @param string $key 密钥
- * @return string
- */
- function decrypt($string, $key = 'b5b84e8b3b55c6daaf60825ff1a95c46')
- {
- // 对接java,服务商做的AES加密通过SHA1PRNG算法(只要password一样,每次生成的数组都是一样的),Java的加密源码翻译php如下:
- $string = base64_decode($string);
- $key = substr(openssl_digest(openssl_digest($key, 'sha1', true), 'sha1', true), 0, 16);
- $decrypted = openssl_decrypt($string, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
- return $decrypted;
- }
- if (!function_exists('get_ip')) {
- /**
- * Notes:获取客户端ip
- * @return string
- * User: yym
- * Date: 2022/9/14
- */
- function get_ip()
- {
- return request()->getRealIp();
- }
- }
|