1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\ResourceGenerator\FileGenerators; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand; |
6
|
|
|
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface; |
7
|
|
|
use Doctrine\Common\Inflector\Inflector; |
8
|
|
|
use PhpParser\BuilderFactory; |
9
|
|
|
use PhpParser\Node; |
10
|
|
|
|
11
|
|
|
final class SyncClassGenerator extends AbstractExtendingClassGenerator implements FileGeneratorInterface |
12
|
|
|
{ |
13
|
|
|
const NAMESPACE = 'Sync'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return Node |
17
|
|
|
*/ |
18
|
1 |
|
public function generate(): Node |
19
|
|
|
{ |
20
|
1 |
|
$classChunks = explode('\\', $this->yaml['class']); |
21
|
1 |
|
$className = array_pop($classChunks); |
22
|
1 |
|
$interfaceName = $className . 'Interface'; |
23
|
|
|
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE; |
24
|
1 |
|
if (count($classChunks) > 0) { |
25
|
1 |
|
$namespace .= '\\' . implode('\\', $classChunks); |
26
|
1 |
|
$namespace = str_replace('\\\\', '\\', $namespace); |
27
|
|
|
} |
28
|
1 |
|
$baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class']; |
29
|
1 |
|
$interfaceFQName = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'] . 'Interface'; |
30
|
|
|
|
31
|
1 |
|
$factory = new BuilderFactory(); |
32
|
|
|
|
33
|
1 |
|
$class = $factory->class($className) |
34
|
1 |
|
->extend('Base' . $className); |
35
|
|
|
|
36
|
1 |
|
$class->addStmt($factory->method('refresh') |
37
|
1 |
|
->makePublic() |
38
|
1 |
|
->setReturnType($className) |
39
|
1 |
|
->addStmt( |
40
|
1 |
|
new Node\Stmt\Return_( |
41
|
1 |
|
new Node\Expr\MethodCall( |
42
|
1 |
|
new Node\Expr\Variable('this'), |
43
|
1 |
|
'wait', |
44
|
|
|
[ |
45
|
1 |
|
new Node\Expr\MethodCall( |
46
|
1 |
|
new Node\Expr\MethodCall( |
47
|
1 |
|
new Node\Expr\Variable('this'), |
48
|
1 |
|
'handleCommand', |
49
|
|
|
[ |
50
|
1 |
|
new Node\Expr\New_( |
51
|
1 |
|
new Node\Name('BuildAsyncFromSyncCommand'), |
52
|
|
|
[ |
53
|
1 |
|
new Node\Expr\ClassConstFetch( |
54
|
1 |
|
new Node\Name('self'), |
55
|
1 |
|
'HYDRATE_CLASS' |
56
|
|
|
), |
57
|
1 |
|
new Node\Expr\Variable('this'), |
58
|
|
|
] |
59
|
|
|
), |
60
|
|
|
] |
61
|
|
|
), |
62
|
1 |
|
'then', |
63
|
|
|
[ |
64
|
1 |
|
new Node\Expr\Closure( |
65
|
|
|
[ |
66
|
|
|
'params' => [ |
67
|
1 |
|
new Node\Param( |
68
|
1 |
|
Inflector::camelize($className), |
69
|
1 |
|
null, |
70
|
1 |
|
$interfaceName |
71
|
|
|
), |
72
|
|
|
], |
73
|
|
|
'stmts' => [ |
74
|
1 |
|
new Node\Stmt\Return_( |
75
|
1 |
|
new Node\Expr\MethodCall( |
76
|
1 |
|
new Node\Expr\Variable( |
77
|
1 |
|
Inflector::camelize($className) |
78
|
|
|
), |
79
|
1 |
|
'refresh' |
80
|
|
|
) |
81
|
|
|
), |
82
|
|
|
], |
83
|
|
|
] |
84
|
|
|
), |
85
|
|
|
] |
86
|
|
|
), |
87
|
|
|
] |
88
|
|
|
) |
89
|
|
|
) |
90
|
|
|
)); |
91
|
|
|
|
92
|
1 |
|
return $factory->namespace($namespace) |
93
|
1 |
|
->addStmt($factory->use(BuildAsyncFromSyncCommand::class)) |
94
|
1 |
|
->addStmt($factory->use($baseClass)->as('Base' . $className)) |
95
|
1 |
|
->addStmt($factory->use($interfaceFQName)) |
96
|
1 |
|
->addStmt($class) |
97
|
1 |
|
->getNode() |
98
|
|
|
; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|