|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cable8mm\Xeed\Command; |
| 4 | + |
| 5 | +use Cable8mm\Xeed\Support\Path; |
| 6 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | +use Symfony\Component\Console\Question\ChoiceQuestion; |
| 11 | + |
| 12 | +/** |
| 13 | + * Clean generated files, seeders, models, factories and migration files. |
| 14 | + * |
| 15 | + * run `bin/console clean` |
| 16 | + */ |
| 17 | +#[AsCommand( |
| 18 | + name: 'clean', |
| 19 | + description: 'Clean generated files, seeders, models, factories and migration files. run `bin/console clean`', |
| 20 | + hidden: false |
| 21 | +)] |
| 22 | +class CleanCommand extends Command |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Run the console command. |
| 26 | + */ |
| 27 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 28 | + { |
| 29 | + $helper = $this->getHelper('question'); |
| 30 | + |
| 31 | + $question = new ChoiceQuestion( |
| 32 | + 'Please select directory for you to want to clean.', |
| 33 | + ['seeder', 'model', 'factory', 'migration', 'all', 'exit'], |
| 34 | + 'exit' |
| 35 | + ); |
| 36 | + |
| 37 | + $question->setErrorMessage('Type %s is invalid.'); |
| 38 | + |
| 39 | + /** @disregard P1013 */ |
| 40 | + $thing = $helper->ask($input, $output, $question); |
| 41 | + |
| 42 | + $output->writeln('You have just selected: '.$thing); |
| 43 | + |
| 44 | + $path = match ($thing) { |
| 45 | + 'seeder' => [Path::seeder()], |
| 46 | + 'model' => [Path::model()], |
| 47 | + 'factory' => [Path::factory()], |
| 48 | + 'migration' => [Path::migration()], |
| 49 | + 'all' => [ |
| 50 | + Path::seeder(), |
| 51 | + Path::model(), |
| 52 | + Path::factory(), |
| 53 | + Path::migration(), |
| 54 | + ], |
| 55 | + default => null, |
| 56 | + }; |
| 57 | + |
| 58 | + if ($path === null) { |
| 59 | + $output->writeln('See you later!'); |
| 60 | + |
| 61 | + return Command::SUCCESS; |
| 62 | + } |
| 63 | + |
| 64 | + foreach ($path as $path) { |
| 65 | + array_map('unlink', array_filter((array) glob($path.'*.php'))); |
| 66 | + $output->writeln($path.' was cleaned. Enjoy it.'); |
| 67 | + } |
| 68 | + |
| 69 | + return Command::SUCCESS; |
| 70 | + } |
| 71 | +} |
0 commit comments