-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
YamlDefinitionLoader.php
148 lines (117 loc) · 5.38 KB
/
YamlDefinitionLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
namespace EventSauce\EventSourcing\CodeGeneration;
use const PATHINFO_EXTENSION;
use InvalidArgumentException;
use LogicException;
use Symfony\Component\Yaml\Yaml;
use function file_get_contents;
use function in_array;
use function is_array;
use function is_string;
use function pathinfo;
class YamlDefinitionLoader implements DefinitionLoader
{
public function canLoad(string $filename): bool
{
return in_array(pathinfo($filename, PATHINFO_EXTENSION), ['yaml', 'yml']);
}
public function load(string $filename, DefinitionGroup $definitionGroup = null): DefinitionGroup
{
/** @var string|bool $fileContents */
$fileContents = file_get_contents($filename);
if ( ! is_string($fileContents) || empty($fileContents)) {
throw new InvalidArgumentException("File {$filename} does not contain anything");
}
$definition = Yaml::parse($fileContents);
if ( ! is_array($definition)) {
throw new InvalidArgumentException('The definition is incorrectly formatted');
}
$definitionGroup = $definitionGroup ?: new DefinitionGroup();
if (isset($definition['namespace'])) {
$definitionGroup->withNamespace($definition['namespace']);
}
$this->loadInterfaces($definitionGroup, $definition['interfaces'] ?? []);
$this->loadTypeHandlers($definitionGroup, $definition['types'] ?? []);
$this->loadFieldDefaults($definitionGroup, $definition['fields'] ?? []);
$this->loadCommands($definitionGroup, $definition['commands'] ?? []);
$this->loadEvents($definitionGroup, $definition['events'] ?? []);
return $definitionGroup;
}
private function loadTypeHandlers(DefinitionGroup $definitionGroup, array $types): void
{
foreach ($types as $type => $handlers) {
if (isset($handlers['type'])) {
$definitionGroup->aliasType($type, $handlers['type']);
}
if (isset($handlers['nullable'])) {
$definitionGroup->setTypeNullability($type, (bool) $handlers['nullable']);
}
if (isset($handlers['serializer'])) {
$definitionGroup->typeSerializer($type, $handlers['serializer']);
}
if (isset($handlers['deserializer'])) {
$definitionGroup->typeDeserializer($type, $handlers['deserializer']);
}
}
}
private function loadCommands(DefinitionGroup $definitionGroup, array $commands): void
{
foreach ($commands as $commandName => $commandDefinition) {
$command = $definitionGroup->command($commandName);
$this->hydrateDefinition($definitionGroup, $command, $commandDefinition);
}
}
private function loadEvents(DefinitionGroup $definitionGroup, array $events): void
{
foreach ($events as $eventName => $eventDefinition) {
$event = $definitionGroup->event($eventName);
$this->hydrateDefinition($definitionGroup, $event, $eventDefinition);
}
}
private function loadFieldDefaults(DefinitionGroup $definitionGroup, array $defaults): void
{
foreach ($defaults as $field => $default) {
$definitionGroup->fieldDefault($field, $default['type'], $default['example'] ?? null, $default['nullable'] ?? null);
if (isset($default['serializer'])) {
$definitionGroup->fieldSerializer($field, $default['serializer']);
}
if (isset($default['deserializer'])) {
$definitionGroup->fieldDeserializer($field, $default['deserializer']);
}
}
}
private function loadInterfaces(DefinitionGroup $definitionGroup, array $interfaces): void
{
foreach ($interfaces as $alias => $interfaceName) {
if ( ! interface_exists($interfaceName)) {
throw new LogicException("Interface {$interfaceName} does not exist.");
}
/** @var class-string $interface */
$interface = '\\' . ltrim($interfaceName, '\\');
$definitionGroup->defineInterface($alias, $interface);
}
}
private function hydrateDefinition(DefinitionGroup $definitionGroup, PayloadDefinition $definition, array $input): void
{
$definition->withFieldsFrom($input['fields_from'] ?? '');
$fields = $input['fields'] ?? [];
$interfaces = $input['implements'] ?? [];
foreach ((array) $interfaces as $interface) {
$definition->withInterface($definitionGroup->resolveInterface($interface));
}
foreach ($fields as $fieldName => $fieldDefinition) {
if (is_string($fieldDefinition)) {
$fieldDefinition = ['type' => TypeNormalizer::normalize($fieldDefinition)];
}
$type = $fieldDefinition['type'] ?? $definitionGroup->typeForField($fieldName);
$definition->field($fieldName, TypeNormalizer::normalize($type), (string) ($fieldDefinition['example'] ?? null), $fieldDefinition['nullable'] ?? null);
if (isset($fieldDefinition['serializer'])) {
$definition->fieldSerializer($fieldName, $fieldDefinition['serializer']);
}
if (isset($fieldDefinition['deserializer'])) {
$definition->fieldDeserializer($fieldName, $fieldDefinition['deserializer']);
}
}
}
}