|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Modelarium\Laravel\Targets; |
|
4
|
|
|
|
|
5
|
|
|
use GraphQL\Type\Definition\NonNull; |
|
6
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
|
8
|
|
|
use Modelarium\BaseGenerator; |
|
9
|
|
|
use Modelarium\GeneratedCollection; |
|
10
|
|
|
use Modelarium\GeneratedItem; |
|
11
|
|
|
use Modelarium\Parser; |
|
12
|
|
|
use Nette\PhpGenerator\ClassType; |
|
13
|
|
|
|
|
14
|
|
|
class EventGenerator extends BaseGenerator |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $stubDir = __DIR__ . "/stubs/"; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var ObjectType |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $type = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var GeneratedCollection |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $events; |
|
30
|
2 |
|
|
|
31
|
|
|
public function generate(): GeneratedCollection |
|
32
|
2 |
|
{ |
|
33
|
|
|
$this->events = new GeneratedCollection(); |
|
34
|
2 |
|
|
|
35
|
1 |
|
foreach ($this->type->getFields() as $field) { |
|
36
|
1 |
|
$directives = $field->astNode->directives; |
|
37
|
|
|
$this->processFieldDirectives($field, $directives, 'Event'); |
|
38
|
2 |
|
} |
|
39
|
|
|
return $this->events; |
|
40
|
|
|
} |
|
41
|
1 |
|
|
|
42
|
|
|
protected function makeEventClass(string $name, string $model): GeneratedItem |
|
43
|
1 |
|
{ |
|
44
|
|
|
list($eventNamespace, $eventClassName, $relativePath) = $this->splitClassName($name); |
|
45
|
1 |
|
|
|
46
|
|
|
$namespace = new \Nette\PhpGenerator\PhpNamespace($eventNamespace); |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var ClassType $class |
|
50
|
1 |
|
*/ |
|
51
|
|
|
$class = $namespace->addClass($eventClassName); |
|
52
|
1 |
|
$class |
|
53
|
1 |
|
->addComment("This file was automatically generated by Modelarium.") |
|
54
|
|
|
->setTraits(['Illuminate\Queue\SerializesModels']); |
|
55
|
1 |
|
|
|
56
|
1 |
|
$class->addProperty('target') |
|
57
|
1 |
|
->setPublic() |
|
58
|
1 |
|
->setComment("@var $model"); |
|
59
|
|
|
$printer = new \Nette\PhpGenerator\PsrPrinter; |
|
60
|
1 |
|
|
|
61
|
1 |
|
$constructor = $class->addMethod('__construct'); |
|
62
|
1 |
|
$constructor->setPublic() |
|
63
|
1 |
|
->addParameter('target') |
|
64
|
|
|
->setType('App\\Models\\' . $model); |
|
65
|
1 |
|
|
|
66
|
1 |
|
$constructor->addBody( |
|
67
|
|
|
'$this->target = $target;' |
|
68
|
|
|
); |
|
69
|
1 |
|
|
|
70
|
1 |
|
return new GeneratedItem( |
|
71
|
1 |
|
$name, |
|
72
|
1 |
|
"<?php declare(strict_types=1);\n\n" . $printer->printNamespace($namespace), |
|
73
|
|
|
$this->getGenerateFilename($relativePath, $eventClassName) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
1 |
|
|
|
77
|
|
|
public function processFieldDirectives( |
|
78
|
|
|
\GraphQL\Type\Definition\FieldDefinition $field, |
|
79
|
|
|
\GraphQL\Language\AST\NodeList $directives, |
|
80
|
|
|
string $type |
|
81
|
|
|
): void { |
|
82
|
1 |
|
// TODO: there's probably no need to override this |
|
83
|
|
|
if ($field->type instanceof NonNull) { |
|
84
|
|
|
$type = $field->type->getWrappedType(); |
|
85
|
1 |
|
} else { |
|
86
|
|
|
$type = $field->type; |
|
87
|
1 |
|
} |
|
88
|
|
|
$typeName = $type->name; /** @phpstan-ignore-line */ |
|
89
|
1 |
|
|
|
90
|
1 |
|
foreach ($directives as $directive) { |
|
91
|
1 |
|
$name = $directive->name->value; |
|
92
|
1 |
|
switch ($name) { |
|
93
|
1 |
|
case 'event': |
|
94
|
|
|
$dispatch = Parser::getDirectiveArgumentByName($directive, 'dispatch'); |
|
95
|
1 |
|
$e = $this->makeEventClass($dispatch, $typeName); |
|
|
|
|
|
|
96
|
|
|
$this->events->push($e); |
|
97
|
|
|
break; |
|
98
|
|
|
default: |
|
99
|
|
|
} |
|
100
|
1 |
|
} |
|
101
|
|
|
} |
|
102
|
1 |
|
|
|
103
|
1 |
|
public function getGenerateFilename(string $relativePath, string $name): string |
|
104
|
1 |
|
{ |
|
105
|
1 |
|
// fix App -> app |
|
106
|
|
|
if (mb_substr($relativePath, 0, 3) === 'App' && !is_dir($this->getBasePath($relativePath))) { |
|
107
|
|
|
$relativePath[0] = 'a'; |
|
108
|
1 |
|
} |
|
109
|
1 |
|
return $this->getBasePath($relativePath . '/' . $name . '.php'); |
|
110
|
1 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|