forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfig.php
214 lines (182 loc) · 6.7 KB
/
Config.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?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;
use cebe\openapi\Reader;
use cebe\openapi\spec\OpenApi;
use Closure;
use Yii;
use yii\base\BaseObject;
use yii\base\InvalidConfigException;
use yii\helpers\StringHelper;
use function call_user_func;
class Config extends BaseObject
{
/**
* @var string path to the OpenAPI specification file. This can be an absolute path or a Yii path alias.
*/
public $openApiPath;
/**
* @var bool whether to generate URL rules for Yii UrlManager from the API spec.
*/
public $generateUrls = true;
/**
* @var string file name for URL rules.
*/
public $urlConfigFile = '@app/config/urls.rest.php';
/**
* @var array Special url prefixes
* @example
* 'urlPrefixes' => [
* //Prefix will be ignored in url pattern,
* //Rule like ['/calendar/<controller>/<action>' => '<controller>/<action>']
* 'calendar' => '',
* //Controller for url with this prefix will be located directly at defined path and namespace
* //Rule like ['/api/v1/<controller>/<action>' => '/api/v1/<controller>/<action>']
* 'api/v1/' => ['path' => '@app/modules/api/controllers/v1/', 'namespace' => '\app\modules\api\v1'],
* //Controller for url with this prefix will be located directly at defined namespace, path resolved by namespace
* //Rule like ['/prefix/<controller>/<action>' => '/xxx/<controller>/<action>']
* 'prefix' => ['module' => 'xxx','namespace' => '\app\modules\xxx\controllers']
* ]
**/
public $urlPrefixes = [];
/**
* @var bool whether to generate Controllers from the spec.
*/
public $generateControllers = true;
/**
* @var bool use actions that return responses by JsonApi spec instead of default yii rest
*/
public $useJsonApi = false;
/**
* @var bool if true, transformers will be generate in base subdirectory and overridable classes will extend it
*/
public $extendableTransformers = true;
/**
* @var bool if true singular resource keys will be used /post/{id}, plural by default
*/
public $singularResourceKeys = false;
/**
* @var string namespace to create controllers in. This must be resolvable via Yii alias.
* Defaults to `null` which means to use the application controller namespace: `Yii::$app->controllerNamespace`.
*/
public $controllerNamespace;
/**
* @var bool whether to generate ActiveRecord models from the spec.
*/
public $generateModels = true;
/**
* @var bool whether to generate Faker for generating dummy data for each model.
*/
public $generateModelFaker = true;
/**
* @var bool namespace to create models in. This must be resolvable via Yii alias.
* Defaults to `app\models`.
*/
public $modelNamespace = 'app\\models';
/**
* @var bool namespace to create fake data generators in. This must be resolvable via Yii alias.
* Defaults to `app\models`.
*/
public $fakerNamespace = 'app\\models';
/**
* @var string namespace to create fractal transformers in. (Only when generatedControllers and useJsonApi checked)
* Defaults to `app\transformers`.
*/
public $transformerNamespace = 'app\\transformers';
/**
* @var array List of model names to exclude.
*/
public $excludeModels = [];
/**
* @var array Map for custom dbModels
*
* @see DbModel::$scenarioDefaultDescription with acceptedInputs: {scenarioName}, {scenarioConst}, {modelName}.
* @example
* 'dbModel' => [
* 'scenarioDefaultDescription' => "Scenario {scenarioName}",
* ]
*/
public $dbModel = [];
/**
* @var array Map for custom controller names not based on model name for exclusive cases
* @example
* 'controllerModelMap' => [
* 'User' => 'Profile', //use ProfileController for User model
* 'File' => 'Upload', //use UploadController for File model
* ]
**/
public $controllerModelMap = [];
/**
* @var bool Generate database models only for Schemas that not starts with underscore
*/
public $skipUnderscoredSchemas = true;
/**
* @var bool Generate database models only for Schemas that have the `x-table` annotation.
*/
public $generateModelsOnlyXTable = false;
/**
* @var bool whether to generate database migrations.
*/
public $generateMigrations = true;
/**
* @var string path to create migration files in.
* Defaults to `@app/migrations`.
*/
public $migrationPath = '@app/migrations';
/**
* @var string namespace to create migrations in.
* Defaults to `null` which means that migrations are generated without namespace.
*/
public $migrationNamespace;
private $fileRenderer;
/**
* @var OpenApi
*/
private $openApi;
public $resolvedOpenApi;
/**
* @return \cebe\openapi\spec\OpenApi
* @throws \cebe\openapi\exceptions\IOException
* @throws \cebe\openapi\exceptions\TypeErrorException
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
*/
public function getOpenApi():OpenApi
{
if ($this->openApi === null) {
$file = Yii::getAlias($this->openApiPath);
if (StringHelper::endsWith($this->openApiPath, '.json', false)) {
$this->openApi = Reader::readFromJsonFile($file, OpenApi::class, false);
} else {
$this->openApi = Reader::readFromYamlFile($file, OpenApi::class, false);
}
}
return $this->openApi;
}
public function getPathFromNamespace(string $namespace):string
{
return Yii::getAlias('@' . str_replace('\\', '/', ltrim($namespace, '\\')));
}
public function setFileRenderer(Closure $renderCallback):void
{
$this->fileRenderer = $renderCallback;
}
/**
* Generates code using the specified code template and parameters.
* Note that the code template will be used as a PHP file.
* @param string $template the code template file. This must be specified as a file path
* relative to [[templatePath]].
* @param array $params list of parameters to be passed to the template file.
* @return string the generated code
* @throws \yii\base\InvalidConfigException
*/
public function render(string $template, array $params = []):string
{
if (!$this->fileRenderer) {
throw new InvalidConfigException('Renderer is not configured');
}
return call_user_func($this->fileRenderer, $template, $params);
}
}