1 | <?php |
||
16 | final class BusBuilder |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $busId; |
||
22 | |||
23 | /** |
||
24 | * @var string[] |
||
25 | */ |
||
26 | private $middlewareIds = []; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $methodInflectorId; |
||
32 | |||
33 | 111 | public function __construct(string $busId, string $methodInflector, array $middlewareIds) |
|
39 | |||
40 | 108 | public function id(): string |
|
44 | |||
45 | 81 | public function serviceId(): string |
|
49 | |||
50 | 81 | public function locatorServiceId() |
|
54 | |||
55 | 81 | public function commandHandlerMiddlewareId(): string |
|
59 | |||
60 | 75 | public function registerInContainer(ContainerBuilder $container, array $commandsToAccept) |
|
61 | { |
||
62 | 75 | $this->registerLocatorService($container, $commandsToAccept); |
|
63 | |||
64 | 75 | $container->setDefinition( |
|
65 | 75 | $this->commandHandlerMiddlewareId(), |
|
66 | 75 | new Definition( |
|
67 | 75 | CommandHandlerMiddleware::class, |
|
68 | [ |
||
69 | 75 | new Reference('tactician.handler.command_name_extractor.class_name'), |
|
70 | 75 | new Reference($this->locatorServiceId()), |
|
71 | 75 | new Reference($this->methodInflectorId), |
|
72 | ] |
||
73 | ) |
||
74 | ); |
||
75 | |||
76 | 75 | $container->setDefinition( |
|
77 | 75 | $this->serviceId(), |
|
78 | 75 | new Definition( |
|
79 | 75 | CommandBus::class, |
|
80 | [ |
||
81 | 75 | array_map( |
|
82 | function (string $id) { return new Reference($id); }, |
||
83 | 75 | $this->middlewareIds |
|
84 | ) |
||
85 | ] |
||
86 | ) |
||
87 | 75 | )->setPublic(true); |
|
88 | 75 | } |
|
89 | |||
90 | 75 | private function registerLocatorService(ContainerBuilder $container, $commandsToAccept) |
|
91 | { |
||
92 | // Leverage symfony/dependency-injection:^3.3 service locators |
||
93 | 75 | if (class_exists(ServiceLocator::class)) { |
|
94 | 75 | $definition = new Definition( |
|
95 | 75 | ContainerLocator::class, |
|
96 | 75 | [new Reference($this->registerHandlerServiceLocator($container, $commandsToAccept)), $commandsToAccept] |
|
97 | ); |
||
98 | } else { |
||
99 | $definition = new Definition( |
||
100 | ContainerBasedHandlerLocator::class, |
||
101 | [new Reference('service_container'), $commandsToAccept] |
||
102 | ); |
||
103 | } |
||
104 | |||
105 | 75 | $container->setDefinition($this->locatorServiceId(), $definition); |
|
106 | 75 | } |
|
107 | |||
108 | 75 | private function registerHandlerServiceLocator(ContainerBuilder $container, array $commandsToAccept): string |
|
126 | } |
||
127 |