1
|
|
|
<?php |
2
|
|
|
namespace SmoothPhp\LaravelAdapter; |
3
|
|
|
|
4
|
|
|
use Illuminate\Contracts\Foundation\Application; |
5
|
|
|
use Illuminate\Database\DatabaseManager; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use SmoothPhp\CommandBus\CommandHandlerMiddleWare; |
8
|
|
|
use SmoothPhp\CommandBus\SimpleCommandBus; |
9
|
|
|
use SmoothPhp\CommandBus\SimpleCommandTranslator; |
10
|
|
|
use SmoothPhp\Contracts\CommandBus\CommandBus; |
11
|
|
|
use SmoothPhp\Contracts\EventBus\EventBus; |
12
|
|
|
use SmoothPhp\Contracts\EventDispatcher\EventDispatcher; |
13
|
|
|
use SmoothPhp\Contracts\EventStore\EventStore; |
14
|
|
|
use SmoothPhp\Contracts\Projections\ProjectionServiceProvider; |
15
|
|
|
use SmoothPhp\Contracts\Serialization\Serializer; |
16
|
|
|
use SmoothPhp\LaravelAdapter\CommandBus\LaravelCommandBusHandlerResolver; |
17
|
|
|
use SmoothPhp\LaravelAdapter\Console\BuildLaravelEventStore; |
18
|
|
|
use SmoothPhp\LaravelAdapter\Console\EventStoreBranchSwap; |
19
|
|
|
use SmoothPhp\LaravelAdapter\Console\ExportEventStore; |
20
|
|
|
use SmoothPhp\LaravelAdapter\Console\ImportEventStore; |
21
|
|
|
use SmoothPhp\LaravelAdapter\Console\RebuildProjectionsCommand; |
22
|
|
|
use SmoothPhp\LaravelAdapter\Console\RunProjectionCommand; |
23
|
|
|
use SmoothPhp\LaravelAdapter\EventStore\LaravelEventStore; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class ServiceProvider |
27
|
|
|
* @package SmoothPhp\LaravelAdapter |
28
|
|
|
* @author Simon Bennett <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class ServiceProvider extends \Illuminate\Support\ServiceProvider |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Register the service provider. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function register() |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
$configPath = __DIR__ . '/../config/cqrses.php'; |
42
|
|
|
$this->mergeConfigFrom($configPath, 'cqrses'); |
43
|
|
|
|
44
|
|
|
$app = $this->app; |
45
|
|
|
|
46
|
|
|
$this->registerCommandBus($app); |
47
|
|
|
|
48
|
|
|
$this->registerSerializer($app); |
49
|
|
|
$this->registerEventStore($app); |
50
|
|
|
|
51
|
|
|
$this->registerEventDispatcher($app); |
52
|
|
|
$this->registerEventBus($app); |
53
|
|
|
|
54
|
|
|
$this->commands( |
55
|
|
|
[ |
56
|
|
|
BuildLaravelEventStore::class, |
57
|
|
|
RebuildProjectionsCommand::class, |
58
|
|
|
EventStoreBranchSwap::class, |
59
|
|
|
ExportEventStore::class, |
60
|
|
|
ImportEventStore::class, |
61
|
|
|
RunProjectionCommand::class, |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function boot() |
68
|
|
|
{ |
69
|
|
|
$configPath = __DIR__ . '/../config/cqrses.php'; |
70
|
|
|
$this->publishes([$configPath => $this->getConfigPath()], 'config'); |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the config path |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function getConfigPath() |
80
|
|
|
{ |
81
|
|
|
return config_path('cqrses.php'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Publish the config file |
86
|
|
|
* |
87
|
|
|
* @param string $configPath |
88
|
|
|
*/ |
89
|
|
|
protected function publishConfig($configPath) |
90
|
|
|
{ |
91
|
|
|
$this->publishes([$configPath => config_path('cqrses.php')], 'config'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Application $app |
96
|
|
|
*/ |
97
|
|
|
protected function registerCommandBus(Application $app) |
98
|
|
|
{ |
99
|
|
|
if ($app['config']->get('cqrses.command_bus_enabled')) { |
100
|
|
|
$middlewareChain = []; |
101
|
|
|
|
102
|
|
|
foreach ($app['config']->get('cqrses.command_bus_middleware') as $middleware) { |
103
|
|
|
$app->singleton($middleware); |
104
|
|
|
$middlewareChain[] = $app->make($middleware); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$middlewareChain[] = new CommandHandlerMiddleWare( |
108
|
|
|
new SimpleCommandTranslator(), |
109
|
|
|
$app->make(LaravelCommandBusHandlerResolver::class) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
$this->app->singleton( |
114
|
|
|
CommandBus::class, |
115
|
|
|
function () use ($middlewareChain) { |
116
|
|
|
return new \SmoothPhp\CommandBus\CommandBus($middlewareChain); |
117
|
|
|
} |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Application $app |
124
|
|
|
*/ |
125
|
|
|
protected function registerSerializer(Application $app) |
126
|
|
|
{ |
127
|
|
|
$app->bind(Serializer::class, $app['config']->get('cqrses.serializer')); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Application $app |
132
|
|
|
*/ |
133
|
|
|
protected function registerEventStore(Application $app) |
134
|
|
|
{ |
135
|
|
|
if ($app['config']->get('cqrses.laravel_eventstore_enabled')) { |
136
|
|
|
$app->bind( |
137
|
|
|
EventStore::class, |
138
|
|
|
function (Application $application) { |
139
|
|
|
return new LaravelEventStore( |
140
|
|
|
$application->make(DatabaseManager::class), |
141
|
|
|
$application->make(Serializer::class), |
142
|
|
|
$application['config']->get('cqrses.eventstore_connection'), |
143
|
|
|
$application['config']->get('cqrses.eventstore_table') |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param Application $app |
152
|
|
|
*/ |
153
|
|
|
protected function registerEventBus(Application $app) |
154
|
|
|
{ |
155
|
|
|
$app->singleton( |
156
|
|
|
EventBus::class, |
157
|
|
|
function (Application $application) { |
158
|
|
|
$eventBus = $application->make($application['config']->get('cqrses.event_bus')); |
159
|
|
|
|
160
|
|
|
foreach ($application['config']->get('cqrses.event_bus_listeners') as $listener) { |
161
|
|
|
$eventBus->subscribe($application->make($listener)); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $eventBus; |
165
|
|
|
} |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $app |
171
|
|
|
*/ |
172
|
|
|
protected function registerEventDispatcher(Application $app) |
173
|
|
|
{ |
174
|
|
|
$app->singleton( |
175
|
|
|
EventDispatcher::class, |
176
|
|
|
function (Application $application) { |
177
|
|
|
/** @var EventDispatcher $dispatcher */ |
178
|
|
|
$dispatcher = $application->make($application['config']->get('cqrses.event_dispatcher')); |
179
|
|
|
|
180
|
|
|
foreach ($this->getProjectionEventSubscribers($application) as $subscriber) { |
181
|
|
|
$dispatcher->addSubscriber($subscriber); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $dispatcher; |
185
|
|
|
} |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param Application $app |
191
|
|
|
* @return \SmoothPhp\Contracts\EventDispatcher\Subscriber[]|Collection |
192
|
|
|
*/ |
193
|
|
|
protected function getProjectionEventSubscribers(Application $app) |
194
|
|
|
{ |
195
|
|
|
return collect($app['config']->get('cqrses.projections_service_providers'))->map( |
196
|
|
|
function ($projectionsServiceProvider) use ($app) { |
197
|
|
|
return $app->make($projectionsServiceProvider); |
198
|
|
|
} |
199
|
|
|
)->map( |
200
|
|
|
function (ProjectionServiceProvider $projectServiceProvider) use ($app) { |
201
|
|
|
return collect($projectServiceProvider->getProjections())->map( |
202
|
|
|
function ($projection) use ($app) { |
203
|
|
|
return $app->make($projection); |
204
|
|
|
} |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
)->collapse(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |