|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\generator; |
|
3
|
|
|
|
|
4
|
|
|
use gossi\codegen\model\AbstractPhpStruct; |
|
5
|
|
|
use gossi\codegen\model\PhpMethod; |
|
6
|
|
|
use gossi\codegen\model\PhpParameter; |
|
7
|
|
|
use keeko\tools\generator\AbstractCodeGenerator; |
|
8
|
|
|
use keeko\core\schema\ActionSchema; |
|
9
|
|
|
use gossi\codegen\model\PhpClass; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractResponseGenerator extends AbstractCodeGenerator { |
|
12
|
|
|
|
|
13
|
2 |
|
protected function getTemplateFolder() { |
|
14
|
2 |
|
return 'response'; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Generates a response class for the given action |
|
19
|
|
|
* |
|
20
|
|
|
* @param ActionSchema $action |
|
21
|
|
|
* @return PhpClass |
|
22
|
|
|
*/ |
|
23
|
|
|
abstract public function generate(ActionSchema $action); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Generates a response class for the given action |
|
27
|
|
|
* |
|
28
|
|
|
* @param ActionSchema $action |
|
29
|
|
|
* @param string $format |
|
30
|
|
|
* @return AbstractPhpStruct |
|
31
|
|
|
*/ |
|
32
|
5 |
|
protected function doGenerate(ActionSchema $action, $format) { |
|
33
|
5 |
|
$struct = $this->generateStruct($action, $format); |
|
34
|
|
|
|
|
35
|
5 |
|
$this->codegenService->addAuthors($struct, $this->packageService->getPackage()); |
|
36
|
|
|
|
|
37
|
5 |
|
$this->addUseStatements($struct); |
|
38
|
5 |
|
$this->addMethods($struct, $action); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
5 |
|
return $struct; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Generates the struct |
|
45
|
|
|
* |
|
46
|
|
|
* @param ActionSchema $action |
|
47
|
|
|
* @param string $format |
|
48
|
|
|
* @return AbstractPhpStruct |
|
49
|
|
|
*/ |
|
50
|
5 |
|
protected function generateStruct(ActionSchema $action, $format) { |
|
51
|
5 |
|
return PhpClass::create($action->getResponse($format)) |
|
52
|
5 |
|
->setParentClassName('AbstractResponse') |
|
53
|
5 |
|
->setDescription('Automatically generated ' . ucwords($format) . 'Response for ' . $action->getTitle()) |
|
54
|
5 |
|
->setLongDescription($action->getDescription()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
5 |
|
protected function generateRunMethod($body = '') { |
|
58
|
5 |
|
return PhpMethod::create('run') |
|
59
|
5 |
|
->setDescription('Automatically generated run method') |
|
60
|
5 |
|
->setType('Response') |
|
61
|
5 |
|
->addParameter(PhpParameter::create('request')->setType('Request')) |
|
62
|
5 |
|
->addParameter(PhpParameter::create('data')->setDefaultValue(null)) |
|
63
|
5 |
|
->setBody($body); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
protected function addUseStatements(AbstractPhpStruct $struct) { |
|
67
|
5 |
|
$struct->addUseStatement('keeko\\core\\action\\AbstractResponse'); |
|
68
|
5 |
|
$struct->addUseStatement('Symfony\\Component\\HttpFoundation\\Request'); |
|
69
|
5 |
|
$struct->addUseStatement('Symfony\\Component\\HttpFoundation\\Response'); |
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: