1 | <?php |
||
17 | final class EventSourcingServiceProvider extends ServiceProvider |
||
18 | { |
||
19 | public function boot(): void |
||
20 | { |
||
21 | if ($this->app->runningInConsole()) { |
||
22 | $this->publishes([ |
||
23 | __DIR__.'/../config/event-sourcing.php' => config_path('event-sourcing.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-sourcing.php', 'event-sourcing'); |
||
41 | |||
42 | $this->app->singleton(Projectionist::class, function () { |
||
43 | $config = config('event-sourcing'); |
||
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-sourcing'); |
||
55 | |||
56 | $this->app->singleton(StoredEventRepository::class, config('event-sourcing.stored_event_repository')); |
||
57 | |||
58 | $this->app->singleton(EventSubscriber::class, fn() => new EventSubscriber(config('event-sourcing.stored_event_repository'))); |
||
|
|||
59 | |||
60 | $this->app |
||
61 | ->when(ReplayCommand::class) |
||
62 | ->needs('$storedEventModelClass') |
||
63 | ->give(config('event-sourcing.stored_event_repository')); |
||
64 | |||
65 | $this->app->bind(EventSerializer::class, config('event-sourcing.event_serializer')); |
||
66 | |||
67 | $this->bindCommands(); |
||
68 | } |
||
69 | |||
70 | private function bindCommands() |
||
71 | { |
||
72 | $this->app->bind('command.event-sourcing:list', ListCommand::class); |
||
73 | $this->app->bind('command.event-sourcing:replay', ReplayCommand::class); |
||
74 | $this->app->bind('command.event-sourcing:cache-event-handlers', CacheEventHandlersCommand::class); |
||
75 | $this->app->bind('command.event-sourcing:clear-event-handlers', ClearCachedEventHandlersCommand::class); |
||
76 | $this->app->bind('command.make:projector', MakeProjectorCommand::class); |
||
77 | $this->app->bind('command.make:reactor', MakeReactorCommand::class); |
||
78 | $this->app->bind('command.make:aggregate', MakeAggregateCommand::class); |
||
79 | $this->app->bind('command.make:storable-event', MakeStorableEventCommand::class); |
||
80 | |||
81 | $this->commands([ |
||
82 | 'command.event-sourcing:list', |
||
83 | 'command.event-sourcing:replay', |
||
84 | 'command.event-sourcing:cache-event-handlers', |
||
85 | 'command.event-sourcing:clear-event-handlers', |
||
86 | 'command.make:projector', |
||
87 | 'command.make:reactor', |
||
88 | 'command.make:aggregate', |
||
89 | 'command.make:storable-event', |
||
90 | ]); |
||
91 | } |
||
92 | |||
93 | private function discoverEventHandlers() |
||
94 | { |
||
95 | $projectionist = app(Projectionist::class); |
||
96 | |||
97 | $cachedEventHandlers = $this->getCachedEventHandlers(); |
||
98 | |||
99 | if (! is_null($cachedEventHandlers)) { |
||
100 | $projectionist->addEventHandlers($cachedEventHandlers); |
||
101 | |||
102 | return; |
||
103 | } |
||
104 | |||
105 | (new DiscoverEventHandlers()) |
||
106 | ->within(config('event-sourcing.auto_discover_projectors_and_reactors')) |
||
107 | ->useBasePath(base_path()) |
||
108 | ->ignoringFiles(Composer::getAutoloadedFiles(base_path('composer.json'))) |
||
109 | ->addToProjectionist($projectionist); |
||
110 | } |
||
111 | |||
112 | private function getCachedEventHandlers(): ?array |
||
113 | { |
||
114 | $cachedEventHandlersPath = config('event-sourcing.cache_path').'/event-handlers.php'; |
||
115 | |||
116 | if (! file_exists($cachedEventHandlersPath)) { |
||
117 | return null; |
||
118 | } |
||
119 | |||
120 | return require $cachedEventHandlersPath; |
||
121 | } |
||
122 | } |
||
123 |