1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Generates swagger.json |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\SwaggerBundle\Command; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generates swagger.json |
14
|
|
|
* |
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
17
|
|
|
* @link http://swisscom.ch |
18
|
|
|
*/ |
19
|
|
|
class SwaggerGenerateCommand extends Command |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* container |
24
|
|
|
* |
25
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
26
|
|
|
*/ |
27
|
|
|
private $container; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* root dir |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $rootDir; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* filesystem |
38
|
|
|
* |
39
|
|
|
* @var \Symfony\Component\Filesystem\Filesystem |
40
|
|
|
*/ |
41
|
|
|
private $filesystem; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* apidoc service |
45
|
|
|
* |
46
|
|
|
* @var \Graviton\SwaggerBundle\Service\Swagger |
47
|
|
|
*/ |
48
|
|
|
private $apidoc; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritDoc} |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
4 |
|
protected function configure() |
56
|
|
|
{ |
57
|
4 |
|
parent::configure(); |
58
|
|
|
|
59
|
4 |
|
$this->setName('graviton:swagger:generate') |
60
|
4 |
|
->setDescription( |
61
|
2 |
|
'Generates swagger.json in web dir' |
62
|
2 |
|
); |
63
|
4 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* set container |
67
|
|
|
* |
68
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container service_container |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
4 |
|
public function setContainer($container) |
73
|
|
|
{ |
74
|
4 |
|
$this->container = $container; |
75
|
4 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* sets the root dir |
79
|
|
|
* |
80
|
|
|
* @param string $rootDir root dir |
81
|
|
|
* |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
4 |
|
public function setRootDir($rootDir) |
85
|
|
|
{ |
86
|
4 |
|
$this->rootDir = $rootDir; |
87
|
4 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* set filesystem |
91
|
|
|
* |
92
|
|
|
* @param mixed $filesystem filesystem |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
4 |
|
public function setFilesystem($filesystem) |
97
|
|
|
{ |
98
|
4 |
|
$this->filesystem = $filesystem; |
99
|
4 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* sets apidoc |
103
|
|
|
* |
104
|
|
|
* @param mixed $apidoc apidoc |
105
|
|
|
* |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
4 |
|
public function setApidoc($apidoc) |
109
|
|
|
{ |
110
|
4 |
|
$this->apidoc = $apidoc; |
111
|
4 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
* |
116
|
|
|
* @param InputInterface $input input |
117
|
|
|
* @param OutputInterface $output output |
118
|
|
|
* |
119
|
|
|
* @return void |
120
|
|
|
*/ |
121
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
122
|
|
|
{ |
123
|
|
|
$baseDir = $this->rootDir . '/../app/cache/'; |
124
|
|
|
$swaggerFile = $baseDir . 'swagger.json'; |
125
|
|
|
$swaggerHashFile = $baseDir . 'swagger.json.hash'; |
126
|
|
|
|
127
|
|
|
$hash = $this->container->getParameter('graviton.generator.hash.all'); |
128
|
|
|
$doGenerate = !($this->filesystem->exists($swaggerFile) && $this->filesystem->exists($swaggerHashFile)); |
129
|
|
|
|
130
|
|
|
// really don't generate? -> compare hash |
131
|
|
|
if (!$doGenerate) { |
132
|
|
|
$currentHash = file_get_contents($swaggerHashFile); |
133
|
|
|
if ($hash != $currentHash) { |
134
|
|
|
$doGenerate = true; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($doGenerate) { |
139
|
|
|
$this->filesystem->dumpFile( |
140
|
|
|
$swaggerFile, |
141
|
|
|
json_encode($this->apidoc->getSwaggerSpec()) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$this->filesystem->dumpFile( |
145
|
|
|
$swaggerHashFile, |
146
|
|
|
$hash |
147
|
|
|
); |
148
|
|
|
} else { |
149
|
|
|
echo 'swagger.json already up to date, no need to generate...'.PHP_EOL; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|