Completed
Pull Request — master (#148)
by Freek
04:33
created

EventProjectorServiceProvider::bindCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\EventProjector;
4
5
use Illuminate\Support\Facades\Event;
6
use Illuminate\Support\ServiceProvider;
7
use Spatie\EventProjector\Console\CacheEventHandlersCommand;
8
use Spatie\EventProjector\Console\ClearCachedEventHandlersCommand;
9
use Spatie\EventProjector\Console\ReplayCommand;
10
use Spatie\EventProjector\Console\MakeReactorCommand;
11
use Spatie\EventProjector\Console\MakeAggregateCommand;
12
use Spatie\EventProjector\Console\MakeProjectorCommand;
13
use Spatie\EventProjector\Console\MakeStorableEventCommand;
14
use Spatie\EventProjector\EventSerializers\EventSerializer;
15
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
35
    public function register(): void
36
    {
37
        $this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector');
38
39
        $this->app->singleton(Projectionist::class, function () {
40
            $config = config('event-projector');
41
42
            $projectionist = new Projectionist($config);
43
44
            $projectionist
45
                ->addProjectors($config['projectors'] ?? [])
46
                ->addReactors($config['reactors'] ?? []);
47
48
            return $projectionist;
49
        });
50
51
        $this->app->alias(Projectionist::class, 'event-projector');
52
53
        $this->app->singleton(EventSubscriber::class, function () {
54
            return new EventSubscriber(config('event-projector.stored_event_model'));
55
        });
56
57
        $this->app
58
            ->when(ReplayCommand::class)
59
            ->needs('$storedEventModelClass')
60
            ->give(config('event-projector.stored_event_model'));
61
62
        $this->app->bind(EventSerializer::class, config('event-projector.event_serializer'));
63
64
        $this->bindCommands();
65
66
        $this->discoverEventHandlers();
67
    }
68
69
    private function bindCommands()
70
    {
71
        $this->app->bind('command.event-projector:replay', ReplayCommand::class);
72
        $this->app->bind('command.event-projector:cache-event-handlers', CacheEventHandlersCommand::class);
73
        $this->app->bind('command.event-projector:clear-event-handlers', ClearCachedEventHandlersCommand::class);
74
        $this->app->bind('command.make:projector', MakeProjectorCommand::class);
75
        $this->app->bind('command.make:reactor', MakeReactorCommand::class);
76
        $this->app->bind('command.make:aggregate', MakeAggregateCommand::class);
77
        $this->app->bind('command.make:storable-event', MakeStorableEventCommand::class);
78
79
        $this->commands([
80
            'command.event-projector:replay',
81
            'command.event-projector:cache-event-handlers',
82
            'command.event-projector:clear-event-handlers',
83
            'command.make:projector',
84
            'command.make:reactor',
85
            'command.make:aggregate',
86
            'command.make:storable-event',
87
        ]);
88
    }
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.discover_event_handlers_in_directories'))
104
            ->useBasePath(app_path())
105
            ->addToProjectionist($projectionist);
106
    }
107
108
    private function getCachedEventHandlers(): ?array
109
    {
110
        $cachedEventHandlersPath = config('event-projector.cache_path');
111
112
        if (! file_exists($cachedEventHandlersPath)) {
113
            return null;
114
        }
115
116
        return require $cachedEventHandlersPath;
117
    }
118
}
119