1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot; |
6
|
|
|
|
7
|
|
|
use Illuminate\Filesystem\Cache; |
8
|
|
|
use FondBot\Events\MessageReceived; |
9
|
|
|
use FondBot\Channels\ChannelManager; |
10
|
|
|
use Illuminate\Support\ServiceProvider; |
11
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
12
|
|
|
use FondBot\Conversation\ConversationManager; |
13
|
|
|
use FondBot\Foundation\Listeners\HandleConversation; |
14
|
|
|
use FondBot\Contracts\Channels\Manager as ChannelManagerContract; |
15
|
|
|
use FondBot\Contracts\Conversation\Manager as ConversationManagerContract; |
16
|
|
|
|
17
|
|
|
class FondBotServiceProvider extends ServiceProvider |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Bootstrap any package services. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function boot(): void |
25
|
|
|
{ |
26
|
|
|
$this->registerConversationManager(); |
27
|
|
|
$this->registerChannelManager(); |
28
|
|
|
$this->registerEventListeners(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function registerConversationManager(): void |
32
|
|
|
{ |
33
|
|
|
$this->app->singleton(ConversationManagerContract::class, function () { |
34
|
|
|
return new ConversationManager($this->app, $this->app[Cache::class]); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$this->app->alias(ConversationManagerContract::class, ConversationManager::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function registerChannelManager(): void |
41
|
|
|
{ |
42
|
|
|
$this->app->singleton(ChannelManagerContract::class, function () { |
43
|
|
|
$channels = collect(config('fondbot.channels'))->mapWithKeys(function (array $parameters, string $name) { |
44
|
|
|
return [$name => $parameters]; |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
return new ChannelManager($this->app, $channels); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
$this->app->alias(ChannelManagerContract::class, ChannelManager::class); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function registerEventListeners(): void |
54
|
|
|
{ |
55
|
|
|
/** @var Dispatcher $events */ |
56
|
|
|
$events = $this->app['events']; |
57
|
|
|
|
58
|
|
|
$events->listen(MessageReceived::class, HandleConversation::class); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Register any application services. |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function register(): void |
67
|
|
|
{ |
68
|
|
|
if ($this->app->runningInConsole()) { |
69
|
|
|
$this->commands([ |
70
|
|
|
Console\MakeIntentCommand::class, |
71
|
|
|
Console\MakeInteractionCommand::class, |
72
|
|
|
Console\MakeActivatorCommand::class, |
73
|
|
|
Console\ListDriversCommand::class, |
74
|
|
|
Console\InstallDriverCommand::class, |
75
|
|
|
Console\ListChannelsCommand::class, |
76
|
|
|
Console\ListIntentsCommand::class, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$this->publishes([ |
80
|
|
|
__DIR__.'/../resources/stubs/FondBotServiceProvider.stub' => app_path('Providers/FondBotServiceProvider.php'), |
81
|
|
|
], 'fondbot-provider'); |
82
|
|
|
|
83
|
|
|
$this->publishes([ |
84
|
|
|
__DIR__.'/../config/fondbot.php' => config_path('fondbot.php'), |
85
|
|
|
], 'fondbot-config'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|