1
|
|
|
<?php |
2
|
|
|
namespace NeedleProject\LaravelRabbitMq\Providers; |
3
|
|
|
|
4
|
|
|
use Illuminate\Foundation\Application; |
5
|
|
|
use Illuminate\Support\ServiceProvider as LaravelServiceProvider; |
6
|
|
|
use NeedleProject\LaravelRabbitMq\Builder\ContainerBuilder; |
7
|
|
|
use NeedleProject\LaravelRabbitMq\Command\BaseConsumerCommand; |
8
|
|
|
use NeedleProject\LaravelRabbitMq\Command\DeleteAllCommand; |
9
|
|
|
use NeedleProject\LaravelRabbitMq\Command\SetupCommand; |
10
|
|
|
use NeedleProject\LaravelRabbitMq\Command\ListEntitiesCommand; |
11
|
|
|
use NeedleProject\LaravelRabbitMq\ConfigHelper; |
12
|
|
|
use NeedleProject\LaravelRabbitMq\ConsumerInterface; |
13
|
|
|
use NeedleProject\LaravelRabbitMq\Container; |
14
|
|
|
use NeedleProject\LaravelRabbitMq\Exception\LaravelRabbitMqException; |
15
|
|
|
use NeedleProject\LaravelRabbitMq\PublisherInterface; |
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ServiceProvider |
21
|
|
|
* |
22
|
|
|
* @package NeedleProject\LaravelRabbitMq\Providers |
23
|
|
|
* @author Adrian Tilita <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ServiceProvider extends LaravelServiceProvider |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Indicates if loading of the provider is deferred. |
29
|
|
|
* |
30
|
|
|
* @var bool |
31
|
|
|
*/ |
32
|
|
|
protected $defer = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Perform post-registration booting of services. |
36
|
|
|
* |
37
|
|
|
* @return void |
38
|
|
|
* @throws LaravelRabbitMqException |
39
|
|
|
*/ |
40
|
|
|
public function boot() |
41
|
|
|
{ |
42
|
|
|
$this->publishConfig(); |
43
|
|
|
$this->registerContainer(); |
44
|
|
|
$this->registerPublishers(); |
45
|
|
|
$this->registerConsumers(); |
46
|
|
|
$this->registerCommands(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Publish Config |
51
|
|
|
*/ |
52
|
|
|
private function publishConfig() |
53
|
|
|
{ |
54
|
|
|
$this->publishes([ |
55
|
|
|
realpath( |
56
|
|
|
dirname(__FILE__) |
57
|
|
|
) . '/../../config/laravel_rabbitmq.php' => config_path('laravel_rabbitmq.php'), |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create container and register binding |
63
|
|
|
*/ |
64
|
|
|
private function registerContainer() |
65
|
|
|
{ |
66
|
|
|
$config = config('laravel_rabbitmq', []); |
67
|
|
|
if (!is_array($config)) { |
68
|
|
|
throw new \RuntimeException( |
69
|
|
|
"Invalid configuration provided for LaravelRabbitMQ!" |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
$configHelper = new ConfigHelper(); |
73
|
|
|
$config = $configHelper->addDefaults($config); |
74
|
|
|
|
75
|
|
|
$this->app->singleton( |
76
|
|
|
Container::class, |
77
|
|
|
function () use ($config) { |
78
|
|
|
$container = new ContainerBuilder(); |
79
|
|
|
return $container->createContainer($config); |
80
|
|
|
} |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Register publisher bindings |
86
|
|
|
*/ |
87
|
|
|
private function registerPublishers() |
88
|
|
|
{ |
89
|
|
|
// Get "tagged" like Publisher |
90
|
|
|
$this->app->singleton(PublisherInterface::class, function (Application $application, $arguments) { |
91
|
|
|
/** @var Container $container */ |
92
|
|
|
$container = $application->make(Container::class); |
93
|
|
|
if (empty($arguments)) { |
94
|
|
|
throw new \RuntimeException("Cannot make Publisher. No publisher identifier provided!"); |
95
|
|
|
} |
96
|
|
|
$aliasName = $arguments[0]; |
97
|
|
|
return $container->getPublisher($aliasName); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Register consumer bindings |
103
|
|
|
*/ |
104
|
|
|
private function registerConsumers() |
105
|
|
|
{ |
106
|
|
|
// Get "tagged" like Consumers |
107
|
|
|
$this->app->singleton(ConsumerInterface::class, function (Application $application, $arguments) { |
108
|
|
|
/** @var Container $container */ |
109
|
|
|
$container = $application->make(Container::class); |
110
|
|
|
if (empty($arguments)) { |
111
|
|
|
throw new \RuntimeException("Cannot make Consumer. No consumer identifier provided!"); |
112
|
|
|
} |
113
|
|
|
$aliasName = $arguments[0]; |
114
|
|
|
|
115
|
|
|
if (!$container->hasConsumer($aliasName)) { |
116
|
|
|
throw new \RuntimeException("Cannot make Consumer.\nNo consumer with alias name {$aliasName} found!"); |
117
|
|
|
} |
118
|
|
|
/** @var LoggerAwareInterface $consumer */ |
119
|
|
|
$consumer = $container->getConsumer($aliasName); |
120
|
|
|
$consumer->setLogger($application->make(LoggerInterface::class)); |
121
|
|
|
return $consumer; |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Register commands |
127
|
|
|
*/ |
128
|
|
|
private function registerCommands() |
129
|
|
|
{ |
130
|
|
|
$this->commands([ |
131
|
|
|
SetupCommand::class, |
132
|
|
|
ListEntitiesCommand::class, |
133
|
|
|
BaseConsumerCommand::class, |
134
|
|
|
DeleteAllCommand::class |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|