1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\EventProjector; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Event; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
use Spatie\EventProjector\Console\ListCommand; |
8
|
|
|
use Spatie\EventProjector\Console\ReplayCommand; |
9
|
|
|
use Spatie\EventProjector\Console\MakeReactorCommand; |
10
|
|
|
use Spatie\EventProjector\Console\MakeAggregateCommand; |
11
|
|
|
use Spatie\EventProjector\Console\MakeProjectorCommand; |
12
|
|
|
use Spatie\EventProjector\Console\MakeStorableEventCommand; |
13
|
|
|
use Spatie\EventProjector\EventSerializers\EventSerializer; |
14
|
|
|
use Spatie\EventProjector\Console\CacheEventHandlersCommand; |
15
|
|
|
use Spatie\EventProjector\Console\ClearCachedEventHandlersCommand; |
16
|
|
|
|
17
|
|
|
final class EventProjectorServiceProvider extends ServiceProvider |
18
|
|
|
{ |
19
|
|
|
public function boot(): void |
20
|
|
|
{ |
21
|
|
|
if ($this->app->runningInConsole()) { |
22
|
|
|
$this->publishes([ |
23
|
|
|
__DIR__.'/../config/event-projector.php' => config_path('event-projector.php'), |
24
|
|
|
], 'config'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (! class_exists('CreateStoredEventsTable')) { |
28
|
|
|
$this->publishes([ |
29
|
|
|
__DIR__.'/../stubs/create_stored_events_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_stored_events_table.php'), |
30
|
|
|
], 'migrations'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
Event::subscribe(EventSubscriber::class); |
34
|
|
|
|
35
|
|
|
$this->discoverEventHandlers(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function register(): void |
39
|
|
|
{ |
40
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector'); |
41
|
|
|
|
42
|
|
|
$this->app->singleton(Projectionist::class, function () { |
43
|
|
|
$config = config('event-projector'); |
44
|
|
|
|
45
|
|
|
$projectionist = new Projectionist($config); |
46
|
|
|
|
47
|
|
|
$projectionist |
48
|
|
|
->addProjectors($config['projectors'] ?? []) |
49
|
|
|
->addReactors($config['reactors'] ?? []); |
50
|
|
|
|
51
|
|
|
return $projectionist; |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
$this->app->alias(Projectionist::class, 'event-projector'); |
55
|
|
|
|
56
|
|
|
$this->app->singleton(StoredEventRepository::class, config('event-projector.stored_event_repository')); |
57
|
|
|
|
58
|
|
|
$this->app->singleton(EventSubscriber::class, function () { |
59
|
|
|
return new EventSubscriber(config('event-projector.stored_event_repository')); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$this->app |
63
|
|
|
->when(ReplayCommand::class) |
64
|
|
|
->needs('$storedEventModelClass') |
65
|
|
|
->give(config('event-projector.stored_event_repository')); |
66
|
|
|
|
67
|
|
|
$this->app->bind(EventSerializer::class, config('event-projector.event_serializer')); |
68
|
|
|
|
69
|
|
|
$this->bindCommands(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function bindCommands() |
73
|
|
|
{ |
74
|
|
|
$this->app->bind('command.event-projector:list', ListCommand::class); |
75
|
|
|
$this->app->bind('command.event-projector:replay', ReplayCommand::class); |
76
|
|
|
$this->app->bind('command.event-projector:cache-event-handlers', CacheEventHandlersCommand::class); |
77
|
|
|
$this->app->bind('command.event-projector:clear-event-handlers', ClearCachedEventHandlersCommand::class); |
78
|
|
|
$this->app->bind('command.make:projector', MakeProjectorCommand::class); |
79
|
|
|
$this->app->bind('command.make:reactor', MakeReactorCommand::class); |
80
|
|
|
$this->app->bind('command.make:aggregate', MakeAggregateCommand::class); |
81
|
|
|
$this->app->bind('command.make:storable-event', MakeStorableEventCommand::class); |
82
|
|
|
|
83
|
|
|
$this->commands([ |
84
|
|
|
'command.event-projector:list', |
85
|
|
|
'command.event-projector:replay', |
86
|
|
|
'command.event-projector:cache-event-handlers', |
87
|
|
|
'command.event-projector:clear-event-handlers', |
88
|
|
|
'command.make:projector', |
89
|
|
|
'command.make:reactor', |
90
|
|
|
'command.make:aggregate', |
91
|
|
|
'command.make:storable-event', |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function discoverEventHandlers() |
96
|
|
|
{ |
97
|
|
|
$projectionist = app(Projectionist::class); |
98
|
|
|
|
99
|
|
|
$cachedEventHandlers = $this->getCachedEventHandlers(); |
100
|
|
|
|
101
|
|
|
if (! is_null($cachedEventHandlers)) { |
102
|
|
|
$projectionist->addEventHandlers($cachedEventHandlers); |
103
|
|
|
|
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
(new DiscoverEventHandlers()) |
108
|
|
|
->within(config('event-projector.auto_discover_projectors_and_reactors')) |
109
|
|
|
->useBasePath(base_path()) |
110
|
|
|
->ignoringFiles(Composer::getAutoloadedFiles(base_path('composer.json'))) |
111
|
|
|
->addToProjectionist($projectionist); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
private function getCachedEventHandlers(): ?array |
115
|
|
|
{ |
116
|
|
|
$cachedEventHandlersPath = config('event-projector.cache_path').'/event-handlers.php'; |
117
|
|
|
|
118
|
|
|
if (! file_exists($cachedEventHandlersPath)) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return require $cachedEventHandlersPath; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|