|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bone\Generator\Command; |
| 4 | + |
| 5 | +use Bone\Generator\Service\ApiGeneratorService; |
| 6 | +use Bone\Generator\Service\ControllerGeneratorService; |
| 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\Style\SymfonyStyle; |
| 11 | + |
| 12 | +use function array_search; |
| 13 | +use function count; |
| 14 | +use function explode; |
| 15 | +use function file_put_contents; |
| 16 | +use function implode; |
| 17 | +use function strtolower; |
| 18 | + |
| 19 | +class ApiEntityCommand extends Command |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private ApiGeneratorService $apiGeneratorService |
| 23 | + ){ |
| 24 | + parent::__construct('api-entity'); |
| 25 | + } |
| 26 | + |
| 27 | + protected function configure() |
| 28 | + { |
| 29 | + $this->setDescription('Generate an entity with REST controller and service'); |
| 30 | + $this->setHelp('Generate an entity with REST controller and service'); |
| 31 | + } |
| 32 | + |
| 33 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 34 | + { |
| 35 | + $io = new SymfonyStyle($input, $output); |
| 36 | + $io->title('Bone Framework API Entity Generator'); |
| 37 | + $load = $input->hasOption('load') ? $input->getOption('load') : false; |
| 38 | + |
| 39 | + if ($load) { |
| 40 | + $json = file_get_contents($load . '.json'); |
| 41 | + $data = json_decode($json, true); |
| 42 | + $this->apiGeneratorService->generateApi($data); |
| 43 | + $io->success('Generated API. Have a nice day!'); |
| 44 | + } else { |
| 45 | + $entityName = $io->ask('Enter the entity name: '); |
| 46 | + $fields = []; |
| 47 | + |
| 48 | + do { |
| 49 | + $fieldName = $io->ask('Enter a field name'); |
| 50 | + $io->comment('Available types (integer, numeric, float, decimal, date, datetime, boolean, string, json)'); |
| 51 | + |
| 52 | + $type = $io->ask('Enter a type', 'string'); |
| 53 | + $decimal = null; |
| 54 | + |
| 55 | + if ($type === 'decimal' || $type === 'float') { |
| 56 | + $decimal = $io->ask('Enter length', '11,2'); |
| 57 | + } |
| 58 | + |
| 59 | + $isRequired = $io->confirm('Is this field required?', true); |
| 60 | + $required = $isRequired ? 'required|' : ''; |
| 61 | + |
| 62 | + switch ($type) { |
| 63 | + case 'integer': |
| 64 | + $rule = $required . 'integer'; |
| 65 | + break; |
| 66 | + case 'float': |
| 67 | + case 'numeric': |
| 68 | + $rule = $required . 'numeric'; |
| 69 | + break; |
| 70 | + case 'decimal': |
| 71 | + $rule = $required . 'decimal:' . $decimal; |
| 72 | + break; |
| 73 | + case 'date': |
| 74 | + $rule = $required . 'date_format:Y-m-d'; |
| 75 | + break; |
| 76 | + case 'datetime': |
| 77 | + $rule = $required . 'date'; |
| 78 | + break; |
| 79 | + case 'boolean': |
| 80 | + $rule = $required . 'boolean'; |
| 81 | + break; |
| 82 | + case 'json': |
| 83 | + $rule = $required . 'json'; |
| 84 | + break; |
| 85 | + case 'string': |
| 86 | + default: |
| 87 | + $rule = $required . 'string|max:255'; |
| 88 | + break; |
| 89 | + } |
| 90 | + $validation = $io->ask('Enter validation rules.', $rule); |
| 91 | + $isSearchable = $io->confirm('Is this field searchable on the index page?'); |
| 92 | + $default = $io->ask('Enter a default value, if any: '); |
| 93 | + |
| 94 | + $fields[] = [ |
| 95 | + 'name' => $fieldName, |
| 96 | + 'type' => $type, |
| 97 | + 'required' => $isRequired, |
| 98 | + 'searchable' => $isSearchable, |
| 99 | + 'validation' => $validation, |
| 100 | + 'decimal' => $decimal, |
| 101 | + 'default' => $default, |
| 102 | + ]; |
| 103 | + |
| 104 | + $continue = $io->confirm('Add another field?'); |
| 105 | + } while ($continue === true); |
| 106 | + $data = [ |
| 107 | + 'entity' => $entityName, |
| 108 | + 'fields' => $fields, |
| 109 | + ]; |
| 110 | + $json = json_encode($data); |
| 111 | + $path = strtolower($entityName) . '.json'; |
| 112 | + file_put_contents($path, $json); |
| 113 | + $io->success([ |
| 114 | + 'Config saved to ' . $path, |
| 115 | + 'Run `bone api-entity --load=' . strtolower($entityName) . '` to generate.' |
| 116 | + ]); |
| 117 | + } |
| 118 | + |
| 119 | + return Command::SUCCESS; |
| 120 | + } |
| 121 | +} |
0 commit comments