'react', // 默认格式为 react ]; public static function name(string $templateName): self { $instance = new self(); $instance->templateName = $templateName; return $instance; } public function param(array $params): self { $this->params = $params; return $this; } public function options(array $options): self { $this->options = $options; return $this; } public function render(): array { // 解析模板类 $this->resolveTemplate(); // 设置参数并渲染 return $this->template ->setParams($this->params) ->setOptions($this->options) ->render(); } protected function resolveTemplate(): void { // 模板名称到类的映射(可以用配置文件替代) $templateMap = [ 'cf' => \App\Services\Templates\ConfidenceTemplate::class, 'nissaya' => \App\Services\Templates\NissayaTemplate::class, 'term' => \App\Services\Templates\TermTemplate::class, 'note' => \App\Services\Templates\NoteTemplate::class, ]; if (!isset($templateMap[$this->templateName])) { throw new InvalidArgumentException("Template {$this->templateName} not found."); } // 通过服务容器解析模板类 $this->template = app($templateMap[$this->templateName]); } }