1 | <?php |
||
16 | final class EventProjectorServiceProvider extends ServiceProvider |
||
17 | { |
||
18 | public function boot(): void |
||
19 | { |
||
20 | if ($this->app->runningInConsole()) { |
||
21 | $this->publishes([ |
||
22 | __DIR__.'/../config/event-projector.php' => config_path('event-projector.php'), |
||
23 | ], 'config'); |
||
24 | } |
||
25 | |||
26 | if (! class_exists('CreateStoredEventsTable')) { |
||
27 | $this->publishes([ |
||
28 | __DIR__.'/../stubs/create_stored_events_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_stored_events_table.php'), |
||
29 | ], 'migrations'); |
||
30 | } |
||
31 | |||
32 | Event::subscribe(EventSubscriber::class); |
||
33 | |||
34 | $this->discoverEventHandlers(); |
||
35 | } |
||
36 | |||
37 | public function register(): void |
||
38 | { |
||
39 | $this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector'); |
||
40 | |||
41 | $this->app->singleton(Projectionist::class, function () { |
||
42 | $config = config('event-projector'); |
||
43 | |||
44 | $projectionist = new Projectionist($config); |
||
45 | |||
46 | $projectionist |
||
47 | ->addProjectors($config['projectors'] ?? []) |
||
48 | ->addReactors($config['reactors'] ?? []); |
||
49 | |||
50 | return $projectionist; |
||
51 | }); |
||
52 | |||
53 | $this->app->alias(Projectionist::class, 'event-projector'); |
||
54 | |||
55 | $this->app->singleton(EventSubscriber::class, function () { |
||
56 | return new EventSubscriber(config('event-projector.stored_event_model')); |
||
57 | }); |
||
58 | |||
59 | $this->app |
||
60 | ->when(ReplayCommand::class) |
||
61 | ->needs('$storedEventModelClass') |
||
62 | ->give(config('event-projector.stored_event_model')); |
||
63 | |||
64 | $this->app->bind(EventSerializer::class, config('event-projector.event_serializer')); |
||
65 | |||
66 | $this->bindCommands(); |
||
67 | } |
||
68 | |||
69 | private function bindCommands() |
||
89 | |||
90 | private function discoverEventHandlers() |
||
91 | { |
||
92 | $projectionist = app(Projectionist::class); |
||
93 | |||
94 | $cachedEventHandlers = $this->getCachedEventHandlers(); |
||
95 | |||
96 | if (! is_null($cachedEventHandlers)) { |
||
97 | $projectionist->addEventHandlers($cachedEventHandlers); |
||
98 | |||
99 | return; |
||
100 | } |
||
101 | |||
102 | (new DiscoverEventHandlers()) |
||
103 | ->within(config('event-projector.auto_discover_projectors_and_reactors')) |
||
104 | ->useBasePath(base_path()) |
||
105 | ->ignoringFiles(Composer::getAutoloadedFiles(base_path('composer.json'))) |
||
106 | ->addToProjectionist($projectionist); |
||
107 | } |
||
108 | |||
109 | private function getCachedEventHandlers(): ?array |
||
119 | } |
||
120 |