mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-07 16:55:38 +08:00
initial framework commit
This commit is contained in:
43
src/globals/defines.php
Normal file
43
src/globals/defines.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
// 工作目录
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
|
||||
define('WORKING_DIR', getcwd());
|
||||
const ROOT_DIR = __DIR__ . '/../..';
|
||||
|
||||
// 程序启动时间
|
||||
define('START_TIME', microtime(true));
|
||||
|
||||
// 规定目录
|
||||
define('BUILD_ROOT_PATH', is_string($a = getenv('BUILD_ROOT_PATH')) ? $a : (WORKING_DIR . '/buildroot'));
|
||||
define('SOURCE_PATH', is_string($a = getenv('SOURCE_PATH')) ? $a : (WORKING_DIR . '/source'));
|
||||
define('DOWNLOAD_PATH', is_string($a = getenv('DOWNLOAD_PATH')) ? $a : (WORKING_DIR . '/downloads'));
|
||||
define('BUILD_LIB_PATH', is_string($a = getenv('INSTALL_LIB_PATH')) ? $a : (BUILD_ROOT_PATH . '/lib'));
|
||||
const BUILD_DEPS_PATH = BUILD_ROOT_PATH;
|
||||
define('BUILD_INCLUDE_PATH', is_string($a = getenv('INSTALL_INCLUDE_PATH')) ? $a : (BUILD_ROOT_PATH . '/include'));
|
||||
define('SEPARATED_PATH', [
|
||||
'/' . pathinfo(BUILD_LIB_PATH)['basename'], // lib
|
||||
'/' . pathinfo(BUILD_INCLUDE_PATH)['basename'], // include
|
||||
BUILD_ROOT_PATH,
|
||||
]);
|
||||
|
||||
// 危险的命令额外用 notice 级别提醒
|
||||
const DANGER_CMD = [
|
||||
'rm',
|
||||
'rmdir',
|
||||
];
|
||||
|
||||
// 替换方案
|
||||
const REPLACE_FILE_STR = 1;
|
||||
const REPLACE_FILE_PREG = 2;
|
||||
const REPLACE_FILE_USER = 3;
|
||||
|
||||
const BUILD_MICRO_NONE = 0;
|
||||
const BUILD_MICRO_ONLY = 1;
|
||||
|
||||
const BUILD_MICRO_BOTH = 2;
|
||||
|
||||
ConsoleLogger::$date_format = 'H:i:s';
|
||||
97
src/globals/functions.php
Normal file
97
src/globals/functions.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ZM\Logger\ConsoleLogger;
|
||||
|
||||
/**
|
||||
* 判断传入的数组是否为关联数组
|
||||
* @param mixed $array
|
||||
*/
|
||||
function is_assoc_array($array): bool
|
||||
{
|
||||
return is_array($array) && (!empty($array) && array_keys($array) !== range(0, count($array) - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 助手方法,返回一个 Logger 实例
|
||||
*/
|
||||
function logger(): LoggerInterface
|
||||
{
|
||||
global $ob_logger;
|
||||
if ($ob_logger === null) {
|
||||
return new ConsoleLogger();
|
||||
}
|
||||
return $ob_logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \SPC\exception\RuntimeException
|
||||
*/
|
||||
function arch2gnu(string $arch): string
|
||||
{
|
||||
$arch = strtolower($arch);
|
||||
return match ($arch) {
|
||||
'x86_64', 'x64', 'amd64' => 'x86_64',
|
||||
'arm64', 'aarch64' => 'aarch64',
|
||||
default => throw new \SPC\exception\RuntimeException('Not support arch: ' . $arch),
|
||||
// 'armv7' => 'arm',
|
||||
};
|
||||
}
|
||||
|
||||
function quote(string $str, string $quote = '"'): string
|
||||
{
|
||||
return $quote . $str . $quote;
|
||||
}
|
||||
|
||||
function osfamily2dir(): string
|
||||
{
|
||||
return match (PHP_OS_FAMILY) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
'Windows', 'WINNT', 'Cygwin' => 'windows',
|
||||
'Darwin' => 'macos',
|
||||
'Linux' => 'linux',
|
||||
default => throw new \SPC\exception\RuntimeException('Not support os: ' . PHP_OS_FAMILY),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \SPC\exception\RuntimeException
|
||||
*/
|
||||
function f_passthru(string $cmd): ?bool
|
||||
{
|
||||
$danger = false;
|
||||
foreach (DANGER_CMD as $danger_cmd) {
|
||||
if (str_starts_with($cmd, $danger_cmd . ' ')) {
|
||||
$danger = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($danger) {
|
||||
logger()->notice('Running dangerous command: ' . $cmd);
|
||||
} else {
|
||||
logger()->debug('Running command with direct output: ' . $cmd);
|
||||
}
|
||||
$ret = passthru($cmd, $code);
|
||||
if ($code !== 0) {
|
||||
throw new \SPC\exception\RuntimeException('Command run failed with code[' . $code . ']: ' . $cmd, $code);
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
function f_exec(string $command, &$output, &$result_code)
|
||||
{
|
||||
logger()->debug('Running command (no output) : ' . $command);
|
||||
return exec($command, $output, $result_code);
|
||||
}
|
||||
|
||||
function f_mkdir(string $directory, int $permissions = 0777, bool $recursive = false): bool
|
||||
{
|
||||
if (file_exists($directory)) {
|
||||
logger()->debug("Dir {$directory} already exists, ignored");
|
||||
return true;
|
||||
}
|
||||
logger()->debug('Making new directory ' . ($recursive ? 'recursive' : '') . ': ' . $directory);
|
||||
return mkdir($directory, $permissions, $recursive);
|
||||
}
|
||||
Reference in New Issue
Block a user