app = $app; $this->class = $class; // 计算指定输出格式 $output = $app->request->request('output', 'default'); $method = $app->request->method() ?: ($app->runningInConsole() ? 'cli' : 'nil'); $this->output = strtolower("{$method}.{$output}"); } /** * 实例对象反射 * @param array $args * @return static */ public static function instance(...$args): Helper { return Container::getInstance()->invokeClass(static::class, $args); } /** * 获取数据库查询对象 * @param BaseQuery|Model|string $query * @return Query|Mongo|BaseQuery */ public static function buildQuery($query) { if (is_string($query)) { return static::buildModel($query)->db(); } if ($query instanceof Model) return $query->db(); if ($query instanceof BaseQuery && !$query->getModel()) { $name = $query->getConfig('name') ?: ''; if (is_string($name) && strlen($name) > 0) { $name = config("database.connections.{$name}") ? $name : ''; } $query->model(static::buildModel($query->getName(), [], $name)); } return $query; } /** * 动态创建模型对象 * @param mixed $name 模型名称 * @param array $data 初始数据 * @param mixed $conn 指定连接 * @return Model */ public static function buildModel(string $name, array $data = [], string $conn = ''): Model { if (strpos($name, '\\') !== false) { if (class_exists($name)) { $model = new $name($data); if ($model instanceof Model) return $model; } $name = basename(str_replace('\\', '/', $name)); } return VirtualModel::mk($name, $data, $conn); } }