forked from luolongfei/freenom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
121 lines (103 loc) · 2.61 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env php
<?php
/**
* 入口文件
*
* 云函数版本维护:
* 1、去掉顶部的 “#!/usr/bin/env php”,将文件名改为 index.php
* 2、将 “define('IS_SCF', false);” 改为 “define('IS_SCF', true);”
* 3、干掉最下方的 run(); 调用
*
* @author mybsdc <mybsdc@gmail.com>
* @date 2019/3/2
* @time 11:05
* @link https://github.com/luolongfei/freenom
*/
error_reporting(E_ERROR);
ini_set('display_errors', 1);
set_time_limit(0);
define('IS_SCF', false); // 是否云函数环境
define('IS_CLI', PHP_SAPI === 'cli');
define('DS', DIRECTORY_SEPARATOR);
define('ROOT_PATH', realpath(__DIR__));
define('VENDOR_PATH', realpath(ROOT_PATH . '/vendor'));
define('APP_PATH', realpath(ROOT_PATH . '/app'));
define('DATA_PATH', IS_SCF ? '/tmp' : realpath(ROOT_PATH . '/app/Data')); // 云函数只有 /tmp 目录的读写权限
define('RESOURCES_PATH', realpath(ROOT_PATH . '/resources'));
date_default_timezone_set('Asia/Shanghai');
/**
* 注册错误处理
*/
register_shutdown_function('customize_error_handler');
/**
* 注册异常处理
*/
set_exception_handler('exception_handler');
require VENDOR_PATH . '/autoload.php';
use Luolongfei\Libs\Log;
use Luolongfei\Libs\Message;
/**
* @throws Exception
*/
function customize_error_handler()
{
if (!is_null($error = error_get_last())) {
system_log(json_encode($error, JSON_UNESCAPED_UNICODE));
Log::error(lang('100057'), $error);
Message::send(lang('100058') . json_encode($error, JSON_UNESCAPED_UNICODE), lang('100059'));
}
}
/**
* @param \Exception $e
*
* @throws \Exception
*/
function exception_handler($e)
{
Log::error(lang('100060') . $e->getMessage());
Message::send(lang('100061') . $e->getMessage(), lang('100062'));
}
/**
* 腾讯云函数
*
* @param $event
* @param $context
*
* @return string
*/
function main_handler($event, $context)
{
return run();
}
/**
* 阿里云函数
*
* @param $event
* @param $context
*
* @return string
*/
function handler($event, $context)
{
$logger = $GLOBALS['fcLogger'];
$logger->info(lang('100063'));
return run();
}
/**
* @return string|bool
*/
function run()
{
try {
system_check();
$class = sprintf('Luolongfei\App\Console\%s', get_argv('c', 'FreeNom'));
$fn = get_argv('m', 'handle');
$class::getInstance()->$fn();
return IS_SCF ? lang('100007') : true;
} catch (\Exception $e) {
system_log(sprintf(lang('100006'), $e->getMessage()), $e->getTrace());
Message::send(lang('100004') . $e->getMessage(), lang('100005'));
}
return IS_SCF ? lang('100008') : false;
}
run();