1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chief\Bridge\Laravel; |
4
|
|
|
|
5
|
|
|
use Chief\Busses\SynchronousCommandBus; |
6
|
|
|
use Chief\Chief; |
7
|
|
|
use Chief\Decorators\CommandQueueingDecorator; |
8
|
|
|
use Chief\Resolvers\NativeCommandHandlerResolver; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
|
11
|
|
|
class LaravelServiceProvider extends ServiceProvider |
12
|
|
|
{ |
13
|
|
|
protected $defaultBus = 'Chief\Busses\SynchronousCommandBus'; |
14
|
|
|
|
15
|
|
|
public function register() |
16
|
|
|
{ |
17
|
|
|
$this->registerContainer(); |
18
|
|
|
|
19
|
|
|
$this->registerCommandHandlerResolver(); |
20
|
|
|
|
21
|
|
|
$this->registerEventDispatcher(); |
22
|
|
|
|
23
|
|
|
$this->registerBusses(); |
24
|
|
|
|
25
|
|
|
$this->registerChief(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Override me to easily add decorators |
30
|
|
|
* |
31
|
|
|
* @return array Array of \Chief\Decorator objects |
32
|
|
|
*/ |
33
|
|
|
protected function getDecorators() |
34
|
|
|
{ |
35
|
|
|
return []; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function registerContainer() |
39
|
|
|
{ |
40
|
|
|
$this->app->bind('Chief\Container', 'Chief\Bridge\Laravel\IlluminateContainer'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function registerCommandHandlerResolver() |
44
|
|
|
{ |
45
|
|
|
$this->app->bind('Chief\CommandHandlerResolver', function () { |
46
|
|
|
return new NativeCommandHandlerResolver($this->app->make('Chief\Container')); |
47
|
|
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function registerBusses() |
51
|
|
|
{ |
52
|
|
|
$this->registerSyncCommandBus(); |
53
|
|
|
$this->registerQueueCommandBus(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function registerSyncCommandBus() |
57
|
|
|
{ |
58
|
|
|
$this->app->bind('Chief\Busses\SynchronousCommandBus', function () { |
59
|
|
|
$resolver = $this->app->make('Chief\CommandHandlerResolver'); |
60
|
|
|
return new SynchronousCommandBus($resolver); |
61
|
|
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function registerQueueCommandBus() |
65
|
|
|
{ |
66
|
|
|
$this->app->bind('Chief\Busses\QueueingCommandBus', function () { |
67
|
|
|
$resolver = $this->app->make('Chief\CommandHandlerResolver'); |
68
|
|
|
return new CommandQueueingDecorator($this->app->make('Chief\Bridge\Laravel\IlluminateQueuer'), $resolver); |
69
|
|
|
}); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function registerEventDispatcher() |
73
|
|
|
{ |
74
|
|
|
$this->app->bind('Chief\Decorators\EventDispatcher', 'Chief\Bridge\Laravel\IlluminateEventDispatcher'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function registerChief() |
78
|
|
|
{ |
79
|
|
|
$this->app->bind('Chief\CommandBus', function () { |
80
|
|
|
$bus = $this->app->make($this->defaultBus); |
81
|
|
|
return new Chief($bus, $this->getDecorators()); |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|