Skip to content

Commit 1e1534e

Browse files
Use attribute to render templates, simplifying controller code
1 parent 41b6821 commit 1e1534e

33 files changed

+1139
-354
lines changed

webapp/src/Controller/Jury/AnalysisController.php

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Doctrine\ORM\EntityManagerInterface;
1212
use Doctrine\ORM\Query\Expr;
1313
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
14+
use Symfony\Bridge\Twig\Attribute\Template;
1415
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
@@ -27,11 +28,24 @@ public function __construct(
2728
private readonly EntityManagerInterface $em
2829
) {}
2930

31+
/**
32+
* @return array{error: string}|array{
33+
* contest: mixed,
34+
* problems: mixed,
35+
* teams: mixed,
36+
* submissions: mixed,
37+
* delayed_judgings: array{data: mixed, overflow: int, delay: int},
38+
* misc: mixed,
39+
* filters: array<string, string>,
40+
* view: string
41+
* }|Response
42+
*/
43+
#[Template(template: 'jury/analysis/contest_overview.html.twig')]
3044
#[Route(path: '', name: 'analysis_index')]
3145
public function indexAction(
3246
#[MapQueryParameter]
3347
?string $view = null
34-
): Response {
48+
): array|Response {
3549
$em = $this->em;
3650
$contest = $this->dj->getCurrentContest();
3751

@@ -66,7 +80,7 @@ public function indexAction(
6680
->orderBy('timediff', 'DESC')
6781
->getQuery()->getResult();
6882

69-
return $this->render('jury/analysis/contest_overview.html.twig', [
83+
return [
7084
'contest' => $contest,
7185
'problems' => $problems,
7286
'teams' => $teams,
@@ -79,11 +93,15 @@ public function indexAction(
7993
'misc' => $misc,
8094
'filters' => StatisticsService::FILTERS,
8195
'view' => $view,
82-
]);
96+
];
8397
}
8498

99+
/**
100+
* @return array<string, mixed>|Response
101+
*/
102+
#[Template(template: 'jury/analysis/team.html.twig')]
85103
#[Route(path: '/team/{team}', name: 'analysis_team')]
86-
public function teamAction(Team $team): Response
104+
public function teamAction(Team $team): array|Response
87105
{
88106
$contest = $this->dj->getCurrentContest();
89107

@@ -93,18 +111,20 @@ public function teamAction(Team $team): Response
93111
]);
94112
}
95113

96-
return $this->render('jury/analysis/team.html.twig',
97-
$this->stats->getTeamStats($contest, $team)
98-
);
114+
return $this->stats->getTeamStats($contest, $team);
99115
}
100116

