-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTemplatesListCommand.php
More file actions
74 lines (58 loc) · 2.28 KB
/
Copy pathTemplatesListCommand.php
File metadata and controls
74 lines (58 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace App\Command;
use App\Model\TemplateData;
use App\Service\TemplateService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:templates:list',
description: 'List templates',
)]
class TemplatesListCommand extends Command
{
public function __construct(
private readonly TemplateService $templateService,
) {
parent::__construct();
}
protected function configure(): void
{
$this->addOption('status', 's', InputOption::VALUE_NONE, 'Get status of installed templates.');
}
final protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$status = $input->getOption('status');
try {
$templates = $this->templateService->getCoreTemplates();
if (0 === count($templates)) {
$io->error('No core templates found.');
return Command::INVALID;
}
$customTemplates = $this->templateService->getCustomTemplates();
$allTemplates = array_merge($templates, $customTemplates);
if ($status) {
$numberOfTemplates = count($allTemplates);
$numberOfInstallledTemplates = count(array_filter($allTemplates, fn ($entry): bool => $entry->installed));
$text = $numberOfInstallledTemplates.' / '.$numberOfTemplates.' templates installed.';
$io->success($text);
} else {
$io->table(['ID', 'Title', 'Status', 'Type'], array_map(fn (TemplateData $templateData) => [
$templateData->id,
$templateData->title,
$templateData->installed ? 'Installed' : 'Not Installed',
$templateData->type,
], $allTemplates));
}
return Command::SUCCESS;
} catch (\Exception $e) {
$io->error($e->getMessage());
return Command::INVALID;
}
}
}