forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControllersGenerator.php
159 lines (146 loc) · 6.01 KB
/
ControllersGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <[email protected]> and contributors
* @license https://github.com/cebe/yii2-openapi/blob/master/LICENSE
*/
namespace cebe\yii2openapi\lib\generators;
use cebe\yii2openapi\lib\CodeFiles;
use cebe\yii2openapi\lib\Config;
use cebe\yii2openapi\lib\items\FractalAction;
use cebe\yii2openapi\lib\items\RestAction;
use Laminas\Code\Generator\AbstractMemberGenerator;
use Laminas\Code\Generator\ClassGenerator;
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\ParameterGenerator;
use Laminas\Code\Generator\ValueGenerator;
use Yii;
use yii\gii\CodeFile;
use yii\helpers\ArrayHelper;
use yii\helpers\Inflector;
class ControllersGenerator
{
/**
* @var \cebe\yii2openapi\lib\Config
*/
protected $config;
/**
* @var array|\cebe\yii2openapi\lib\items\RestAction[]|\cebe\yii2openapi\lib\items\FractalAction[]
*/
protected $controllers;
protected $files;
public function __construct(Config $config, array $actions = [])
{
$this->config = $config;
$this->controllers = ArrayHelper::index($actions, null, 'controllerId');
$this->files = new CodeFiles([]);
}
/**
* @throws \yii\base\InvalidConfigException
*/
public function generate():CodeFiles
{
if (!$this->config->generateControllers) {
return new CodeFiles([]);
}
$namespace = $this->config->controllerNamespace ?? Yii::$app->controllerNamespace;
$path = $this->config->getPathFromNamespace($namespace);
$templateName = $this->config->useJsonApi ? 'controller_jsonapi.php' : 'controller.php';
foreach ($this->controllers as $controller => $actions) {
$controllerNamespace = $namespace;
$controllerPath = $path;
/**
* @var RestAction|FractalAction $action
**/
$action = $actions[0];
if ($action->prefix && !empty($action->prefixSettings)) {
$controllerNamespace = trim($action->prefixSettings['namespace'], '\\');
$controllerPath = $action->prefixSettings['path']
?? $this->config->getPathFromNamespace($controllerNamespace);
}
$className = Inflector::id2camel($controller) . 'Controller';
$this->files->add(new CodeFile(
Yii::getAlias($controllerPath . "/base/$className.php"),
$this->config->render(
$templateName,
[
'className' => $className,
'namespace' => $controllerNamespace . '\\base',
'actions' => $actions,
]
)
));
// only generate custom classes if they do not exist, do not override
if (!file_exists(Yii::getAlias("$controllerPath/$className.php"))) {
$classFileGenerator = $this->makeCustomController($className, $controllerNamespace, $actions);
$this->files->add(new CodeFile(
Yii::getAlias("$controllerPath/$className.php"),
$classFileGenerator->generate()
));
}
}
return $this->files;
}
/**
* @param string $className
* @param string $controllerNamespace
* @param RestAction[]|FractalAction[] $actions
* @return FileGenerator
*/
protected function makeCustomController(
string $className,
string $controllerNamespace,
array $actions
):FileGenerator {
$classFileGenerator = new FileGenerator();
$reflection = new ClassGenerator(
$className,
$controllerNamespace,
null,
$controllerNamespace . '\\base\\' . $className
);
/**@var FractalAction[]|RestAction[] $abstractActions * */
$abstractActions = array_filter($actions, static function ($action) {
return $action->shouldBeAbstract();
});
if ($this->config->useJsonApi) {
$body = <<<'PHP'
$actions = parent::actions();
return $actions;
PHP;
$reflection->addMethod('actions', [], AbstractMemberGenerator::FLAG_PUBLIC, $body);
}
$params = [
new ParameterGenerator('action'),
new ParameterGenerator('model', null, new ValueGenerator(null)),
new ParameterGenerator('params', null, new ValueGenerator([])),
];
$reflection->addMethod('checkAccess', $params, AbstractMemberGenerator::FLAG_PUBLIC, '//TODO implement checkAccess');
foreach ($abstractActions as $action) {
$responseHttpStatusCodes = '';
foreach ($this->config->getOpenApi()->paths->getPaths()[$action->urlPath]->getOperations() as $verb => $operation) {
$codes = array_keys($operation->responses->getResponses());
$only200OrDefault = false;
if ($codes === [200] || $codes === ['default']) {
$only200OrDefault = true;
}
if (in_array('default', $codes) && in_array(200, $codes) && count($codes) === 2) {
$only200OrDefault = true;
}
if ($verb === strtolower($action->requestMethod) && !$only200OrDefault) {
$responseHttpStatusCodes = implode(', ', $codes);
}
}
$params = array_map(static function ($param) {
return ['name' => $param];
}, $action->getParamNames());
$reflection->addMethod(
$action->actionMethodName,
$params,
AbstractMemberGenerator::FLAG_PUBLIC,
'//TODO implement ' . $action->actionMethodName . ($responseHttpStatusCodes ? PHP_EOL . '// In order to conform with OpenAPI spec, response of this action must have one of the following HTTP status code: ' . $responseHttpStatusCodes : '')
);
}
$classFileGenerator->setClasses([$reflection]);
return $classFileGenerator;
}
}