-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.php
196 lines (163 loc) · 7.5 KB
/
container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
declare(strict_types=1);
namespace Building\App;
use Building\Domain\Aggregate\Building;
use Building\Domain\Command;
use Building\Domain\Repository\Buildings;
use Building\Infrastructure\Repository\BuildingsFromAggregateRepository;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\SchemaException;
use Interop\Container\ContainerInterface as InteropContainer;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ActionEventListenerAggregate;
use Prooph\Common\Event\ProophActionEventEmitter;
use Prooph\Common\Messaging\FQCNMessageFactory;
use Prooph\Common\Messaging\NoOpMessageConverter;
use Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator;
use Prooph\EventStore\Adapter\Doctrine\DoctrineEventStoreAdapter;
use Prooph\EventStore\Adapter\Doctrine\Schema\EventStoreSchema;
use Prooph\EventStore\Adapter\PayloadSerializer\JsonPayloadSerializer;
use Prooph\EventStore\Aggregate\AggregateRepository;
use Prooph\EventStore\Aggregate\AggregateType;
use Prooph\EventStore\EventStore;
use Prooph\EventStoreBusBridge\EventPublisher;
use Prooph\EventStoreBusBridge\TransactionManager;
use Prooph\ServiceBus\CommandBus;
use Prooph\ServiceBus\EventBus;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\Plugin\ServiceLocatorPlugin;
use Psr\Container\ContainerInterface;
use Zend\ServiceManager\ServiceManager;
require_once __DIR__ . '/vendor/autoload.php';
return new ServiceManager([
'factories' => [
Connection::class => static function () {
$connection = DriverManager::getConnection([
'driverClass' => Driver::class,
'path' => __DIR__ . '/data/db.sqlite3',
]);
try {
$schema = $connection->getSchemaManager()->createSchema();
EventStoreSchema::createSingleStream($schema, 'event_stream', true);
foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) {
$connection->exec($sql);
}
} catch (SchemaException $ignored) {
}
return $connection;
},
EventStore::class => static function (ContainerInterface $container) {
$eventBus = new EventBus();
$eventStore = new EventStore(
new DoctrineEventStoreAdapter(
$container->get(Connection::class),
new FQCNMessageFactory(),
new NoOpMessageConverter(),
new JsonPayloadSerializer()
),
new ProophActionEventEmitter()
);
// This listener forwards events to a listener service called exactly like:
//
// * `get_class($event) . '-listeners'.
// * `get_class($event) . '-projectors'.
//
// Please note that the service must be a {{@see callable[]}}
$eventBus->utilize(new class ($container, $container) implements ActionEventListenerAggregate
{
/**
* @var ContainerInterface
*/
private $eventHandlers;
/**
* @var ContainerInterface
*/
private $projectors;
public function __construct(
ContainerInterface $eventHandlers,
ContainerInterface $projectors
) {
$this->eventHandlers = $eventHandlers;
$this->projectors = $projectors;
}
public function attach(ActionEventEmitter $dispatcher) : void
{
$dispatcher->attachListener(MessageBus::EVENT_ROUTE, [$this, 'onRoute']);
}
public function detach(ActionEventEmitter $dispatcher) : void
{
throw new \BadMethodCallException('Not implemented');
}
public function onRoute(ActionEvent $actionEvent) : void
{
$messageName = (string) $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME);
$handlers = [];
$listeners = $messageName . '-listeners';
$projectors = $messageName . '-projectors';
if ($this->projectors->has($projectors)) {
$handlers = array_merge($handlers, $this->eventHandlers->get($projectors));
}
if ($this->eventHandlers->has($listeners)) {
$handlers = array_merge($handlers, $this->eventHandlers->get($listeners));
}
if ($handlers !== []) {
$actionEvent->setParam(EventBus::EVENT_PARAM_EVENT_LISTENERS, $handlers);
}
}
});
(new EventPublisher($eventBus))->setUp($eventStore);
return $eventStore;
},
CommandBus::class => static function (InteropContainer $container) : CommandBus {
$commandBus = new CommandBus();
// Following configuration makes sure that commands are dispatched by command handler services
// called exactly like the command class name. The handler must be a {{@see callable}}.
$commandBus->utilize(new ServiceLocatorPlugin($container));
$commandBus->utilize(new class implements ActionEventListenerAggregate
{
public function attach(ActionEventEmitter $dispatcher) : void
{
$dispatcher->attachListener(MessageBus::EVENT_ROUTE, [$this, 'onRoute']);
}
public function detach(ActionEventEmitter $dispatcher) : void
{
throw new \BadMethodCallException('Not implemented');
}
public function onRoute(ActionEvent $actionEvent) : void
{
$actionEvent->setParam(
MessageBus::EVENT_PARAM_MESSAGE_HANDLER,
(string) $actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME)
);
}
});
$transactionManager = new TransactionManager();
$transactionManager->setUp($container->get(EventStore::class));
$commandBus->utilize($transactionManager);
return $commandBus;
},
// Command -> CommandHandlerFactory
// this is where most of the work will be done (by you!)
// Each command is a `function (CommandClass) : void`
Command\RegisterNewBuilding::class => static function (ContainerInterface $container) : callable {
$buildings = $container->get(Buildings::class);
return static function (Command\RegisterNewBuilding $command) use ($buildings) {
$buildings->add(Building::new($command->name()));
};
},
// Our concrete repository implementation
Buildings::class => static function (ContainerInterface $container
) : Buildings {
return new BuildingsFromAggregateRepository(
new AggregateRepository(
$container->get(EventStore::class),
AggregateType::fromAggregateRootClass(Building::class),
new AggregateTranslator()
)
);
},
],
]);