diff --git a/server b/server index 245c54e..79cc0ec 100755 --- a/server +++ b/server @@ -3,40 +3,29 @@ /** * @author jan huang * @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) { + $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 - ); + 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; @@ -54,27 +43,33 @@ $logo = <<writeln(sprintf("%s %s", $logo, Application::VERSION)); $output->writeln(''); - if ($input->hasParameterOption(['--help', '-h'])) { + if ($input->hasParameterOption([ + '--help', + '-h' + ]) + ) { $output->writeln("Usage:"); $output->writeln(" server [command] [option]"); $output->writeln(""); $output->writeln("Options:"); - $output->writeln(sprintf(" %s %s %s", "-d, --daemon", " ", "Run server as daemon, Do not ask any interactive question")); - $output->writeln(sprintf(" %s %s %s", "-t, --path", " ", "Web root relative path. Default: " . $root . "")); - $output->writeln(sprintf(" %s %s %s", "-e, --env", " ", "Environment. Default: development")); - $output->writeln(sprintf(" %s %s %s", "-h, --help", " ", "Show this help")); + $output->writeln(sprintf(" %s %s %s", "-c, --container", " ", "运行在Docker容器中, 默认: no, 接受: yesoff")); + $output->writeln(sprintf(" %s %s %s", "-d, --daemon", " ", "以守护进程运行, 不接受参数")); + $output->writeln(sprintf(" %s %s %s", "-e, --env", " ", "环境名称. 默认: development, 接受: developmenttestingreleaseproduction")); + $output->writeln(sprintf(" %s %s %s", "-i, --ipaddr", " ", "Docker容器的IP地址")); + $output->writeln(sprintf(" %s %s %s", "-p, --path", " ", "Web相对路径. 默认: {$root}")); + $output->writeln(sprintf(" %s %s %s", "-h, --help", " ", "Show this help")); $output->writeln(""); $output->writeln("Available commands:"); $output->writeln(sprintf(" %s %s %s", "start", " ", "Start the server")); @@ -83,42 +78,41 @@ try { $output->writeln(sprintf(" %s %s %s", "status", "", "Show the server status [default]")); 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)); $server->run($input); -} catch (\Throwable $e) { +} catch(\Throwable $e) { $output->writeln(''); $output->writeln(sprintf("Oho, Some error found: %s", $e->getMessage())); $output->writeln(''); - $output->writeln(" File: " . $e->getFile()); - $output->writeln(" Line: " . $e->getLine()); + $output->writeln(" File: ".$e->getFile()); + $output->writeln(" Line: ".$e->getLine()); $output->writeln(''); $output->writeln("Trace: "); $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; + $script = $cwd.DIRECTORY_SEPARATOR.$script; } $rootPath = dirname($script); - while (!file_exists($rootPath . DIRECTORY_SEPARATOR . 'app')) { + 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 "Error: Cannot detect app root".PHP_EOL; echo PHP_EOL; exit; } } return $rootPath; -} \ No newline at end of file +}