Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit 9f29f67

Browse files
committed
refactor(challenge): Reduce Twig render time
Make ColumnsOfAnswer deferred.
1 parent 53d7f77 commit 9f29f67

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Twig\Components\Challenge;
6+
7+
use App\Entity\Question;
8+
use App\Service\QuestionDbRunnerService;
9+
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
10+
use Symfony\UX\LiveComponent\Attribute\LiveProp;
11+
use Symfony\UX\LiveComponent\DefaultActionTrait;
12+
13+
#[AsLiveComponent]
14+
final class ColumnsOfAnswer
15+
{
16+
use DefaultActionTrait;
17+
18+
public function __construct(
19+
private readonly QuestionDbRunnerService $questionDbRunnerService,
20+
) {
21+
}
22+
23+
#[LiveProp]
24+
public Question $question;
25+
26+
/**
27+
* Get the columns of the answer.
28+
*
29+
* @return string[] the columns of the answer
30+
*/
31+
public function getColumnsOfAnswer(): array
32+
{
33+
try {
34+
$answer = $this->questionDbRunnerService->getAnswerResult($this->question);
35+
$answerResult = $answer->getResult();
36+
37+
if (0 === \count($answerResult)) {
38+
return [];
39+
}
40+
41+
return $answer->getResult()[0];
42+
} catch (\Throwable $e) {
43+
return ["⚠️ Invalid Question: {$e->getMessage()}"];
44+
}
45+
}
46+
}

src/Twig/Components/Challenge/Description.php

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,10 @@
55
namespace App\Twig\Components\Challenge;
66

77
use App\Entity\Question;
8-
use App\Service\QuestionDbRunnerService;
98
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
109

1110
#[AsTwigComponent]
1211
final class Description
1312
{
14-
public function __construct(
15-
private readonly QuestionDbRunnerService $questionDbRunnerService,
16-
) {
17-
}
18-
1913
public Question $question;
20-
21-
/**
22-
* Get the columns of the answer.
23-
*
24-
* @return string[] the columns of the answer
25-
*/
26-
public function getColumnsOfAnswer(): array
27-
{
28-
try {
29-
$answer = $this->questionDbRunnerService->getAnswerResult($this->question);
30-
$answerResult = $answer->getResult();
31-
32-
if (0 === \count($answerResult)) {
33-
return [];
34-
}
35-
36-
return $answer->getResult()[0];
37-
} catch (\Throwable $e) {
38-
return ["⚠️ Invalid Question: {$e->getMessage()}"];
39-
}
40-
}
4114
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<p {{ attributes }}>
2+
<strong>輸出格式:</strong>欄位順序分別為:<code>{{
3+
this.columnsOfAnswer|joinToQuoted('')
4+
}}</code>。
5+
</p>

templates/components/Challenge/Description.html.twig

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
{{ question.description|raw }}
44
</div>
55

6-
<p>
7-
<strong>輸出格式:</strong>欄位順序分別為:<code>{{
8-
this.columnsOfAnswer|joinToQuoted('')
9-
}}</code>。
10-
</p>
6+
<twig:Challenge:ColumnsOfAnswer :question="question" loading="defer" />
117

128
{% if question.schema %}
139
<p><strong>Schema ({{ question.schema.id }}): </strong>

0 commit comments

Comments
 (0)