|
|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
/**
|
|
|
* This file is part of webman.
|
|
|
*
|
|
|
@@ -12,10 +13,10 @@
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
*/
|
|
|
|
|
|
+use support\Container;
|
|
|
use support\Request;
|
|
|
use support\Response;
|
|
|
use support\Translation;
|
|
|
-use support\Container;
|
|
|
use support\view\Raw;
|
|
|
use support\view\Blade;
|
|
|
use support\view\ThinkPHP;
|
|
|
@@ -25,96 +26,127 @@ 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__ . '/../'));
|
|
|
+// Webman version
|
|
|
+const WEBMAN_VERSION = '1.4';
|
|
|
+
|
|
|
+// Project base path
|
|
|
+define('BASE_PATH', dirname(__DIR__));
|
|
|
+
|
|
|
+/**
|
|
|
+ * return the program execute directory
|
|
|
+ * @param string $path
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+function run_path(string $path = ''): string
|
|
|
+{
|
|
|
+ static $run_path = '';
|
|
|
+ if (!$run_path) {
|
|
|
+ $run_path = \is_phar() ? \dirname(\Phar::running(false)) : BASE_PATH;
|
|
|
+ }
|
|
|
+ return \path_combine($run_path, $path);
|
|
|
}
|
|
|
-define('WEBMAN_VERSION', '1.3.0');
|
|
|
|
|
|
/**
|
|
|
- * @param $return_phar
|
|
|
- * @return false|string
|
|
|
+ * if the param $path equal false,will return this program current execute directory
|
|
|
+ * @param string|false $path
|
|
|
+ * @return string
|
|
|
*/
|
|
|
-function base_path($return_phar = true)
|
|
|
+function base_path($path = ''): string
|
|
|
{
|
|
|
- static $real_path = '';
|
|
|
- if (!$real_path) {
|
|
|
- $real_path = is_phar() ? dirname(Phar::running(false)) : BASE_PATH;
|
|
|
+ if (false === $path) {
|
|
|
+ return \run_path();
|
|
|
}
|
|
|
- return $return_phar ? BASE_PATH : $real_path;
|
|
|
+ return \path_combine(BASE_PATH, $path);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * App path
|
|
|
+ * @param string $path
|
|
|
* @return string
|
|
|
*/
|
|
|
-function app_path()
|
|
|
+function app_path(string $path = ''): string
|
|
|
{
|
|
|
- return BASE_PATH . DIRECTORY_SEPARATOR . 'app';
|
|
|
+ return \path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'app', $path);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Public path
|
|
|
+ * @param string $path
|
|
|
* @return string
|
|
|
*/
|
|
|
-function public_path()
|
|
|
+function public_path(string $path = ''): string
|
|
|
{
|
|
|
- static $path = '';
|
|
|
- if (!$path) {
|
|
|
- $path = get_realpath(config('app.public_path', BASE_PATH . DIRECTORY_SEPARATOR . 'public'));
|
|
|
+ static $public_path = '';
|
|
|
+ if (!$public_path) {
|
|
|
+ $public_path = \config('app.public_path') ? : \run_path('public');
|
|
|
}
|
|
|
- return $path;
|
|
|
+ return \path_combine($public_path, $path);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Config path
|
|
|
+ * @param string $path
|
|
|
* @return string
|
|
|
*/
|
|
|
-function config_path()
|
|
|
+function config_path(string $path = ''): string
|
|
|
{
|
|
|
- return BASE_PATH . DIRECTORY_SEPARATOR . 'config';
|
|
|
+ return \path_combine(BASE_PATH . DIRECTORY_SEPARATOR . 'config', $path);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Phar support.
|
|
|
- * Compatible with the 'realpath' function in the phar file.
|
|
|
- *
|
|
|
+ * Runtime path
|
|
|
+ * @param string $path
|
|
|
* @return string
|
|
|
*/
|
|
|
-function runtime_path()
|
|
|
+function runtime_path(string $path = ''): string
|
|
|
{
|
|
|
- static $path = '';
|
|
|
- if (!$path) {
|
|
|
- $path = get_realpath(config('app.runtime_path', BASE_PATH . DIRECTORY_SEPARATOR . 'runtime'));
|
|
|
+ static $runtime_path = '';
|
|
|
+ if (!$runtime_path) {
|
|
|
+ $runtime_path = \config('app.runtime_path') ? : \run_path('runtime');
|
|
|
}
|
|
|
- return $path;
|
|
|
+ return \path_combine($runtime_path, $path);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Generate paths based on given information
|
|
|
+ * @param string $front
|
|
|
+ * @param string $back
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+function path_combine(string $front, string $back): string
|
|
|
+{
|
|
|
+ return $front . ($back ? (DIRECTORY_SEPARATOR . ltrim($back, DIRECTORY_SEPARATOR)) : $back);
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Response
|
|
|
* @param int $status
|
|
|
* @param array $headers
|
|
|
* @param string $body
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function response($body = '', $status = 200, $headers = array())
|
|
|
+function response(string $body = '', int $status = 200, array $headers = []): Response
|
|
|
{
|
|
|
return new Response($status, $headers, $body);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Json response
|
|
|
* @param $data
|
|
|
* @param int $options
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function json($data, $options = JSON_UNESCAPED_UNICODE)
|
|
|
+function json($data, int $options = JSON_UNESCAPED_UNICODE): Response
|
|
|
{
|
|
|
- return new Response(200, ['Content-Type' => 'application/json'], json_encode($data, $options));
|
|
|
+ return new Response(200, ['Content-Type' => 'application/json'], \json_encode($data, $options));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Xml response
|
|
|
* @param $xml
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function xml($xml)
|
|
|
+function xml($xml): Response
|
|
|
{
|
|
|
if ($xml instanceof SimpleXMLElement) {
|
|
|
$xml = $xml->asXML();
|
|
|
@@ -123,25 +155,27 @@ function xml($xml)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Jsonp response
|
|
|
* @param $data
|
|
|
* @param string $callback_name
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function jsonp($data, $callback_name = 'callback')
|
|
|
+function jsonp($data, string $callback_name = 'callback'): Response
|
|
|
{
|
|
|
- if (!is_scalar($data) && null !== $data) {
|
|
|
- $data = json_encode($data);
|
|
|
+ if (!\is_scalar($data) && null !== $data) {
|
|
|
+ $data = \json_encode($data);
|
|
|
}
|
|
|
return new Response(200, [], "$callback_name($data)");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $location
|
|
|
+ * Redirect response
|
|
|
+ * @param string $location
|
|
|
* @param int $status
|
|
|
* @param array $headers
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function redirect($location, $status = 302, $headers = [])
|
|
|
+function redirect(string $location, int $status = 302, array $headers = []): Response
|
|
|
{
|
|
|
$response = new Response($status, ['Location' => $location]);
|
|
|
if (!empty($headers)) {
|
|
|
@@ -151,66 +185,72 @@ function redirect($location, $status = 302, $headers = [])
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $template
|
|
|
+ * View response
|
|
|
+ * @param string $template
|
|
|
* @param array $vars
|
|
|
- * @param null $app
|
|
|
+ * @param string|null $app
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function view($template, $vars = [], $app = null)
|
|
|
+function view(string $template, array $vars = [], string $app = null): Response
|
|
|
{
|
|
|
- static $handler;
|
|
|
- if (null === $handler) {
|
|
|
- $handler = config('view.handler');
|
|
|
- }
|
|
|
+ $request = \request();
|
|
|
+ $plugin = $request->plugin ?? '';
|
|
|
+ $handler = \config($plugin ? "plugin.$plugin.view.handler" : 'view.handler');
|
|
|
return new Response(200, [], $handler::render($template, $vars, $app));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $template
|
|
|
+ * Raw view response
|
|
|
+ * @param string $template
|
|
|
* @param array $vars
|
|
|
- * @param null $app
|
|
|
+ * @param string|null $app
|
|
|
* @return Response
|
|
|
+ * @throws Throwable
|
|
|
*/
|
|
|
-function raw_view($template, $vars = [], $app = null)
|
|
|
+function raw_view(string $template, array $vars = [], string $app = null): Response
|
|
|
{
|
|
|
return new Response(200, [], Raw::render($template, $vars, $app));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $template
|
|
|
+ * Blade view response
|
|
|
+ * @param string $template
|
|
|
* @param array $vars
|
|
|
- * @param null $app
|
|
|
+ * @param string|null $app
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function blade_view($template, $vars = [], $app = null)
|
|
|
+function blade_view(string $template, array $vars = [], string $app = null): Response
|
|
|
{
|
|
|
return new Response(200, [], Blade::render($template, $vars, $app));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $template
|
|
|
+ * Think view response
|
|
|
+ * @param string $template
|
|
|
* @param array $vars
|
|
|
- * @param null $app
|
|
|
+ * @param string|null $app
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function think_view($template, $vars = [], $app = null)
|
|
|
+function think_view(string $template, array $vars = [], string $app = null): Response
|
|
|
{
|
|
|
return new Response(200, [], ThinkPHP::render($template, $vars, $app));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $template
|
|
|
+ * Twig view response
|
|
|
+ * @param string $template
|
|
|
* @param array $vars
|
|
|
- * @param null $app
|
|
|
+ * @param string|null $app
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function twig_view($template, $vars = [], $app = null)
|
|
|
+function twig_view(string $template, array $vars = [], string $app = null): Response
|
|
|
{
|
|
|
return new Response(200, [], Twig::render($template, $vars, $app));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @return Request
|
|
|
+ * Get request
|
|
|
+ * @return \Webman\Http\Request|Request|null
|
|
|
*/
|
|
|
function request()
|
|
|
{
|
|
|
@@ -218,37 +258,49 @@ function request()
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $key
|
|
|
- * @param null $default
|
|
|
- * @return mixed
|
|
|
+ * Get config
|
|
|
+ * @param string|null $key
|
|
|
+ * @param $default
|
|
|
+ * @return array|mixed|null
|
|
|
*/
|
|
|
-function config($key = null, $default = null)
|
|
|
+function config(string $key = null, $default = null)
|
|
|
{
|
|
|
return Config::get($key, $default);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param $name
|
|
|
- * @param array $parameters
|
|
|
+ * Create url
|
|
|
+ * @param string $name
|
|
|
+ * @param ...$parameters
|
|
|
* @return string
|
|
|
*/
|
|
|
-function route($name, $parameters = [])
|
|
|
+function route(string $name, ...$parameters): string
|
|
|
{
|
|
|
$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 null $key
|
|
|
- * @param null $default
|
|
|
+ * Session
|
|
|
+ * @param mixed $key
|
|
|
+ * @param mixed $default
|
|
|
* @return mixed
|
|
|
*/
|
|
|
function session($key = null, $default = null)
|
|
|
{
|
|
|
- $session = request()->session();
|
|
|
+ $session = \request()->session();
|
|
|
if (null === $key) {
|
|
|
return $session;
|
|
|
}
|
|
|
@@ -256,27 +308,40 @@ function session($key = null, $default = null)
|
|
|
$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
|
|
|
+ * Translation
|
|
|
+ * @param 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)
|
|
|
+function trans(string $id, array $parameters = [], string $domain = null, string $locale = null): string
|
|
|
{
|
|
|
$res = Translation::trans($id, $parameters, $domain, $locale);
|
|
|
return $res === '' ? $id : $res;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @param null|string $locale
|
|
|
- * @return string
|
|
|
+ * Locale
|
|
|
+ * @param string|null $locale
|
|
|
+ * @return void
|
|
|
*/
|
|
|
-function locale(string $locale = null)
|
|
|
+function locale(string $locale = null): string
|
|
|
{
|
|
|
if (!$locale) {
|
|
|
return Translation::getLocale();
|
|
|
@@ -286,56 +351,56 @@ function locale(string $locale = null)
|
|
|
|
|
|
/**
|
|
|
* 404 not found
|
|
|
- *
|
|
|
* @return Response
|
|
|
*/
|
|
|
-function not_found()
|
|
|
+function not_found(): Response
|
|
|
{
|
|
|
- return new Response(404, [], file_get_contents(public_path() . '/404.html'));
|
|
|
+ return new Response(404, [], \file_get_contents(public_path() . '/404.html'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Copy dir.
|
|
|
- * @param $source
|
|
|
- * @param $dest
|
|
|
+ * Copy dir
|
|
|
+ * @param string $source
|
|
|
+ * @param string $dest
|
|
|
* @param bool $overwrite
|
|
|
* @return void
|
|
|
*/
|
|
|
-function copy_dir($source, $dest, $overwrite = false)
|
|
|
+function copy_dir(string $source, string $dest, bool $overwrite = false)
|
|
|
{
|
|
|
- if (is_dir($source)) {
|
|
|
+ if (\is_dir($source)) {
|
|
|
if (!is_dir($dest)) {
|
|
|
- mkdir($dest);
|
|
|
+ \mkdir($dest);
|
|
|
}
|
|
|
- $files = scandir($source);
|
|
|
+ $files = \scandir($source);
|
|
|
foreach ($files as $file) {
|
|
|
if ($file !== "." && $file !== "..") {
|
|
|
- copy_dir("$source/$file", "$dest/$file");
|
|
|
+ \copy_dir("$source/$file", "$dest/$file");
|
|
|
}
|
|
|
}
|
|
|
- } else if (file_exists($source) && ($overwrite || !file_exists($dest))) {
|
|
|
- copy($source, $dest);
|
|
|
+ } else if (\file_exists($source) && ($overwrite || !\file_exists($dest))) {
|
|
|
+ \copy($source, $dest);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Remove dir.
|
|
|
- * @param $dir
|
|
|
+ * Remove dir
|
|
|
+ * @param string $dir
|
|
|
* @return bool
|
|
|
*/
|
|
|
-function remove_dir($dir)
|
|
|
+function remove_dir(string $dir): bool
|
|
|
{
|
|
|
- if (is_link($dir) || is_file($dir)) {
|
|
|
- return unlink($dir);
|
|
|
+ if (\is_link($dir) || \is_file($dir)) {
|
|
|
+ return \unlink($dir);
|
|
|
}
|
|
|
- $files = array_diff(scandir($dir), array('.', '..'));
|
|
|
+ $files = \array_diff(\scandir($dir), array('.', '..'));
|
|
|
foreach ($files as $file) {
|
|
|
- (is_dir("$dir/$file") && !is_link($dir)) ? remove_dir("$dir/$file") : unlink("$dir/$file");
|
|
|
+ (\is_dir("$dir/$file") && !\is_link($dir)) ? \remove_dir("$dir/$file") : \unlink("$dir/$file");
|
|
|
}
|
|
|
- return rmdir($dir);
|
|
|
+ return \rmdir($dir);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Bind worker
|
|
|
* @param $worker
|
|
|
* @param $class
|
|
|
*/
|
|
|
@@ -352,16 +417,17 @@ function worker_bind($worker, $class)
|
|
|
'onWebSocketConnect'
|
|
|
];
|
|
|
foreach ($callback_map as $name) {
|
|
|
- if (method_exists($class, $name)) {
|
|
|
+ if (\method_exists($class, $name)) {
|
|
|
$worker->$name = [$class, $name];
|
|
|
}
|
|
|
}
|
|
|
- if (method_exists($class, 'onWorkerStart')) {
|
|
|
- call_user_func([$class, 'onWorkerStart'], $worker);
|
|
|
+ if (\method_exists($class, 'onWorkerStart')) {
|
|
|
+ \call_user_func([$class, 'onWorkerStart'], $worker);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Start worker
|
|
|
* @param $process_name
|
|
|
* @param $config
|
|
|
* @return void
|
|
|
@@ -386,10 +452,10 @@ function worker_start($process_name, $config)
|
|
|
}
|
|
|
|
|
|
$worker->onWorkerStart = function ($worker) use ($config) {
|
|
|
- require_once base_path() . '/support/bootstrap.php';
|
|
|
+ require_once \base_path() . '/support/bootstrap.php';
|
|
|
|
|
|
foreach ($config['services'] ?? [] as $server) {
|
|
|
- if (!class_exists($server['handler'])) {
|
|
|
+ if (!\class_exists($server['handler'])) {
|
|
|
echo "process error: class {$server['handler']} not exists\r\n";
|
|
|
continue;
|
|
|
}
|
|
|
@@ -398,61 +464,62 @@ function worker_start($process_name, $config)
|
|
|
echo "listen: {$server['listen']}\n";
|
|
|
}
|
|
|
$instance = Container::make($server['handler'], $server['constructor'] ?? []);
|
|
|
- worker_bind($listen, $instance);
|
|
|
+ \worker_bind($listen, $instance);
|
|
|
$listen->listen();
|
|
|
}
|
|
|
|
|
|
if (isset($config['handler'])) {
|
|
|
- if (!class_exists($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);
|
|
|
+ \worker_bind($worker, $instance);
|
|
|
}
|
|
|
-
|
|
|
};
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Phar support.
|
|
|
- * Compatible with the 'realpath' function in the phar file.
|
|
|
- *
|
|
|
+ * Get realpath
|
|
|
* @param string $file_path
|
|
|
* @return string
|
|
|
*/
|
|
|
function get_realpath(string $file_path): string
|
|
|
{
|
|
|
- if (strpos($file_path, 'phar://') === 0) {
|
|
|
+ if (\strpos($file_path, 'phar://') === 0) {
|
|
|
return $file_path;
|
|
|
} else {
|
|
|
- return realpath($file_path);
|
|
|
+ return \realpath($file_path);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Is phar
|
|
|
* @return bool
|
|
|
*/
|
|
|
-function is_phar()
|
|
|
+function is_phar(): bool
|
|
|
{
|
|
|
- return class_exists(\Phar::class, false) && Phar::running();
|
|
|
+ return \class_exists(\Phar::class, false) && Phar::running();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * Get cpu count
|
|
|
* @return int
|
|
|
*/
|
|
|
-function cpu_count()
|
|
|
+function cpu_count(): int
|
|
|
{
|
|
|
// Windows does not support the number of processes setting.
|
|
|
if (\DIRECTORY_SEPARATOR === '\\') {
|
|
|
return 1;
|
|
|
}
|
|
|
- if (strtolower(PHP_OS) === 'darwin') {
|
|
|
- $count = shell_exec('sysctl -n machdep.cpu.core_count');
|
|
|
- } else {
|
|
|
- $count = shell_exec('nproc');
|
|
|
+ $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');
|
|
|
+ }
|
|
|
}
|
|
|
- $count = (int)$count > 0 ? (int)$count : 4;
|
|
|
- return $count;
|
|
|
+ return $count > 0 ? $count : 4;
|
|
|
}
|