117+
/**
118+
* @return array<string, mixed>|Response
119+
*/
120+
#[Template(template: 'jury/analysis/problem.html.twig')]
101121
#[Route(path: '/problem/{probid}', name: 'analysis_problem')]
102122
public function problemAction(
103123
#[MapEntity(id: 'probid')]
104124
Problem $problem,
105125
#[MapQueryParameter]
106126
?string $view = null
107-
): Response {
127+
): array|Response {
108128
$contest = $this->dj->getCurrentContest();
109129

110130
if ($contest === null) {
@@ -116,16 +136,18 @@ public function problemAction(
116136
$filterKeys = array_keys(StatisticsService::FILTERS);
117137
$view = $view ?: reset($filterKeys);
118138

119-
return $this->render('jury/analysis/problem.html.twig',
120-
$this->stats->getProblemStats($contest, $problem, $view)
121-
);
139+
return $this->stats->getProblemStats($contest, $problem, $view);
122140
}
123141

142+
/**
143+
* @return array<string, mixed>|Response
144+
*/
145+
#[Template(template: 'jury/analysis/languages.html.twig')]
124146
#[Route(path: '/languages', name: 'analysis_languages')]
125147
public function languagesAction(
126148
#[MapQueryParameter]
127149
?string $view = null
128-
): Response {
150+
): array|Response {
129151
$contest = $this->dj->getCurrentContest();
130152

131153
if ($contest === null) {
@@ -137,8 +159,6 @@ public function languagesAction(
137159
$filterKeys = array_keys(StatisticsService::FILTERS);
138160
$view = $view ?: reset($filterKeys);
139161

140-
return $this->render('jury/analysis/languages.html.twig',
141-
$this->stats->getLanguagesStats($contest, $view)
142-
);
162+
return $this->stats->getLanguagesStats($contest, $view);
143163
}
144164
}

webapp/src/Controller/Jury/AuditLogController.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use App\Utils\Utils;
1212
use Doctrine\ORM\EntityManagerInterface;
1313
use Doctrine\ORM\Tools\Pagination\Paginator;
14+
use Symfony\Bridge\Twig\Attribute\Template;
1415
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15-
use Symfony\Component\HttpFoundation\Response;
1616
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
1717
use Symfony\Component\Routing\Attribute\Route;
1818
use Symfony\Component\Security\Http\Attribute\IsGranted;
@@ -28,13 +28,24 @@ public function __construct(
2828
protected readonly EventLogService $eventLogService
2929
) {}
3030

31+
/**
32+
* @return array{
33+
* auditlog: list<array{data: array<string, mixed>, actions: list<mixed>}>,
34+
* table_fields: array<string, array{title: string, sort: bool}>,
35+
* table_options: array{ordering: string, searching: string, full_clickable: bool},
36+
* maxPages: int,
37+
* thisPage: int,
38+
* showAll: bool
39+
* }
40+
*/
3141
#[Route(path: '', name: 'jury_auditlog')]
42+
#[Template(template: 'jury/auditlog.html.twig')]
3243
public function indexAction(
3344
#[MapQueryParameter]
3445
bool $showAll = false,
3546
#[MapQueryParameter]
3647
int $page = 1,
37-
): Response {
48+
): array {
3849
$timeFormat = (string)$this->config->get('time_format');
3950

4051
$limit = 1000;
@@ -96,17 +107,17 @@ public function indexAction(
96107
'what' => ['title' => 'action', 'sort' => false],
97108
];
98109

99-
$maxPages = ceil($paginator->count() / $limit);
110+
$maxPages = (int)ceil($paginator->count() / $limit);
100111
$thisPage = $page;
101112

102-
return $this->render('jury/auditlog.html.twig', [
113+
return [
103114
'auditlog' => $auditlog_table,
104115
'table_fields' => $table_fields,
105116
'table_options' => ['ordering' => 'false', 'searching' => 'false', 'full_clickable' => false],
106117
'maxPages' => $maxPages,
107118
'thisPage' => $thisPage,
108119
'showAll' => $showAll,
109-
]);
120+
];
110121
}
111122

112123
private function generateDatatypeUrl(string $type, int|string|null $id): ?string

webapp/src/Controller/Jury/BalloonController.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use App\Service\EventLogService;
1212
use App\Utils\Utils;
1313
use Doctrine\ORM\EntityManagerInterface;
14+
use Symfony\Bridge\Twig\Attribute\Template;
1415
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1516
use Symfony\Component\ExpressionLanguage\Expression;
1617
use Symfony\Component\HttpFoundation\RedirectResponse;
17-
use Symfony\Component\HttpFoundation\Response;
1818
use Symfony\Component\Routing\Attribute\Route;
1919
use Symfony\Component\Security\Http\Attribute\IsGranted;
2020

@@ -49,12 +49,26 @@ private function areDefault(array $filters, array $defaultCategories): bool {
4949
return false;
5050
}
5151

52+
/**
53+
* @return array{}|array{
54+
* refresh: array{after: int, url: string, ajax: bool},
55+
* isfrozen: bool,
56+
* hasFilters: bool,
57+
* filteredAffiliations: list<TeamAffiliation>,
58+
* filteredLocations: list<Team>,
59+
* filteredCategories: list<TeamCategory>,
60+
* availableCategories: list<TeamCategory>,
61+
* defaultCategories: list<int>,
62+
* balloons: list<mixed>
63+
* }
64+
*/
5265
#[Route(path: '', name: 'jury_balloons')]
53-
public function indexAction(BalloonService $balloonService): Response
66+
#[Template(template: 'jury/balloons.html.twig')]
67+
public function indexAction(BalloonService $balloonService): array
5468
{
5569
$contest = $this->dj->getCurrentContest();
5670
if (is_null($contest)) {
57-
return $this->render('jury/balloons.html.twig');
71+
return [];
5872
}
5973

6074
$balloons_table = $balloonService->collectBalloonTable($contest);
@@ -151,7 +165,7 @@ public function indexAction(BalloonService $balloonService): Response
151165
->getArrayResult();
152166
$defaultCategories = array_column($defaultCategories, "categoryid");
153167

154-
return $this->render('jury/balloons.html.twig', [
168+
return [
155169
'refresh' => [
156170
'after' => 60,
157171
'url' => $this->generateUrl('jury_balloons'),
@@ -165,7 +179,7 @@ public function indexAction(BalloonService $balloonService): Response
165179
'availableCategories' => $availableCategories,
166180
'defaultCategories' => $defaultCategories,
167181
'balloons' => $balloons_table
168-
]);
182+
];
169183
}
170184

171185
#[Route(path: '/{balloonId}/done', name: 'jury_balloons_setdone')]

webapp/src/Controller/Jury/ClarificationController.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
use App\Utils\Utils;
1515
use Doctrine\ORM\EntityManagerInterface;
1616
use Doctrine\ORM\Query\Expr\Join;
17+
use Symfony\Bridge\Twig\Attribute\Template;
1718
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1819
use Symfony\Component\Form\FormInterface;
20+
use Symfony\Component\Form\FormView;
21+
use Symfony\Component\HttpFoundation\RedirectResponse;
1922
use Symfony\Component\HttpFoundation\Request;
2023
use Symfony\Component\HttpFoundation\Response;
2124
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
@@ -34,13 +37,25 @@ public function __construct(
3437
protected readonly EventLogService $eventLogService
3538
) {}
3639

40+
/**
41+
* @return array{
42+
* newClarifications: list<Clarification>,
43+
* oldClarifications: list<Clarification>,
44+
* generalClarifications: list<Clarification>,
45+
* queues: array<string, string>,
46+
* currentQueue: string,
47+
* currentFilter: string|null,
48+
* categories: array<string, string>
49+
* }
50+
*/
3751
#[Route(path: '', name: 'jury_clarifications')]
52+
#[Template(template: 'jury/clarifications.html.twig')]
3853
public function indexAction(
3954
#[MapQueryParameter(name: 'filter')]
4055
?string $currentFilter = null,
4156
#[MapQueryParameter(name: 'queue')]
4257
string $currentQueue = 'all',
43-
): Response {
58+
): array {
4459
$categories = $this->config->get('clar_categories');
4560
if ($contest = $this->dj->getCurrentContest()) {
4661
$contestIds = [$contest->getCid()];
@@ -102,19 +117,27 @@ public function indexAction(
102117

103118
$queues = $this->config->get('clar_queues');
104119

105-
return $this->render('jury/clarifications.html.twig', [
120+
return [
106121
'newClarifications' => $newClarifications,
107122
'oldClarifications' => $oldClarifications,
108123
'generalClarifications' => $generalClarifications,
109124
'queues' => $queues,
110125
'currentQueue' => $currentQueue,
111126
'currentFilter' => $currentFilter,
112127
'categories' => $categories,
113-
]);
128+
];
114129
}
115130

131+
/**
132+
* @return array{
133+
* list: list<array<string, mixed>>,
134+
* queues: array<string, string>,
135+
* answers: list<string>, jurymember: string|null
136+
* }|RedirectResponse
137+
*/
116138
#[Route(path: '/{id<\d+>}', name: 'jury_clarification')]
117-
public function viewAction(Request $request, int $id): Response
139+
#[Template(template: 'jury/clarification.html.twig')]
140+
public function viewAction(Request $request, int $id): array|RedirectResponse
118141
{
119142
$clarification = $this->em->getRepository(Clarification::class)->find($id);
120143
if (!$clarification) {
@@ -230,15 +253,19 @@ public function viewAction(Request $request, int $id): Response
230253
->getQuery()
231254
->getSingleResult()['jury_member'];
232255

233-
return $this->render('jury/clarification.html.twig', $parameters);
256+
return $parameters;
234257
}
235258

259+
/**
260+
* @return array{form: FormView}|RedirectResponse
261+
*/
236262
#[Route(path: '/send', name: 'jury_clarification_new')]
263+
#[Template(template: 'jury/clarification_new.html.twig')]
237264
public function composeClarificationAction(
238265
Request $request,
239266
#[MapQueryParameter]
240267
?string $teamto = null,
241-
): Response {
268+
): array|RedirectResponse {
242269
$formData = ['recipient' => JuryClarificationType::RECIPIENT_MUST_SELECT];
243270

244271
if ($teamto !== null) {
@@ -253,7 +280,7 @@ public function composeClarificationAction(
253280
return $this->processSubmittedClarification($form);
254281
}
255282

256-
return $this->render('jury/clarification_new.html.twig', ['form' => $form->createView()]);
283+
return ['form' => $form->createView()];
257284
}
258285

259286
#[Route(path: '/{clarId<\d+>}/claim', name: 'jury_clarification_claim')]
@@ -354,7 +381,7 @@ public function changeQueueAction(Request $request, int $clarId): Response
354381
protected function processSubmittedClarification(
355382
FormInterface $form,
356383
?Clarification $inReplTo = null
357-
): Response {
384+
): RedirectResponse {
358385
$formData = $form->getData();
359386
$clarification = new Clarification();
360387
$clarification->setInReplyTo($inReplTo);

0 commit comments

Comments
 (0)