|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\generator; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
|
5
|
|
|
use gossi\codegen\model\PhpTrait; |
|
6
|
|
|
use keeko\core\schema\ActionSchema; |
|
7
|
|
|
use keeko\tools\utils\NameUtils; |
|
8
|
|
|
use gossi\codegen\model\PhpMethod; |
|
9
|
|
|
use gossi\codegen\model\PhpParameter; |
|
10
|
|
|
|
|
11
|
|
|
class ModelResponseTraitGenerator extends AbstractResponseGenerator { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Generates a json response class for the given action |
|
15
|
|
|
* |
|
16
|
|
|
* @param ActionSchema $action |
|
17
|
|
|
* @return PhpTrait |
|
18
|
|
|
*/ |
|
19
|
2 |
|
public function generate(ActionSchema $action) { |
|
20
|
2 |
|
return $this->doGenerate($action, 'json'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Generates the struct |
|
25
|
|
|
* |
|
26
|
|
|
* @param ActionSchema $action |
|
27
|
|
|
* @param string $format |
|
28
|
|
|
* @return PhpTrat |
|
29
|
|
|
*/ |
|
30
|
2 |
|
protected function generateStruct(ActionSchema $action, $format) { |
|
31
|
2 |
|
$modelName = $this->modelService->getModelNameByAction($action); |
|
32
|
2 |
|
$model = $this->modelService->getModel($modelName); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
2 |
|
$namespace = $this->modelService->getDatabase()->getNamespace(); |
|
|
|
|
|
|
35
|
2 |
|
$nsModelName = $namespace . '\\' . $model->getPhpName(); |
|
36
|
|
|
|
|
37
|
2 |
|
$response = $action->getResponse($format); |
|
38
|
2 |
|
$name = str_replace('action', 'response', $response); |
|
|
|
|
|
|
39
|
2 |
|
$ns = dirname(str_replace('\\', '/', $name)); |
|
|
|
|
|
|
40
|
2 |
|
$name = str_replace('/', '\\', $ns) . '\\' . $model->getPhpName() . 'ResponseTrait'; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
2 |
|
return PhpTrait::create($name) |
|
43
|
2 |
|
->setDescription('Automatically generated common response methods for ' . $modelName) |
|
44
|
2 |
|
->addUseStatement($nsModelName); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
protected function addUseStatements(AbstractPhpStruct $struct) { |
|
48
|
2 |
|
$struct->addUseStatement('keeko\\core\\utils\\FilterUtils'); |
|
49
|
2 |
|
$struct->addUseStatement('Propel\\Runtime\\Map\\TableMap'); |
|
50
|
2 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
protected function addMethods(PhpTrait $trait, ActionSchema $action) { |
|
53
|
2 |
|
$modelName = $this->modelService->getModelNameByAction($action); |
|
|
|
|
|
|
54
|
2 |
|
$model = $this->modelService->getModel($modelName); |
|
|
|
|
|
|
55
|
2 |
|
$modelVariableName = NameUtils::toCamelCase($modelName); |
|
56
|
2 |
|
$modelObjectName = $model->getPhpName(); |
|
|
|
|
|
|
57
|
2 |
|
$codegen = $this->codegenService->getCodegen(); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
// method: filter(array ${{model}}) |
|
|
|
|
|
|
60
|
2 |
|
$trait->setMethod(PhpMethod::create('filter') |
|
61
|
2 |
|
->setDescription('Automatically generated method, will be overridden') |
|
62
|
2 |
|
->addParameter(PhpParameter::create($modelVariableName)->setType('array')) |
|
63
|
2 |
|
->setVisibility('protected') |
|
64
|
2 |
|
->setBody($this->twig->render('filter.twig', [ |
|
65
|
2 |
|
'model' => $modelVariableName, |
|
66
|
2 |
|
'filter' => $this->codegenService->arrayToCode($codegen->getReadFilter($modelName)) |
|
67
|
2 |
|
])) |
|
68
|
2 |
|
); |
|
69
|
|
|
|
|
70
|
|
|
// method: {{model}}toArray({{Model}} ${{model}}) |
|
|
|
|
|
|
71
|
2 |
|
$trait->setMethod(PhpMethod::create($modelVariableName . 'ToArray') |
|
72
|
2 |
|
->setDescription('Automatically generated method, will be overridden') |
|
73
|
2 |
|
->addParameter(PhpParameter::create($modelVariableName)->setType($modelObjectName)) |
|
74
|
2 |
|
->setVisibility('protected') |
|
75
|
2 |
|
->setBody($this->twig->render('modelToArray.twig', ['model' => $modelVariableName])) |
|
76
|
2 |
|
); |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.