-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
executable file
·120 lines (119 loc) · 5.48 KB
/
server
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
#!/usr/bin/env php
<?php
/**
* @author jan huang <bboyjanhuang@gmail.com>
* @copyright 2016
* @link https://www.github.com/janhuang
* @link https://fastdlabs.com
*/
set_time_limit(0);
date_default_timezone_set('PRC');
// detect app root
$root = __detectRoot();
// autoload composer
foreach ([
$root.'/vendor/autoload.php',
__DIR__.'/../../autoload.php',
__DIR__.'/../vendor/autoload.php',
__DIR__.'/vendor/autoload.php',
] as $value) {
if (file_exists($value)) {
define('COMPOSER_INSTALL', $value);
break;
}
}
if (!defined('COMPOSER_INSTALL')) {
fwrite(STDERR, 'You need to set up the project dependencies using the following commands:'.PHP_EOL.'wget http://getcomposer.org/composer.phar'.PHP_EOL.'php composer.phar install'.PHP_EOL);
}
include COMPOSER_INSTALL;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Uniondrug\Server\Application;
use Uniondrug\Server\Server;
$logo = <<<LOGO
__ __ _ ____
/ / / /___ (_)___ ____ / __ \_______ ______ _
/ / / / __ \/ / __ \/ __ \/ / / / ___/ / / / __ `/
/ /_/ / / / / / /_/ / / / / /_/ / / / /_/ / /_/ /
\____/_/ /_/_/\____/_/ /_/_____/_/ \__,_/\__, /
/____/ Server
LOGO;
$output = new ConsoleOutput();
try {
$input = new ArgvInput(null, new InputDefinition([
new InputArgument('action', InputArgument::OPTIONAL, 'The server action', 'status'),
new InputOption('container', 'c', InputOption::VALUE_OPTIONAL, 'Docker container', null),
new InputOption('daemon', 'd', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
new InputOption('ipaddr', 'i', InputOption::VALUE_OPTIONAL, 'Server ip address'),
new InputOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Server port'),
new InputOption('path', 'x', InputOption::VALUE_OPTIONAL, 'Web root relative path', $root),
new InputOption('env', 'e', InputOption::VALUE_OPTIONAL, 'Enviroment', 'development'),
new InputOption('help', 'h', InputOption::VALUE_OPTIONAL, 'Show help', null),
]));
$output->writeln(sprintf("%s <info>%s</info>", $logo, Application::VERSION));
$output->writeln('');
if ($input->hasParameterOption([
'--help',
'-h'
])
) {
$output->writeln("<comment>Usage:</comment>");
$output->writeln(" server [command] [option]");
$output->writeln("");
$output->writeln("<comment>Options:</comment>");
$output->writeln(sprintf(" <info>%s</info> %s %s", "-c, --container", " ", "运行在Docker容器中, 默认: <comment>no</comment>, 接受: <comment>yes</comment>、<comment>off</comment>"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-d, --daemon", " ", "以守护进程运行, 不接受参数"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-e, --env", " ", "环境名称. 默认: <comment>development</comment>, 接受: <comment>development</comment>、<comment>testing</comment>、<comment>release</comment>、<comment>production</comment>"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-i, --ipaddr", " ", "IP地址"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-p, --port", " ", "端口号"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-x, --path", " ", "Web相对路径. 默认: <comment>{$root}</comment>"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "-h, --help", " ", "Show this help"));
$output->writeln("");
$output->writeln("<comment>Available commands:</comment>");
$output->writeln(sprintf(" <info>%s</info> %s %s", "start", " ", "Start the server"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "stop", " ", "Stop the server"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "reload", "", "Reload the server"));
$output->writeln(sprintf(" <info>%s</info> %s %s", "status", "", "Show the server status [<comment>default</comment>]"));
exit(0);
}
$path = $input->getOption('path');
$env = $input->getOption('env');
$docker = $input->getOption('container');
putenv("APP_ENV=$env");
putenv("DOCKER_CONTAINER={$docker}");
$server = new Server(new Application($path), $input);
$server->run($input);
} catch(\Throwable $e) {
$output->writeln('');
$output->writeln(sprintf("<info>Oho, Some error found:</info> <error>%s</error>", $e->getMessage()));
$output->writeln('');
$output->writeln("<info> File: </info>".$e->getFile());
$output->writeln("<info> Line: </info>".$e->getLine());
$output->writeln('');
$output->writeln("<info>Trace: </info>");
$output->writeln('');
$output->write($e->getTraceAsString());
$output->writeln('');
}
function __detectRoot()
{
$cwd = getcwd();
$script = $_SERVER['SCRIPT_FILENAME'];
if (substr($script, 0, 1) != DIRECTORY_SEPARATOR) {
$script = $cwd.DIRECTORY_SEPARATOR.$script;
}
$rootPath = dirname($script);
while (!file_exists($rootPath.DIRECTORY_SEPARATOR.'app')) {
$rootPath = dirname($rootPath);
if ($rootPath == DIRECTORY_SEPARATOR) {
echo PHP_EOL;
echo "Error: Cannot detect app root".PHP_EOL;
echo PHP_EOL;
exit;
}
}
return $rootPath;
}