Version 1.0.0
This commit is contained in:
96
src/ServiceProvider.php
Normal file
96
src/ServiceProvider.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Diffhead\PHP\LaravelRabbitMQ;
|
||||
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Command\Consume;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Event\Broadcast;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Interface\EventMapper as EventMapperInterface;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Interface\Serializer as SerializerInterface;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Interface\Unserializer as UnserializerInterface;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Listener\PublishEvent;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Service\EventMapper;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Service\Serializer;
|
||||
use Diffhead\PHP\LaravelRabbitMQ\Service\Unserializer;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
|
||||
|
||||
class ServiceProvider extends LaravelServiceProvider
|
||||
{
|
||||
/**
|
||||
* @var array<int,string>
|
||||
*/
|
||||
private array $commands = [
|
||||
Consume::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array<string,array<int,string>>
|
||||
*/
|
||||
private array $listeners = [
|
||||
Broadcast::class => [
|
||||
PublishEvent::class,
|
||||
]
|
||||
];
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
$this->registerServices();
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->registerConfigsPublishment();
|
||||
|
||||
$this->registerCommands();
|
||||
$this->registerListeners();
|
||||
}
|
||||
|
||||
private function registerServices(): void
|
||||
{
|
||||
$this->app->bind(
|
||||
SerializerInterface::class,
|
||||
config('rabbitmq.message.serializer', Serializer::class)
|
||||
);
|
||||
|
||||
$this->app->bind(
|
||||
UnserializerInterface::class,
|
||||
config('rabbitmq.message.unserializer', Unserializer::class)
|
||||
);
|
||||
|
||||
$this->app->bind(
|
||||
EventMapperInterface::class,
|
||||
config('rabbitmq.event.mapper', EventMapper::class)
|
||||
);
|
||||
}
|
||||
|
||||
private function registerCommands(): void
|
||||
{
|
||||
$this->commands($this->commands);
|
||||
}
|
||||
|
||||
private function registerListeners(): void
|
||||
{
|
||||
foreach ($this->listeners as $event => $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
Event::listen($event, $listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function registerConfigsPublishment(): void
|
||||
{
|
||||
$this->publishes(
|
||||
[
|
||||
$this->configPath('config/rabbitmq.php') => config_path('rabbitmq.php'),
|
||||
],
|
||||
'config'
|
||||
);
|
||||
}
|
||||
|
||||
private function configPath(string $path): string
|
||||
{
|
||||
return sprintf('%s/../%s', __DIR__, $path);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user