Files
kiri-core/function.php
T

172 lines
2.8 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
defined('APP_PATH') or define('APP_PATH', __DIR__ . '/../../');
use HttpServer\Http\Response;
use Snowflake\Snowflake;
2020-08-31 01:39:28 +08:00
use HttpServer\Http\Context;
2020-08-31 01:27:08 +08:00
if (!function_exists('make')) {
/**
* @param $name
* @param $default
* @return stdClass
* @throws
*/
function make($name, $default)
{
if (Snowflake::has($name)) {
$class = Snowflake::get()->$name;
} else if (Snowflake::has($default)) {
$class = Snowflake::get()->$default;
} else {
$class = Snowflake::createObject($default);
Snowflake::setAlias($name, $default);
}
return $class;
}
}
if (!function_exists('storage')) {
/**
* @param string $fileName
* @param string $path
* @return string
* @throws Exception
*/
function storage($fileName = '', $path = '')
{
$basePath = Snowflake::getStoragePath();
2020-08-31 01:54:20 +08:00
if (empty($path)) {
2020-08-31 11:01:46 +08:00
$fileName = $basePath . '/' . $fileName;
2020-08-31 01:54:20 +08:00
} else if (empty($fileName)) {
2020-08-31 11:01:46 +08:00
$fileName = initDir($basePath, $path);
} else {
$fileName = initDir($basePath, $path) . '/' . $fileName;
}
if (!file_exists($fileName)) {
touch($fileName);
2020-08-31 01:54:20 +08:00
}
2020-08-31 11:01:46 +08:00
return $fileName;
2020-08-31 01:27:08 +08:00
}
/**
* @param $basePath
* @param $path
* @return false|string
* @throws Exception
*/
function initDir($basePath, $path)
{
$explode = array_filter(explode('/', $path));
2020-08-31 11:01:46 +08:00
$_path = '/' . trim($basePath, '/') . '/';
2020-08-31 01:27:08 +08:00
foreach ($explode as $value) {
2020-08-31 11:01:46 +08:00
$_path .= $value . '/';
if (!is_dir(rtrim($_path, '/'))) {
mkdir(rtrim($_path, '/'));
2020-08-31 01:27:08 +08:00
}
2020-08-31 11:01:46 +08:00
if (!is_dir($_path)) {
throw new Exception('System error, directory ' . $_path . ' is not writable');
2020-08-31 01:27:08 +08:00
}
}
2020-08-31 11:01:46 +08:00
return realpath($_path);
2020-08-31 01:27:08 +08:00
}
}
if (!function_exists('alias')) {
/**
* @param $class
* @param $name
*/
function alias($class, $name)
{
Snowflake::setAlias($class, $name);
}
}
if (!function_exists('name')) {
2020-08-31 01:39:28 +08:00
/**
* @param string $name
*/
2020-08-31 01:27:08 +08:00
function name($name)
{
swoole_set_process_name($name);
}
}
if (!function_exists('response')) {
/**
* @return Response|stdClass
* @throws
*/
function response()
{
2020-08-31 01:39:28 +08:00
if (!Context::hasContext('response')) {
2020-08-31 01:27:08 +08:00
return make('response', Response::class);
}
2020-08-31 01:39:28 +08:00
return Context::getContext('response');
2020-08-31 01:27:08 +08:00
}
}
if (!function_exists('redirect')) {
function redirect($url)
{
return response()->redirect($url);
}
}
if (!function_exists('env')) {
/**
* @param $key
* @param null $default
* @return array|false|string|null
*/
function env($key, $default = null)
{
$env = getenv($key);
if ($env === false) {
return $default;
}
return $env;
}
}
2020-08-31 01:59:30 +08:00
if (!function_exists('sweep')) {
/**
* @param string $configPath
* @return array|false|string|null
*/
function sweep($configPath = APP_PATH . '/config')
{
$array = [];
foreach (glob($configPath . '/*') as $config) {
$array = array_merge(require_once "$config", $array);
}
return $array;
}
}