RU
apps/caramail-server / src / Main.php · commit 070afda
<?phpnamespace App;use App\Command\DemoCommand;use App\Command\RoomsCommand;use App\Command\ServeCommand;use App\Command\StatusCommand;use App\Command\TokenCommand;use Symfony\Component\Console\Application;use Symfony\Component\Console\Input\ArgvInput;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Output\OutputInterface;/** * The chat server's program: serve (the daemon), status, rooms, token, demo. boot() is what app.php and the * exported executable call. The data folder (rooms.json, status.json): CARAMAIL_DATA, else JUMPHP_VAR, else var/. */final class Main{    public readonly Application $console;    public function __construct(public readonly string $data)    {        $this->console = new Application(self::title(), self::version());        $this->console->addCommands([            new ServeCommand($data),            new StatusCommand($data),            new RoomsCommand($data),            new TokenCommand(),            new DemoCommand(),        ]);        $this->console->setDefaultCommand('list');    }    /** @param string $root the app's folder (app.php's, or where the executable unpacked it) */    public static function boot(string $root): self    {        $data = getenv('CARAMAIL_DATA') ?: (getenv('JUMPHP_VAR') ?: $root . '/var');        if (defined('JUMPHP_CWD')) {            chdir((string)constant('JUMPHP_CWD'));        }        return new self($data);    }    public function run(?InputInterface $input = null, ?OutputInterface $output = null): int    {        $this->console->setAutoExit(false);        return $this->console->run($input ?? new ArgvInput(), $output);    }    public static function title(): string    {        return self::config()['title'] ?? 'CaraChat Serveur';    }    public static function version(): string    {        return (string)(self::config()['version'] ?? '0.1.0');    }    /** @return array<string, mixed> config/project.yaml's id, title and version (read without a YAML parser) */    private static function config(): array    {        $out = [];        $yaml = (string)@file_get_contents(dirname(__DIR__) . '/config/project.yaml');        foreach (['id', 'title', 'version'] as $key) {            if (preg_match('/^\s+' . $key . ':\s*"?([^"#\n]*?)"?\s*(?:#.*)?$/m', $yaml, $m)) {                $out[$key] = trim($m[1]);            }        }        return $out;    }}