GrammarTemplate.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Services\Markup\Templates;
  3. use App\Services\ChannelService;
  4. use App\Services\Markup\Ir\Call;
  5. use App\Services\Markup\Render\RenderContext;
  6. use App\Services\Markup\Resolve\Resolver;
  7. use App\Services\Markup\Resolve\TermLoader;
  8. /**
  9. * 语法术语查询:`{{g|word}}`,在系统语法术语 channel(`_System_Grammar_Term_{lang}_`,缺省 en)里查。
  10. */
  11. final class GrammarTemplate extends Template
  12. {
  13. protected string $component = 'grammar';
  14. public function __construct(private readonly ChannelService $channelService) {}
  15. public function params(): array
  16. {
  17. return ['word' => ParamSpec::at(1)];
  18. }
  19. public function collect(Call $call, Resolver $resolver): void
  20. {
  21. $lang = $resolver->channels()->get($call->scope->firstChannel())?->lang ?: $resolver->options->lang;
  22. $channel = $this->channelService->systemUid(
  23. '_System_Grammar_Term_'.strtolower((string) $lang).'_',
  24. '_System_Grammar_Term_en_',
  25. );
  26. $call->data['term'] = $resolver->loader(TermLoader::class)->request(
  27. (string) $call->param('word'),
  28. '',
  29. $channel === false ? null : $channel,
  30. $call->scope,
  31. );
  32. }
  33. /**
  34. * @return array<string, mixed>
  35. */
  36. public function props(Call $call, RenderContext $ctx): array
  37. {
  38. $word = (string) $call->param('word');
  39. return ['word' => $word, 'term' => TermTemplate::termProps($ctx, $call->data['term'] ?? null, $word)];
  40. }
  41. public function text(Call $call, RenderContext $ctx): string
  42. {
  43. return (string) $call->param('word');
  44. }
  45. }