1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\CommandBus; |
4
|
|
|
|
5
|
|
|
use Composed\Package; |
6
|
|
|
use League\Tactician\Container\ContainerLocator; |
7
|
|
|
use League\Tactician\Handler\CommandHandlerMiddleware; |
8
|
|
|
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
9
|
|
|
use League\Tactician\Handler\MethodNameInflector\HandleInflector; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use React\EventLoop\LoopInterface; |
12
|
|
|
use Traversable; |
13
|
|
|
use function Composed\packages; |
14
|
|
|
use function igorw\get_in; |
15
|
|
|
|
16
|
|
|
final class Factory |
17
|
|
|
{ |
18
|
1 |
|
public static function create(ContainerInterface $container, array $options = []) |
19
|
|
|
{ |
20
|
1 |
|
$commandToHandlerMap = self::resolveMapping($options[Options::COMMAND_HANDLER_MAP_CACHE_FILE] ?? null); |
21
|
|
|
|
22
|
1 |
|
if ($commandToHandlerMap instanceof Traversable) { |
23
|
1 |
|
$commandToHandlerMap = iterator_to_array($commandToHandlerMap); |
24
|
1 |
|
} |
25
|
|
|
|
26
|
|
|
$containerLocator = new ContainerLocator( |
27
|
1 |
|
$container, |
28
|
1 |
|
$commandToHandlerMap |
29
|
1 |
|
); |
30
|
1 |
|
|
31
|
|
|
$commandHandlerMiddleware = new CommandHandlerMiddleware( |
32
|
|
|
new ClassNameExtractor(), |
33
|
1 |
|
$containerLocator, |
34
|
1 |
|
new HandleInflector() |
35
|
1 |
|
); |
36
|
|
|
|
37
|
|
|
return new CommandBus( |
38
|
|
|
$container->get(LoopInterface::class), |
39
|
1 |
|
$commandHandlerMiddleware |
40
|
|
|
); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
1 |
|
private static function resolveMapping(?string $cacheFile): array |
44
|
1 |
|
{ |
45
|
|
|
$directories = []; |
46
|
1 |
|
/** @var Package $package */ |
47
|
1 |
|
foreach (packages() as $package) { |
48
|
|
|
$config = $package->getConfig('extra'); |
49
|
|
|
|
50
|
1 |
|
if ($config === null) { |
51
|
1 |
|
continue; |
52
|
|
|
} |
53
|
1 |
|
|
54
|
|
|
$mapping = get_in( |
55
|
|
|
$config, |
56
|
|
|
[ |
57
|
|
|
'api-clients', |
58
|
1 |
|
'command-bus', |
59
|
1 |
|
] |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
if ($mapping === null) { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
1 |
|
|
66
|
|
|
$directories[$package->getPath($mapping['path'])] = $mapping['namespace']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return Mapping::resolve($directories, $cacheFile); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|