TemplateRegistry.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Services\Template;
  3. // ================== 模板注册表 ==================
  4. class TemplateRegistry
  5. {
  6. private array $templates = [];
  7. public function __construct()
  8. {
  9. $this->loadDefaultTemplates();
  10. }
  11. private function loadDefaultTemplates(): void
  12. {
  13. $this->templates = [
  14. 'note' => [
  15. 'defaultParams' => ['text', 'type'],
  16. 'paramMapping' => [
  17. '0' => 'text',
  18. '1' => 'type'
  19. ],
  20. 'defaultValues' => [
  21. 'type' => 'info'
  22. ],
  23. 'validation' => [
  24. 'required' => ['text'],
  25. 'optional' => ['type', 'title']
  26. ]
  27. ],
  28. 'info' => [
  29. 'defaultParams' => ['content'],
  30. 'paramMapping' => [
  31. '0' => 'content'
  32. ],
  33. 'defaultValues' => [],
  34. 'validation' => [
  35. 'required' => ['content'],
  36. 'optional' => ['title']
  37. ]
  38. ],
  39. 'warning' => [
  40. 'defaultParams' => ['message'],
  41. 'paramMapping' => [
  42. '0' => 'message'
  43. ],
  44. 'defaultValues' => [],
  45. 'validation' => [
  46. 'required' => ['message'],
  47. 'optional' => ['title']
  48. ]
  49. ]
  50. ];
  51. }
  52. public function registerTemplate(string $name, array $config): void
  53. {
  54. $this->templates[$name] = $config;
  55. }
  56. public function getTemplate(string $name): ?array
  57. {
  58. return $this->templates[$name] ?? null;
  59. }
  60. public function hasTemplate(string $name): bool
  61. {
  62. return isset($this->templates[$name]);
  63. }
  64. public function getParamMapping(string $templateName): array
  65. {
  66. return $this->templates[$templateName]['paramMapping'] ?? [];
  67. }
  68. public function getDefaultValues(string $templateName): array
  69. {
  70. return $this->templates[$templateName]['defaultValues'] ?? [];
  71. }
  72. }