-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.php
182 lines (150 loc) · 6.77 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
<?php
declare(strict_types=1);
namespace Building\App;
use Building\Domain\Aggregate\Building;
use Building\Domain\Command;
use Building\Domain\Repository\BuildingRepositoryInterface;
use Building\Infrastructure\Repository\BuildingRepository;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\SchemaException;
use Interop\Container\ContainerInterface;
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 Zend\ServiceManager\ServiceManager;
require_once __DIR__ . '/vendor/autoload.php';
return new ServiceManager([
'factories' => [
Connection::class => 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 => function (ContainerInterface $container) {
$eventBus = new EventBus();
$eventStore = new EventStore(
new DoctrineEventStoreAdapter(
$container->get(Connection::class),
new FQCNMessageFactory(),
new NoOpMessageConverter(),
new JsonPayloadSerializer()
),
new ProophActionEventEmitter()
);
$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)
{
$dispatcher->attachListener(MessageBus::EVENT_ROUTE, [$this, 'onRoute']);
}
public function detach(ActionEventEmitter $dispatcher)
{
throw new \BadMethodCallException('Not implemented');
}
public function onRoute(ActionEvent $actionEvent)
{
$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 => function (ContainerInterface $container) : CommandBus {
$commandBus = new CommandBus();
$commandBus->utilize(new ServiceLocatorPlugin($container));
$commandBus->utilize(new class implements ActionEventListenerAggregate {
public function attach(ActionEventEmitter $dispatcher)
{
$dispatcher->attachListener(MessageBus::EVENT_ROUTE, [$this, 'onRoute']);
}
public function detach(ActionEventEmitter $dispatcher)
{
throw new \BadMethodCallException('Not implemented');
}
public function onRoute(ActionEvent $actionEvent)
{
$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!)
Command\RegisterNewBuilding::class => function (ContainerInterface $container) : callable {
$buildings = $container->get(BuildingRepositoryInterface::class);
return function (Command\RegisterNewBuilding $command) use ($buildings) {
$buildings->store(Building::new($command->name()));
};
},
BuildingRepositoryInterface::class => function (ContainerInterface $container) : BuildingRepositoryInterface {
return new BuildingRepository(
new AggregateRepository(
$container->get(EventStore::class),
AggregateType::fromAggregateRootClass(Building::class),
new AggregateTranslator()
)
);
},
],
]);