1+ <?php
2+
3+ namespace HichemTabTech \LaravelFS \Console ;
4+
5+ use Symfony \Component \Console \Command \Command ;
6+ use Symfony \Component \Console \Input \InputArgument ;
7+ use Symfony \Component \Console \Input \InputInterface ;
8+ use Symfony \Component \Console \Input \InputOption ;
9+ use Symfony \Component \Console \Output \OutputInterface ;
10+
11+ use function Laravel \Prompts \confirm ;
12+ use function Laravel \Prompts \error ;
13+ use function Laravel \Prompts \intro ;
14+
15+ class RemoveTemplateCommand extends Command
16+ {
17+ use Concerns \CommandsUtils, Concerns \ConfiguresPrompts, Concerns \CommonTemplateUtils;
18+
19+ protected function configure (): void
20+ {
21+ $ this ->setName ('template:remove ' )
22+ ->setDescription ('remove a saved template ' )
23+ ->setHelp ('This command removes a saved template that you no longer need. ' )
24+ ->addOption ('all ' , 'a ' , InputOption::VALUE_NONE , 'Remove all saved templates ' )
25+ ->addArgument ('template-name ' , InputArgument::REQUIRED , 'The name of the template to remove ' );
26+ }
27+
28+ protected function interact (InputInterface $ input , OutputInterface $ output ): void
29+ {
30+ parent ::interact ($ input , $ output );
31+
32+ $ this ->configurePrompts ($ input , $ output );
33+
34+ $ output ->write (PHP_EOL . ' <fg=blue> _ _
35+ | | | |
36+ | | __ _ _ __ __ ___ _____| |
37+ | | / _` | __/ _` \ \ / / _ \ |
38+ | |___| (_| | | | (_| |\ V / __/ |
39+ |______\__,_|_| \__,_| \_/ \___|_|</> ' . PHP_EOL . PHP_EOL );
40+
41+ if (!$ input ->getArgument ('template-name ' )) {
42+ if (!$ input ->getOption ('all ' )) {
43+ $ this ->ensureTemplateNameArgument ($ input );
44+ } else {
45+ $ input ->setArgument ('template-name ' , confirm (
46+ label: 'Are you sure you want to remove all saved templates? ' ,
47+ default: false ,
48+ hint: 'This action is irreversible. ' ,
49+ ) ? '/all/ ' : null );
50+ }
51+ }
52+ }
53+
54+
55+ protected function execute (InputInterface $ input , OutputInterface $ output ): int
56+ {
57+
58+ $ templatesData = $ this ->getSavedTemplates ();
59+ $ templates = $ templatesData ['templates ' ];
60+ if (empty ($ templates )) {
61+ $ this ->info ('No saved templates found. ' );
62+ return Command::SUCCESS ;
63+ }
64+
65+ if ($ input ->getArgument ('template-name ' ) == '/all/ ' ) {
66+ $ templateToRemove = null ;
67+ } elseif (!$ input ->getArgument ('template-name ' )) {
68+ intro ('Operation cancelled. ' );
69+ return Command::SUCCESS ;
70+ } else {
71+ $ templateName = $ input ->getArgument ('template-name ' );
72+
73+ if (!isset ($ templates [$ templateName ])) {
74+ error ("Template ' $ templateName' not found. " );
75+ return Command::INVALID ;
76+ }
77+
78+ $ templateToRemove = $ templateName ;
79+ }
80+ intro ("Removing a saved template... " );
81+
82+ if ($ templateToRemove ) {
83+ $ done = $ this ->removeTemplates ($ templatesData ['path ' ], $ templatesData , $ templateToRemove );
84+ } else {
85+ $ done = $ this ->removeTemplates ($ templatesData ['path ' ], $ templatesData );
86+ }
87+
88+ if ($ done ) {
89+ $ this ->info ($ templateToRemove ? "Template ' $ templateToRemove' removed successfully. " : 'All saved templates removed successfully. ' );
90+ return Command::SUCCESS ;
91+ }
92+
93+ error ($ templateToRemove ? "Failed to remove template ' $ templateToRemove'. " : 'Failed to remove all saved templates. ' );
94+ return Command::FAILURE ;
95+ }
96+
97+ private function removeTemplates (string $ path , array $ templatesData , string |null $ templateToRemove = null ): bool
98+ {
99+ if (!$ templateToRemove ) {
100+ $ templatesConfig = ['templates ' => []];
101+ } else {
102+ unset($ templatesData ['templates ' ][$ templateToRemove ]);
103+ $ templatesConfig = ['templates ' => $ templatesData ['templates ' ]];
104+ }
105+ return file_put_contents ($ path , json_encode ($ templatesConfig , JSON_PRETTY_PRINT )) !== false ;
106+ }
107+ }
0 commit comments