/
CoreSchemaPlugin.php
172 lines (141 loc) · 4.93 KB
/
CoreSchemaPlugin.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php declare(strict_types = 1);
namespace Apitte\Core\DI\Plugin;
use Apitte\Core\DI\Loader\DoctrineAnnotationLoader;
use Apitte\Core\DI\Loader\ILoader;
use Apitte\Core\DI\Loader\NeonLoader;
use Apitte\Core\Schema\SchemaBuilder;
use Apitte\Core\Schema\Serialization\ArrayHydrator;
use Apitte\Core\Schema\Serialization\ArraySerializator;
use Apitte\Core\Schema\Serialization\IDecorator;
use Apitte\Core\Schema\Validation\ControllerPathValidation;
use Apitte\Core\Schema\Validation\ControllerValidation;
use Apitte\Core\Schema\Validation\FullpathValidation;
use Apitte\Core\Schema\Validation\GroupPathValidation;
use Apitte\Core\Schema\Validation\IdValidation;
use Apitte\Core\Schema\Validation\IValidation;
use Apitte\Core\Schema\Validation\NegotiationValidation;
use Apitte\Core\Schema\Validation\PathValidation;
use Apitte\Core\Schema\Validation\RequestBodyValidation;
use Apitte\Core\Schema\Validation\RequestParameterValidation;
use Apitte\Core\Schema\Validator\SchemaBuilderValidator;
use Nette\DI\Config\Adapters\NeonAdapter;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\Arrays;
use stdClass;
/**
* @property-read stdClass $config
*/
class CoreSchemaPlugin extends Plugin
{
/** @var IDecorator[] */
public static array $decorators = [];
public static function getName(): string
{
return 'schema';
}
/**
* Decorate services
*/
public function beforePluginCompile(): void
{
// Receive container builder
$builder = $this->getContainerBuilder();
// Register services
$builder->addDefinition($this->extensionPrefix('core.schema.hydrator'))
->setFactory(ArrayHydrator::class);
$schemaDefinition = $builder->getDefinition($this->extensionPrefix('core.schema'));
assert($schemaDefinition instanceof ServiceDefinition);
$schemaDefinition->setFactory('@' . $this->extensionPrefix('core.schema.hydrator') . '::hydrate', [$this->compileSchema()]);
}
protected function getConfigSchema(): Schema
{
return Expect::structure([
'loaders' => Expect::structure([
'annotations' => Expect::structure([
'enable' => Expect::bool(true),
'loader' => Expect::string(DoctrineAnnotationLoader::class),
]),
'neon' => Expect::structure([
'enable' => Expect::bool(false),
'files' => Expect::arrayOf('string'),
]),
]),
'schema' => Expect::array(),
'validations' => Expect::array()->default([
'controller' => ControllerValidation::class,
'controllerPath' => ControllerPathValidation::class,
'fullPath' => FullpathValidation::class,
'groupPath' => GroupPathValidation::class,
'id' => IdValidation::class,
'negotiation' => NegotiationValidation::class,
'path' => PathValidation::class,
'requestBody' => RequestBodyValidation::class,
]),
]);
}
/**
* @return mixed[]
*/
protected function compileSchema(): array
{
// Instance schema builder
$builder = new SchemaBuilder();
// Load schema
$builder = $this->loadSchema($builder);
// Validate schema
$builder = $this->validateSchema($builder);
// Update schema at compile-time
foreach (self::$decorators as $decorator) {
$decorator->decorate($builder);
}
// Convert schema to array (for DI)
$generator = new ArraySerializator();
return $generator->serialize($builder);
}
protected function loadSchema(SchemaBuilder $builder): SchemaBuilder
{
$loaders = $this->config->loaders;
if ($loaders->annotations->enable) {
if (!class_exists($loaders->annotations->loader)) {
throw new \RuntimeException(sprintf('Annotation loader class %s does not exist', $loaders->annotations->loader));
}
if (!is_subclass_of($loaders->annotations->loader, ILoader::class)) {
throw new \RuntimeException(sprintf('Annotation loader class %s must be subclass of %s', $loaders->annotations->loader, ILoader::class));
}
$loader = new $loaders->annotations->loader($this->getContainerBuilder());
$builder = $loader->load($builder);
}
if ($loaders->neon->enable) {
$files = $loaders->neon->files;
// Load schema from files
$adapter = new NeonAdapter();
$schema = [];
foreach ($files as $file) {
$schema = Arrays::mergeTree($schema, $adapter->load($file));
}
$loader = new NeonLoader($schema);
$builder = $loader->load($builder);
}
return $builder;
}
protected function validateSchema(SchemaBuilder $builder): SchemaBuilder
{
$validations = $this->config->validations;
$validator = new SchemaBuilderValidator();
// Add all validators at compile-time
/** @var class-string<IValidation> $validation */
foreach ($validations as $validation) {
$validator->add(new $validation());
}
/** @var ?CoreMappingPlugin $coreMappingPlugin */
$coreMappingPlugin = $this->compiler->getPlugin(CoreMappingPlugin::getName());
if ($coreMappingPlugin !== null) {
$validator->add(new RequestParameterValidation($coreMappingPlugin->getAllowedTypes()));
}
// Validate schema
$validator->validate($builder);
return $builder;
}
}