Skip to content

Commit 910616b

Browse files
committed
adding api generator
1 parent edf8a2c commit 910616b

File tree

4 files changed

+152
-2
lines changed

4 files changed

+152
-2
lines changed

src/Command/ApiEntityCommand.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

src/Command/ControllerCommand.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
3434
$srcFolderNamespace = $io->ask('Enter the base namespace for src/ : ', 'Bone');
3535
$controllerNamespace = $io->ask('Enter the controller namespace: ', 'App\\Controller');
3636
$controllerName = $io->ask('Enter the controller name: ', 'TestController');
37-
$features = [];
3837
$keepAddingFeatures = true;
3938
$chosenFeatures = [];
4039
$features = [...ControllerGeneratorService::FEATURES, 'continue'];

src/GeneratorPackage.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Barnacle\Container;
66
use Barnacle\RegistrationInterface;
77
use Bone\Console\CommandRegistrationInterface;
8+
use Bone\Generator\Command\ApiEntityCommand;
89
use Bone\Generator\Command\ControllerCommand;
10+
use Bone\Generator\Service\ApiGeneratorService;
911
use Bone\Generator\Service\ControllerGeneratorService;
1012

1113
class GeneratorPackage implements RegistrationInterface, CommandRegistrationInterface
@@ -16,8 +18,12 @@ public function registerConsoleCommands(Container $container): array
1618
$controller = new ControllerCommand($controllerGeneratorService);
1719
$controller->setName('generate:controller');
1820

21+
$apiGeneratorService = new ApiGeneratorService();
22+
$api = new ApiEntityCommand($apiGeneratorService);
23+
1924
return [
20-
$controller
25+
$controller,
26+
$api,
2127
];
2228
}
2329

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bone\Generator\Service;
6+
7+
use Bone\Generator\Exception\GeneratorException;
8+
use function file_exists;
9+
10+
class ApiGeneratorService
11+
{
12+
public function generateApi(array $data): void
13+
{
14+
$entityName = $data['entity'];
15+
$fields = $data['fields'];
16+
}
17+
18+
private function ensureNoFileExists(string $fileName): void
19+
{
20+
if (file_exists($fileName)) {
21+
throw new GeneratorException(GeneratorException::FILE_EXISTS);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)