/** * The main() */ public static function main($base_dir, $task = self::TASK_API) { // Throw exceptions on all errors set_error_handler(function ($errno, $errstr, $errfile, $errline) { if (error_reporting()) { throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); } }); try { $config = static::loadConfig($base_dir); $smalldb = static::createSmalldb($config); // TODO: Replace switch with something nice switch ($task) { case self::TASK_INIT: // Initialization complete, let the rest of the script do its job. return [$config, $smalldb]; case self::TASK_API: $handler = static::createHandler($config, $smalldb); $router = static::createRouter($config, $handler); JsonResponse::sendData($router->handle($_SERVER, $_GET, $_POST)); break; case self::TASK_DIAGRAM: static::renderStateMachine($smalldb, $_GET['machine'], $_GET['format']); break; case self::TASK_SELFCHECK: JsonResponse::sendData(static::performSelfCheck($smalldb)); break; default: throw new \InvalidArgumentException('Unknown task'); } } catch (\Exception $ex) { error_log($ex); JsonResponse::sendException($ex); } }