1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot; |
6
|
|
|
|
7
|
|
|
use SplFileInfo; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use FondBot\Conversation\Intent; |
11
|
|
|
use FondBot\Channels\ChannelManager; |
12
|
|
|
use Symfony\Component\Finder\Finder; |
13
|
|
|
use Illuminate\Support\Facades\Route; |
14
|
|
|
use FondBot\Toolbelt\MakeIntentCommand; |
15
|
|
|
use Illuminate\Support\ServiceProvider; |
16
|
|
|
use FondBot\Toolbelt\ListDriversCommand; |
17
|
|
|
use FondBot\Toolbelt\ListIntentsCommand; |
18
|
|
|
use FondBot\Toolbelt\ListChannelsCommand; |
19
|
|
|
use Illuminate\Cache\Repository as Cache; |
20
|
|
|
use FondBot\Toolbelt\InstallDriverCommand; |
21
|
|
|
use FondBot\Toolbelt\MakeActivatorCommand; |
22
|
|
|
use FondBot\Toolbelt\MakeInteractionCommand; |
23
|
|
|
use FondBot\Conversation\ConversationManager; |
24
|
|
|
use FondBot\Contracts\Channels\Manager as ChannelManagerContract; |
25
|
|
|
|
26
|
|
|
class FondBotServiceProvider extends ServiceProvider |
27
|
|
|
{ |
28
|
|
|
public function register(): void |
29
|
|
|
{ |
30
|
|
|
$this->app->singleton(FondBot::class, function () { |
31
|
|
|
return new FondBot(); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
$this->registerChannelManager(); |
35
|
|
|
$this->registerConversationManager(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function boot(): void |
39
|
|
|
{ |
40
|
|
|
if ($this->app->runningInConsole()) { |
41
|
|
|
$this->commands([ |
42
|
|
|
MakeIntentCommand::class, |
43
|
|
|
MakeInteractionCommand::class, |
44
|
|
|
MakeActivatorCommand::class, |
45
|
|
|
ListDriversCommand::class, |
46
|
|
|
InstallDriverCommand::class, |
47
|
|
|
ListChannelsCommand::class, |
48
|
|
|
ListIntentsCommand::class, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
Route::prefix('fondbot') |
53
|
|
|
->middleware('fondbot.webhook') |
54
|
|
|
->group(function () { |
55
|
|
|
Route::get('/', 'FondBot\Foundation\Controller@index'); |
56
|
|
|
Route::get('/webhook/{channel}/{secret?}', 'FondBot\Foundation\Controller@webhook')->name('fondbot.webhook'); |
57
|
|
|
Route::post('/webhook/{channel}/{secret?}', 'FondBot\Foundation\Controller@webhook')->name('fondbot.webhook'); |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function registerChannelManager(): void |
62
|
|
|
{ |
63
|
|
|
$this->app->singleton(ChannelManagerContract::class, function () { |
64
|
|
|
$manager = new ChannelManager($this->app); |
65
|
|
|
|
66
|
|
|
$manager->register( |
67
|
|
|
collect(config('fondbot.channels')) |
68
|
|
|
->mapWithKeys(function (array $parameters, string $name) { |
69
|
|
|
return [$name => $parameters]; |
70
|
|
|
}) |
71
|
|
|
->toArray() |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
return $manager; |
75
|
|
|
}); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function registerConversationManager(): void |
79
|
|
|
{ |
80
|
|
|
$this->app->singleton('conversation', function () { |
81
|
|
|
$manager = new ConversationManager($this->app, $this->app[Cache::class]); |
82
|
|
|
|
83
|
|
|
$namespace = $this->app->getNamespace(); |
84
|
|
|
|
85
|
|
|
/** @var SplFileInfo[] $files */ |
86
|
|
|
$files = (new Finder())->in(config('fondbot.intents_path'))->files(); |
87
|
|
|
|
88
|
|
|
foreach ($files as $file) { |
89
|
|
|
$file = $namespace.str_replace( |
90
|
|
|
['/', '.php'], |
91
|
|
|
['\\', ''], |
92
|
|
|
Str::after($file->getPathname(), app_path().DIRECTORY_SEPARATOR) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
if (is_subclass_of($file, Intent::class) && !(new ReflectionClass($file))->isAbstract()) { |
96
|
|
|
$manager->registerIntent($file); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $manager; |
101
|
|
|
}); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|