1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation\Hydrator; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Events\CommandLocatorEvent; |
6
|
|
|
use ApiClients\Foundation\Hydrator\Annotations; |
7
|
|
|
use Interop\Container\ContainerInterface; |
8
|
|
|
use League\Event\EmitterInterface; |
9
|
|
|
|
10
|
|
|
class Factory |
11
|
|
|
{ |
12
|
|
|
const ANNOTATIONS = [ |
13
|
|
|
Annotations\Collection::class => Annotations\Handler\CollectionHandler::class, |
14
|
|
|
Annotations\Nested::class => Annotations\Handler\NestedHandler::class, |
15
|
|
|
Annotations\Rename::class => Annotations\Handler\RenameHandler::class, |
16
|
|
|
]; |
17
|
|
|
|
18
|
8 |
|
public static function create(ContainerInterface $container, array $options = []): Hydrator |
19
|
|
|
{ |
20
|
8 |
|
$container->get(EmitterInterface::class)-> |
21
|
8 |
|
addListener(CommandLocatorEvent::NAME, function (CommandLocatorEvent $event) { |
22
|
1 |
|
$event->add( |
23
|
1 |
|
__DIR__ . DIRECTORY_SEPARATOR . 'CommandBus' . DIRECTORY_SEPARATOR, |
24
|
1 |
|
__NAMESPACE__ . '\CommandBus' |
25
|
|
|
); |
26
|
8 |
|
}) |
27
|
|
|
; |
28
|
|
|
|
29
|
8 |
|
$options[Options::ANNOTATIONS] = static::annotations($options[Options::ANNOTATIONS] ?? []); |
30
|
|
|
|
31
|
8 |
|
$hydrator = new Hydrator($container, $options); |
32
|
|
|
|
33
|
8 |
|
self::preheat($hydrator, $options); |
34
|
|
|
|
35
|
8 |
|
return $hydrator; |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
protected static function annotations(array $annotations): array |
39
|
|
|
{ |
40
|
8 |
|
foreach (static::ANNOTATIONS as $annotation => $handler) { |
41
|
8 |
|
if (isset($annotations[$annotation])) { |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
8 |
|
$annotations[$annotation] = $handler; |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
return $annotations; |
49
|
|
|
} |
50
|
|
|
|
51
|
8 |
|
protected static function preheat(Hydrator $hydrator, array $options) |
52
|
|
|
{ |
53
|
8 |
|
$hasNamespaceDir = isset($options[Options::NAMESPACE_DIR]); |
54
|
|
|
$hasNamespace = isset($options[Options::NAMESPACE]); |
55
|
8 |
|
if (!$hasNamespaceDir || !$hasNamespace) { |
56
|
8 |
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$hydrator->preheat($options[Options::NAMESPACE_DIR], $options[Options::NAMESPACE]); |
60
|
1 |
|
} |
61
|
|
|
} |
62
|
|
|
|