1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Events\CommandLocatorEvent; |
6
|
|
|
use ApiClients\Foundation\Events\ServiceLocatorEvent; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Factory as HydratorFactory; |
8
|
|
|
use ApiClients\Foundation\Hydrator\Hydrator; |
9
|
|
|
use ApiClients\Foundation\Transport\Client as TransportClient; |
10
|
|
|
use ApiClients\Foundation\Transport\Factory as TransportFactory; |
11
|
|
|
use ApiClients\Tools\CommandBus\CommandBus; |
12
|
|
|
use Generator; |
13
|
|
|
use Interop\Container\ContainerInterface; |
14
|
|
|
use League\Container\Container; |
15
|
|
|
use League\Container\ReflectionContainer; |
16
|
|
|
use League\Event\Emitter; |
17
|
|
|
use League\Event\EmitterInterface; |
18
|
|
|
use League\Tactician\Container\ContainerLocator; |
19
|
|
|
use League\Tactician\Handler\CommandHandlerMiddleware; |
20
|
|
|
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
21
|
|
|
use League\Tactician\Handler\MethodNameInflector\HandleInflector; |
22
|
|
|
use React\EventLoop\LoopInterface; |
23
|
|
|
|
24
|
|
|
final class Factory |
25
|
|
|
{ |
26
|
|
|
public static function create( |
27
|
|
|
LoopInterface $loop = null, |
28
|
|
|
ContainerInterface $wrappedContainer = null, |
29
|
|
|
array $options = [] |
30
|
|
|
): Client { |
31
|
|
|
$container = self::createContainer($wrappedContainer); |
32
|
|
|
|
33
|
|
|
$container->share(EmitterInterface::class, new Emitter()); |
34
|
|
|
$container->share(TransportClient::class, self::createTransport($container, $loop, $options)); |
35
|
|
|
$container->share(Hydrator::class, self::createHydrator($container, $options)); |
36
|
|
|
$container->share(CommandBus::class, function () use ($container) { |
37
|
|
|
return self::createCommandBus($container); |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
foreach (self::locateServices($container->get(EmitterInterface::class)) as $service) { |
41
|
|
|
$container->share($service); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return new Client( |
45
|
|
|
$container |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private static function createContainer(ContainerInterface $wrappedContainer = null): Container |
50
|
|
|
{ |
51
|
|
|
$container = new Container(); |
52
|
|
|
$container->delegate(new ReflectionContainer()); |
53
|
|
|
|
54
|
|
|
if ($wrappedContainer instanceof ContainerInterface) { |
55
|
|
|
$container->delegate($wrappedContainer); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $container; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private static function createCommandBus(ContainerInterface $container): CommandBus |
62
|
|
|
{ |
63
|
|
|
$commandToHandlerMap = self::mapCommandsToHandlers($container->get(EmitterInterface::class)); |
64
|
|
|
|
65
|
|
|
$containerLocator = new ContainerLocator( |
66
|
|
|
$container, |
67
|
|
|
$commandToHandlerMap |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$commandHandlerMiddleware = new CommandHandlerMiddleware( |
71
|
|
|
new ClassNameExtractor(), |
72
|
|
|
$containerLocator, |
73
|
|
|
new HandleInflector() |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return new CommandBus( |
77
|
|
|
$container->get(LoopInterface::class), |
78
|
|
|
$commandHandlerMiddleware |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private static function mapCommandsToHandlers(EmitterInterface $emitter): array |
83
|
|
|
{ |
84
|
|
|
return $emitter->emit(CommandLocatorEvent::create())->getMap(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private static function locateServices(EmitterInterface $emitter): Generator |
88
|
|
|
{ |
89
|
|
|
return $emitter->emit(ServiceLocatorEvent::create())->getMap(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private static function createTransport( |
93
|
|
|
ContainerInterface $container, |
94
|
|
|
LoopInterface $loop = null, |
95
|
|
|
array $options = [] |
96
|
|
|
): TransportClient { |
97
|
|
|
return TransportFactory::create($container, $loop, $options[Options::TRANSPORT_OPTIONS]); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private static function createHydrator(ContainerInterface $container, array $options = []) |
101
|
|
|
{ |
102
|
|
|
if (isset($options[Options::HYDRATOR]) && $options[Options::HYDRATOR] instanceof Hydrator) { |
103
|
|
|
return $options[Options::HYDRATOR]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (!isset($options[Options::HYDRATOR_OPTIONS])) { |
107
|
|
|
throw new \Exception('Missing Hydrator options'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return HydratorFactory::create($container, $options[Options::HYDRATOR_OPTIONS]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: