1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\command; |
3
|
|
|
|
4
|
|
|
use gossi\swagger\Swagger; |
5
|
|
|
use gossi\swagger\Tag; |
6
|
|
|
use keeko\tools\generator\api\ApiGenerator; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class GenerateApiCommand extends AbstractKeekoCommand { |
12
|
|
|
|
13
|
20 |
|
private $needsResourceIdentifier = false; |
|
|
|
|
14
|
20 |
|
private $needsPagedMeta = false; |
|
|
|
|
15
|
20 |
|
|
16
|
20 |
|
protected function configure() { |
17
|
|
|
$this |
18
|
|
|
->setName('generate:api') |
19
|
20 |
|
->setDescription('Generates the api for the module') |
20
|
20 |
|
->addOption( |
21
|
|
|
'model', |
22
|
|
|
'm', |
23
|
|
|
InputOption::VALUE_OPTIONAL, |
24
|
|
|
'The model for which the actions should be generated, when there is no name argument (if ommited all models will be generated)' |
25
|
|
|
) |
26
|
|
|
; |
27
|
|
|
|
28
|
|
|
$this->configureGenerateOptions(); |
29
|
|
|
|
30
|
|
|
parent::configure(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Checks whether api can be generated at all by reading composer.json and verify |
35
|
|
|
* all required information are available |
36
|
|
|
*/ |
37
|
|
|
private function check() { |
38
|
|
|
$module = $this->packageService->getModule(); |
39
|
|
|
if ($module === null) { |
40
|
|
|
throw new \DomainException('No module definition found in composer.json - please run `keeko init`.'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) { |
45
|
|
|
$this->check(); |
46
|
|
|
|
47
|
|
|
$module = $this->package->getKeeko()->getModule(); |
48
|
|
|
$swagger = new Swagger(); |
49
|
|
|
$swagger->setVersion('2.0'); |
50
|
|
|
$swagger->getInfo()->setTitle($module->getTitle() . ' API'); |
51
|
|
|
$swagger->getTags()->add(new Tag(['name' => $module->getSlug()])); |
52
|
|
|
|
53
|
|
|
// generate api from package |
54
|
|
|
$apigen = new ApiGenerator($this->service); |
55
|
|
|
$apigen->generatePaths($swagger); |
56
|
|
|
$apigen->generateDefinitions($swagger); |
57
|
|
|
|
58
|
|
|
// add custom entries from generator.json |
59
|
|
|
$custom = new Swagger($this->project->getGeneratorDefinition()->getApi()); |
|
|
|
|
60
|
|
|
$swagger->getPaths()->addAll($custom->getPaths()); |
61
|
|
|
$swagger->getDefinitions()->setAll($swagger->getDefinitions()); |
62
|
|
|
|
63
|
|
|
// dump to file |
64
|
|
|
$filename = $this->project->getApiFileName(); |
65
|
|
|
$this->jsonService->write($filename, $swagger->toArray()); |
66
|
|
|
$this->io->writeln(sprintf('API for <info>%s</info> written at <info>%s</info>', $this->package->getFullName(), $filename)); |
67
|
|
|
} |
68
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.