Completed
Push — master ( 78679a...aac6ad )
by Freek
11s
created

getCachedEventHandlers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
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
    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.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
110
    {
111
        $cachedEventHandlersPath = config('event-projector.cache_path').'/event-handlers.php';
112
113
        if (! file_exists($cachedEventHandlersPath)) {
114
            return null;
115
        }
116
117
        return require $cachedEventHandlersPath;
118
    }
119
}
120