1
|
|
|
<?php |
2
|
|
|
namespace keeko\tools\generator\responder; |
3
|
|
|
|
4
|
|
|
use gossi\codegen\model\PhpClass; |
5
|
|
|
use gossi\codegen\model\PhpMethod; |
6
|
|
|
use keeko\framework\utils\NameUtils; |
7
|
|
|
use Propel\Generator\Model\Table; |
8
|
|
|
use gossi\codegen\model\PhpParameter; |
9
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
10
|
|
|
use keeko\framework\schema\ActionSchema; |
11
|
|
|
|
12
|
|
|
class AbstractPayloadJsonResponderGenerator extends AbstractJsonResponderGenerator { |
13
|
|
|
|
14
|
|
|
protected function ensureUseStatements(AbstractPhpStruct $struct) { |
15
|
|
|
parent::ensureUseStatements($struct); |
16
|
|
|
$struct->removeUseStatement('keeko\\framework\\domain\\payload\\PayloadInterface'); |
17
|
|
|
$struct->removeUseStatement('keeko\\framework\\foundation\\AbstractResponder'); |
18
|
|
|
$struct->addUseStatement('keeko\\framework\\foundation\\AbstractPayloadResponder'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function generateStruct(ActionSchema $action, $format) { |
22
|
|
|
$class = parent::generateStruct($action, $format); |
23
|
|
|
$class->setParentClassName('AbstractPayloadResponder'); |
24
|
|
|
|
25
|
|
|
return $class; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function generateGetPayloadMethods(PhpClass $class, $body = '') { |
29
|
|
|
$class->setMethod(PhpMethod::create('getPayloadMethods') |
30
|
|
|
->setVisibility(PhpMethod::VISIBILITY_PROTECTED) |
31
|
|
|
->setBody($body) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function generatePayloadMethod($name, $body, $type = 'PayloadInterface') { |
36
|
|
|
return PhpMethod::create($name) |
37
|
|
|
->addParameter(PhpParameter::create('request') |
38
|
|
|
->setType('Request') |
39
|
|
|
) |
40
|
|
|
->addParameter(PhpParameter::create('payload') |
41
|
|
|
->setType($type) |
42
|
|
|
) |
43
|
|
|
->setBody($body) |
44
|
|
|
; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function generateNotValid(PhpClass $class) { |
48
|
|
|
$class->addUseStatement('keeko\\framework\\domain\\payload\\NotValid'); |
49
|
|
|
$class->addUseStatement('keeko\framework\exceptions\ValidationException'); |
50
|
|
|
$notValid = $this->generatePayloadMethod('notValid', $this->twig->render('payload/notValid.twig'), |
51
|
|
|
'NotValid'); |
52
|
|
|
$class->setMethod($notValid); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function generateNotFound(PhpClass $class) { |
56
|
|
|
$class->addUseStatement('keeko\\framework\\domain\\payload\\NotFound'); |
57
|
|
|
$class->addUseStatement('Symfony\Component\Routing\Exception\ResourceNotFoundException'); |
58
|
|
|
$notFound = $this->generatePayloadMethod('notFound', $this->twig->render('payload/notFound.twig'), |
59
|
|
|
'NotFound'); |
60
|
|
|
$class->setMethod($notFound); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function getRelationshipIncludes(Table $model, $root = '', $processed = []) { |
64
|
|
|
if (in_array($model->getOriginCommonName(), $processed)) { |
65
|
|
|
return []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$relationships = $this->modelService->getRelationships($model); |
69
|
|
|
$includes = []; |
70
|
|
|
|
71
|
|
|
foreach ($relationships->getAll() as $rel) { |
72
|
|
|
$foreign = $rel->getForeign(); |
73
|
|
|
$processed[] = $foreign->getOriginCommonName(); |
74
|
|
|
|
75
|
|
|
$typeName = $rel->getRelatedTypeName(); |
76
|
|
|
$includes[] = (!empty($root) ? $root . '.' : '') . $typeName; |
77
|
|
|
$includes = array_merge($includes, $this->getRelationshipIncludes($foreign, $typeName, $processed)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $includes; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function getModelFields(Table $model, $root = '', $processed = []) { |
84
|
|
|
if (in_array($model->getOriginCommonName(), $processed)) { |
85
|
|
|
return []; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$typeName = NameUtils::dasherize($model->getOriginCommonName()); |
89
|
|
|
$relationships = $this->modelService->getRelationships($model); |
90
|
|
|
$fields = [$typeName => $model]; |
91
|
|
|
|
92
|
|
|
foreach ($relationships->getAll() as $rel) { |
93
|
|
|
$foreign = $rel->getForeign(); |
94
|
|
|
$processed[] = $foreign->getOriginCommonName(); |
95
|
|
|
|
96
|
|
|
$typeName = $rel->getRelatedTypeName(); |
97
|
|
|
$name = (!empty($root) ? $root . '.' : '') . $typeName; |
98
|
|
|
|
99
|
|
|
$fields[$name] = $foreign; |
100
|
|
|
$fields = array_merge($fields, $this->getModelFields($foreign, $name, $processed)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $fields; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getFieldsCode(array $fields) { |
107
|
|
|
$code = ''; |
108
|
|
|
foreach ($fields as $typeName => $field) { |
109
|
|
|
$code .= sprintf("\t'%s' => %s::getSerializer()->getFields(),\n", $typeName, $field->getPhpName()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (strlen($code) > 0) { |
113
|
|
|
$code = substr($code, 0, -2); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return sprintf("[\n%s\n]", $code); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|