forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonActionGenerator.php
112 lines (104 loc) · 4.84 KB
/
JsonActionGenerator.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
<?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\openapi\spec\Operation;
use cebe\yii2openapi\lib\items\FractalAction;
use cebe\yii2openapi\lib\items\RouteData;
use cebe\yii2openapi\lib\openapi\ResponseSchema;
use Yii;
use yii\base\BaseObject;
use yii\helpers\Inflector;
class JsonActionGenerator extends RestActionGenerator
{
/**
* @param string $method
* @param \cebe\openapi\spec\Operation $operation
* @param \cebe\yii2openapi\lib\items\RouteData $routeData
* @return \cebe\yii2openapi\lib\items\RestAction|object
* @throws \yii\base\InvalidConfigException
* @throws \cebe\openapi\exceptions\UnresolvableReferenceException
*/
protected function prepareAction(
string $method,
Operation $operation,
RouteData $routeData,
?string $customRoute = null
): BaseObject {
$actionType = $this->resolveActionType($routeData, $method);
$modelClass = ResponseSchema::guessModelClass($operation, $actionType);
$expectedRelations = in_array($actionType, ['list', 'view'])
? ResponseSchema::guessResponseRelations($operation)
: [];
// fallback to known model class on same URL
if ($modelClass === null && isset($this->knownModelClasses[$routeData->path])) {
$modelClass = $this->knownModelClasses[$routeData->path];
} else {
$this->knownModelClasses[$routeData->path] = $modelClass;
}
if ($routeData->isRelationship()) {
$relatedClass = $modelClass;
$transformerClass = $modelClass !== null
? $this->config->transformerNamespace . '\\' . Inflector::id2camel($modelClass, '_') . 'Transformer'
: null;
$controllerId = $routeData->controller;
$modelClass = Inflector::id2camel(Inflector::singularize($controllerId));
if (isset($this->config->controllerModelMap[$modelClass])) {
$controllerId = Inflector::camel2id($this->config->controllerModelMap[$modelClass]);
}
} else {
$relatedClass = null;
if ($modelClass === null || !$routeData->isModelBasedType()) {
$controllerId = $routeData->controller;
} elseif (isset($this->config->controllerModelMap[$modelClass])) {
$controllerId = Inflector::camel2id($this->config->controllerModelMap[$modelClass]);
} else {
$controllerId = Inflector::camel2id($modelClass);
}
$transformerClass = $modelClass !== null
? $this->config->transformerNamespace . '\\' . Inflector::id2camel($modelClass, '_') . 'Transformer'
: null;
}
if ($routeData->type === RouteData::TYPE_RESOURCE_OPERATION && !$modelClass) {
$modelClass = Inflector::id2camel(Inflector::singularize($controllerId));
if (isset($this->config->controllerModelMap[$modelClass])) {
$controllerId = Inflector::camel2id($this->config->controllerModelMap[$modelClass]);
}
}
$actionId = $routeData->isNonCrudAction() ? trim("{$actionType}-{$routeData->action}", '-')
: "$actionType{$routeData->action}";
if (!empty($customRoute)) {
// $actionType = '';
$parts = explode('/', $customRoute);
$controllerId = $parts[count($parts) - 2];
$actionId = $parts[count($parts) - 1];
}
return Yii::createObject(FractalAction::class, [
[
'singularResourceKey' => $this->config->singularResourceKeys,
'type' => $routeData->type,
'id' => $actionId,
'controllerId' => $controllerId,
'urlPath' => $routeData->path,
'requestMethod' => strtoupper($method),
'urlPattern' => $routeData->pattern,
'idParam' => $routeData->idParam ?? null,
'parentIdParam' => $routeData->parentParam ?? null,
'params' => $routeData->params,
'modelName' => $modelClass,
'relatedModel' => $relatedClass,
'modelFqn' => $modelClass !== null
? $this->config->modelNamespace . '\\' . Inflector::id2camel($modelClass, '_')
: null,
'transformerFqn' => $transformerClass,
'expectedRelations' => $expectedRelations,
'prefix' => $routeData->getPrefix(),
'prefixSettings' => $routeData->getPrefixSettings(),
'xRoute' => $customRoute,
'modulesList' => $routeData->listModules()
],
]);
}
}