|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiClients\Tools\CommandBus; |
|
4
|
|
|
|
|
5
|
|
|
use Composed\Package; |
|
6
|
|
|
use function Composed\packages; |
|
7
|
|
|
use League\Tactician\Container\ContainerLocator; |
|
8
|
|
|
use League\Tactician\Handler\CommandHandlerMiddleware; |
|
9
|
|
|
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
|
10
|
|
|
use League\Tactician\Handler\MethodNameInflector\HandleInflector; |
|
11
|
|
|
use Psr\Container\ContainerInterface; |
|
12
|
|
|
use React\EventLoop\LoopInterface; |
|
13
|
|
|
use function WyriHaximus\getIn; |
|
14
|
|
|
use function WyriHaximus\iteratorOrArrayToArray; |
|
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
|
1 |
|
$commandToHandlerMap = iteratorOrArrayToArray($commandToHandlerMap); |
|
22
|
|
|
|
|
23
|
1 |
|
$containerLocator = new ContainerLocator( |
|
24
|
1 |
|
$container, |
|
25
|
1 |
|
$commandToHandlerMap |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
1 |
|
$commandHandlerMiddleware = new CommandHandlerMiddleware( |
|
29
|
1 |
|
new ClassNameExtractor(), |
|
30
|
1 |
|
$containerLocator, |
|
31
|
1 |
|
new HandleInflector() |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
1 |
|
return new CommandBus( |
|
35
|
1 |
|
$container->get(LoopInterface::class), |
|
36
|
1 |
|
$commandHandlerMiddleware |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
private static function resolveMapping(?string $cacheFile): iterable |
|
41
|
|
|
{ |
|
42
|
1 |
|
yield from Mapping::resolve(self::resolveDirectories(), $cacheFile); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
private static function resolveDirectories() |
|
46
|
|
|
{ |
|
47
|
|
|
/** @var Package $package */ |
|
48
|
1 |
|
foreach (packages() as $package) { |
|
49
|
1 |
|
$config = $package->getConfig('extra'); |
|
50
|
|
|
|
|
51
|
1 |
|
if ($config === null) { |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
$mapping = getIn($config, 'api-clients.command-bus'); |
|
56
|
|
|
|
|
57
|
1 |
|
if ($mapping === null) { |
|
58
|
1 |
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
yield $package->getPath($mapping['path']) => $mapping['namespace']; |
|
62
|
|
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|