-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContainerConfig.php
More file actions
200 lines (170 loc) · 7.9 KB
/
Copy pathContainerConfig.php
File metadata and controls
200 lines (170 loc) · 7.9 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
declare(strict_types=1);
namespace ESB;
use ESB\Auth\AuthServiceInterface;
use ESB\Exception\SetupException;
use ESB\Handlers\HTTP\ESBHandler;
use ESB\Handlers\HTTP\ESBHandlerInterface;
use ESB\Handlers\PostHandlerInterface;
use ESB\Handlers\QueueMessageHandler;
use ESB\Middleware\Core\PostErrorMiddleware;
use ESB\Middleware\Core\PostSuccessMiddleware;
use ESB\Middleware\Core\ProcessingMiddleware;
use ESB\Middleware\Core\SyncRecordsMiddleware;
use ESB\Middleware\Core\TransportMiddleware;
use ESB\Middleware\Core\ValidatorMiddleware;
use ESB\Middleware\Queue\ErrorHandlerMiddleware;
use ESB\Middleware\Queue\RunCoreMiddleware;
use ESB\Service\AuthServicePool;
use ESB\Service\ClientPool;
use ESB\Service\CoreRunnersPool;
use ESB\Service\DynamicDsnParser;
use ESB\Service\DynamicDsnParserInterface;
use ESB\Service\DynamicPropertiesFetcher;
use ESB\Service\DynamicPropertiesFetcherInterface;
use ESB\Service\PostErrorHandlersPool;
use ESB\Service\PostSuccessHandlersPool;
use ESB\Service\ValidatorsPool;
use ESB\Validation\ValidatorInterface;
use Psr\Container\ContainerInterface;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\TwigFunction;
class ContainerConfig
{
public function __invoke() : array
{
return [
'validators' => [
// Reserved key for custom validators, should implement ValidatorInterface
// 'alias' => CustomValidator::class,
],
'formatters' => [
// Reserved key for twig function-helpers
// alias => Invocable::class
],
'post-success' => [
// Reserved key for post success handlers
// alias => MyPostSuccessHandler::class
],
'post-error' => [
// Reserved key for post error handlers
// alias => MyPostErrorHandler::class
],
'runner' => [
// Reserved key for custom runners
// alias => MyCustomRunner::class
],
'auth' => [
// Reserved key for auth services
// alias => MyAuthService::class
],
Core::class => function(ContainerInterface $container) : Core
{
// PostSuccessMiddleware is last, ValidatorMiddleware is first
return new Core(
$container->get(PostErrorMiddleware::class),
$container->get(PostSuccessMiddleware::class),
$container->get(SyncRecordsMiddleware::class),
$container->get(TransportMiddleware::class),
$container->get(ProcessingMiddleware::class),
$container->get(ValidatorMiddleware::class),
);
},
CoreRunner::class => function(ContainerInterface $container) : CoreRunner
{
return new CoreRunner(
$container->get(Core::class),
);
},
CoreRunnersPool::class => function(ContainerInterface $container) : CoreRunnersPool
{
$pool = new CoreRunnersPool($container->get(CoreRunner::class));
$definedRunners = $container->get('runner');
foreach ($definedRunners as $alias => $runnerClass) {
$runner = $container->get($runnerClass);
if (! $runner instanceof CoreRunnerInterface) {
throw new SetupException('ESBHandler: runner config invalid');
}
$pool->add($alias, $runner);
}
return $pool;
},
Environment::class => function(ContainerInterface $container) : Environment
{
$twig = new Environment(new ArrayLoader(), [
'strict_variables' => false,
'autoescape' => false,
]);
$definedFormatters = $container->get('formatters');
foreach ($definedFormatters as $key => $formatterClass) {
$formatter = $container->get($formatterClass);
$twig->addFunction(new TwigFunction($key, $formatter(...)));
}
return $twig;
},
QueueMessageHandler::class => fn(ContainerInterface $container) => new QueueMessageHandler(
$container->get(RunCoreMiddleware::class),
$container->get(ErrorHandlerMiddleware::class),
),
ClientPool::class => fn() => new ClientPool(),
AuthServicePool::class => function(ContainerInterface $container) : AuthServicePool {
$pool = new AuthServicePool;
$authServices = $container->get('auth');
foreach ($authServices as $alias => $serviceClass) {
$service = $container->get($serviceClass);
if (! $service instanceof AuthServiceInterface) {
throw new SetupException('AuthServicePool: auth config invalid');
}
$pool->add($alias, $service);
}
return $pool;
},
ValidatorsPool::class => function(ContainerInterface $container) : ValidatorsPool
{
// Get list of defined validators
$definedValidators = $container->get('validators');
$pool = new ValidatorsPool;
foreach ($definedValidators as $alias => $validatorClass) {
$validator = $container->get($validatorClass);
if (! $validator instanceof ValidatorInterface) {
throw new SetupException('ValidatorMiddleware: custom validator config invalid');
}
$pool->add($alias, $validator);
}
return $pool;
},
PostSuccessHandlersPool::class => function(ContainerInterface $container) : PostSuccessHandlersPool {
$pool = new PostSuccessHandlersPool();
// Get list of defined post success handlers
$definedHandlers = $container->get('post-success');
// Prepare list with all classes
foreach ($definedHandlers as $alias => $handlerClass) {
$containerHandler = $container->get($handlerClass);
if (! $containerHandler instanceof PostHandlerInterface) {
throw new SetupException('PostSuccessMiddleware: custom handler config invalid');
}
$pool->add($alias, $containerHandler);
}
return $pool;
},
PostErrorHandlersPool::class => function(ContainerInterface $container) : PostErrorHandlersPool {
$pool = new PostErrorHandlersPool();
// Get list of defined post success handlers
$definedHandlers = $container->get('post-error');
// Prepare list with all classes
foreach ($definedHandlers as $alias => $handlerClass) {
$containerHandler = $container->get($handlerClass);
if (! $containerHandler instanceof PostHandlerInterface) {
throw new SetupException('PostErrorMiddleware: custom handler config invalid');
}
$pool->add($alias, $containerHandler);
}
return $pool;
},
DynamicPropertiesFetcherInterface::class => static fn(ContainerInterface $container) : mixed => $container->get(DynamicPropertiesFetcher::class),
DynamicDsnParserInterface::class => fn(ContainerInterface $container) => $container->get(DynamicDsnParser::class),
ESBHandlerInterface::class => fn(ContainerInterface $container) : mixed => $container->get(ESBHandler::class)
];
}
}