Skip to content

Commit 65eac05

Browse files
committed
Add sorting in command categories for command scheduling
1 parent 3856e8b commit 65eac05

1 file changed

Lines changed: 23 additions & 9 deletions

File tree

src/Controller/CommandController.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use Doctrine\ORM\OptimisticLockException;
99
use Symfony\Bundle\FrameworkBundle\Console\Application;
1010
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11+
use Symfony\Component\Console\Command\Command;
1112
use Symfony\Component\HttpFoundation\Request;
1213
use Symfony\Component\HttpFoundation\Response;
1314
use Symfony\Component\HttpKernel\KernelInterface;
15+
use Symfony\Component\Messenger\Exception\ExceptionInterface;
1416
use Symfony\Component\Routing\Attribute\Route;
1517
use Symfony\Component\Security\Http\Attribute\IsGranted;
1618
use Symfony\Contracts\Translation\TranslatorInterface;
@@ -39,6 +41,9 @@ public function schedule(KernelInterface $kernel, Request $request, ?JobRecurrin
3941
]);
4042
}
4143

44+
/**
45+
* @throws ExceptionInterface
46+
*/
4247
#[Route(path: '/schedule-init', name: 'command_schedule_init')]
4348
public function scheduleInit(Request $request, CommandJobFactory $commandJobFactory, TranslatorInterface $translator): Response
4449
{
@@ -48,7 +53,7 @@ public function scheduleInit(Request $request, CommandJobFactory $commandJobFact
4853
$commandName = $commandScheduleRequest['command'] ?? null;
4954
$runMethod = $commandScheduleRequest['method'] ?? null;
5055
if (empty($commandName) || empty($runMethod)) {
51-
// Redirect back to the command schedule if somehow missing some of required values
56+
// Redirect back to the command schedule if somehow missing some of the required values
5257
$this->addFlash('danger', $translator->trans('command.schedule.job.error.name'));
5358
return $this->redirectToRoute('command_schedule', ['listId' => $listId, 'listName' => $listName]);
5459
}
@@ -143,28 +148,37 @@ public function scheduleInit(Request $request, CommandJobFactory $commandJobFact
143148
private function getApplicationCommands(KernelInterface $kernel): array
144149
{
145150
$application = new Application($kernel);
146-
$commands = [];
151+
$commandCategories = [];
147152
foreach ($application->all() as $command) {
148153
if (str_starts_with($command->getName(), '_')) {
149-
// Unset internal commands
154+
// Skip internal commands
150155
continue;
151156
}
157+
152158
if (str_contains($command->getName(), ':')) {
153-
// Make command groups by first command part (app:|make: etc.)
154-
$key = explode(':', $command->getName())[0];
155-
$commands[$key][] = $command;
159+
// Add command in categories by the first command part (app:|make: etc.)
160+
$categoryKey = explode(':', $command->getName())[0];
161+
$commandCategories[$categoryKey][] = $command;
156162
} else {
157-
$commands['uncategorized'][] = $command;
163+
$commandCategories['uncategorized'][] = $command;
158164
}
159165
}
160166

161167
// Ensure uncategorized commands are last
162-
uksort($commands, function ($a, $b) {
168+
uksort($commandCategories, function (string $a, string $b) {
163169
if ($a === 'uncategorized') return 1;
164170
if ($b === 'uncategorized') return -1;
165171
return $a <=> $b;
166172
});
167173

168-
return $commands;
174+
// Sort commands by name in individual categories
175+
foreach ($commandCategories as $category => $commands) {
176+
usort($commands, function (Command $a, Command $b) {
177+
return $a->getName() <=> $b->getName();
178+
});
179+
$commandCategories[$category] = $commands;
180+
}
181+
182+
return $commandCategories;
169183
}
170184
}

0 commit comments

Comments
 (0)