|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Console; |
|
4
|
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
|
6
|
|
|
use BrainExe\Core\Application\SerializedRouteCollection; |
|
7
|
|
|
|
|
8
|
|
|
use BrainExe\Core\Traits\ConfigTrait; |
|
9
|
|
|
use Generator; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
use BrainExe\Core\Annotations\Command as CommandAnnotation; |
|
15
|
|
|
use Symfony\Component\Routing\CompiledRoute; |
|
16
|
|
|
use Symfony\Component\Routing\Route; |
|
17
|
|
|
use Symfony\Component\Yaml\Dumper; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @CommandAnnotation |
|
21
|
|
|
* @codeCoverageIgnore |
|
22
|
|
|
*/ |
|
23
|
|
|
class SwaggerDumpCommand extends Command |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
use ConfigTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var SerializedRouteCollection |
|
30
|
|
|
*/ |
|
31
|
|
|
private $routes; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function configure() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->setName('swagger:dump') |
|
39
|
|
|
->setDescription('Dump swagger config'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @Inject("@Core.RouteCollection") |
|
44
|
|
|
* @param SerializedRouteCollection $rebuild |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(SerializedRouteCollection $rebuild) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->routes = $rebuild; |
|
49
|
|
|
|
|
50
|
|
|
parent::__construct(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
57
|
|
|
{ |
|
58
|
|
|
$applicationName = $this->getParameter('application.name'); |
|
59
|
|
|
|
|
60
|
|
|
$routes = $this->routes->all(); |
|
61
|
|
|
|
|
62
|
|
|
$dumper = new Dumper(); |
|
63
|
|
|
|
|
64
|
|
|
$resources = $this->getResources($routes); |
|
65
|
|
|
|
|
66
|
|
|
$url = parse_url($this->getParameter('application.url')); |
|
67
|
|
|
|
|
68
|
|
|
$formatted = [ |
|
69
|
|
|
'swagger' => '2.0', |
|
70
|
|
|
'info' => [ |
|
71
|
|
|
'title' => $applicationName, |
|
72
|
|
|
'description' => sprintf('%s API', $applicationName), |
|
73
|
|
|
'version' => '1.0.0' |
|
74
|
|
|
], |
|
75
|
|
|
'consumes' => ['application/json'], |
|
76
|
|
|
'produces' => ['application/json', 'text/html'], |
|
77
|
|
|
'host' => $url['host'], |
|
78
|
|
|
'schemes' => [$url['scheme']], |
|
79
|
|
|
'securityDefinitions' => [ |
|
80
|
|
|
'token' => [ |
|
81
|
|
|
'type' => 'apiKey', |
|
82
|
|
|
'description' => 'Given API Token', |
|
83
|
|
|
'name' => 'Token', |
|
84
|
|
|
'in' => 'Header' |
|
85
|
|
|
] |
|
86
|
|
|
], |
|
87
|
|
|
'security' => [ |
|
88
|
|
|
'token' => [ |
|
89
|
|
|
'all' |
|
90
|
|
|
] |
|
91
|
|
|
], |
|
92
|
|
|
'paths' => $resources, |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
echo $dumper->dump($formatted, 4); |
|
96
|
|
|
echo PHP_EOL; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Route $route |
|
101
|
|
|
* @return Generator |
|
102
|
|
|
*/ |
|
103
|
|
|
private function getParameters(Route $route) |
|
104
|
|
|
{ |
|
105
|
|
|
/** @var CompiledRoute $compiled */ |
|
106
|
|
|
$compiled = $route->compile(); |
|
107
|
|
|
|
|
108
|
|
|
foreach ($compiled->getVariables() as $name) { |
|
109
|
|
|
yield [ |
|
110
|
|
|
'name' => $name, |
|
111
|
|
|
'type' => 'string', |
|
112
|
|
|
'required' => true, |
|
113
|
|
|
'in' => 'path' |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param array $routes |
|
120
|
|
|
* @return array |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getResources(array $routes) : array |
|
123
|
|
|
{ |
|
124
|
|
|
$resources = []; |
|
125
|
|
|
foreach ($routes as $name => $route) { |
|
126
|
|
|
$parameters = iterator_to_array($this->getParameters($route)); |
|
127
|
|
|
|
|
128
|
|
|
$data = [ |
|
129
|
|
|
'summary' => $name, |
|
130
|
|
|
'responses' => [ |
|
131
|
|
|
200 => [ |
|
132
|
|
|
'description' => 'OK' |
|
133
|
|
|
] |
|
134
|
|
|
] |
|
135
|
|
|
]; |
|
136
|
|
|
|
|
137
|
|
|
if ($parameters) { |
|
|
|
|
|
|
138
|
|
|
$data['parameters'] = $parameters; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$resources[$route->getPath()][strtolower(implode(',', $route->getMethods()) ?: 'get')] = $data; |
|
142
|
|
|
} |
|
143
|
|
|
return $resources; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.