Completed
Pull Request — master (#148)
by Freek
01:36
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\ReplayCommand;
8
use Spatie\EventProjector\Console\MakeReactorCommand;
9
use Spatie\EventProjector\Console\MakeAggregateCommand;
10
use Spatie\EventProjector\Console\MakeProjectorCommand;
11
use Spatie\EventProjector\Console\MakeStorableEventCommand;
12
use Spatie\EventProjector\EventSerializers\EventSerializer;
13
use Spatie\EventProjector\Console\CacheEventHandlersCommand;
14
use Spatie\EventProjector\Console\ClearCachedEventHandlersCommand;
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
        $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
70
    private function bindCommands()
71
    {
72
        $this->app->bind('command.event-projector:replay', ReplayCommand::class);
73
        $this->app->bind('command.event-projector:cache-event-handlers', CacheEventHandlersCommand::class);
74
        $this->app->bind('command.event-projector:clear-event-handlers', ClearCachedEventHandlersCommand::class);
75
        $this->app->bind('command.make:projector', MakeProjectorCommand::class);
76
        $this->app->bind('command.make:reactor', MakeReactorCommand::class);
77
        $this->app->bind('command.make:aggregate', MakeAggregateCommand::class);
78
        $this->app->bind('command.make:storable-event', MakeStorableEventCommand::class);
79
80
        $this->commands([
81
            'command.event-projector:replay',
82
            'command.event-projector:cache-event-handlers',
83
            'command.event-projector:clear-event-handlers',
84
            'command.make:projector',
85
            'command.make:reactor',
86
            'command.make:aggregate',
87
            'command.make:storable-event',
88
        ]);
89
    }
90
91
    private function discoverEventHandlers()
92
    {
93
        $projectionist = app(Projectionist::class);
94
95
        $cachedEventHandlers = $this->getCachedEventHandlers();
96
97
        if (! is_null($cachedEventHandlers)) {
98
            $projectionist->addEventHandlers($cachedEventHandlers);
99
100
            return;
101
        }
102
103
        (new DiscoverEventHandlers())
104
            ->within(config('event-projector.auto_discover_projectors_and_reactors'))
105
            ->useBasePath(base_path())
106
            ->ignoringFiles(Composer::getAutoloadedFiles(base_path('composer.json')))
107
            ->addToProjectionist($projectionist);
108
    }
109
110
    private function getCachedEventHandlers(): ?array
111
    {
112
        $cachedEventHandlersPath = config('event-projector.cache_path');
113
114
        if (! file_exists($cachedEventHandlersPath)) {
115
            return null;
116
        }
117
118
        return require $cachedEventHandlersPath;
119
    }
120
}
121