1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Foundation\Providers; |
6
|
|
|
|
7
|
|
|
use ReflectionClass; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use FondBot\Conversation\Intent; |
10
|
|
|
use FondBot\Framework\Application; |
11
|
|
|
use Symfony\Component\Finder\Finder; |
12
|
|
|
use Illuminate\Support\ServiceProvider; |
13
|
|
|
use FondBot\Conversation\FallbackIntent; |
14
|
|
|
use FondBot\Conversation\SessionManager; |
15
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
16
|
|
|
use FondBot\Contracts\Conversation\Manager; |
17
|
|
|
use FondBot\Conversation\ConversationManager; |
18
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @property Application $app |
22
|
|
|
*/ |
23
|
|
|
class ConversationServiceProvider extends ServiceProvider |
24
|
|
|
{ |
25
|
|
|
protected $intents = []; |
26
|
|
|
protected $fallbackIntent = FallbackIntent::class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register application services. |
30
|
|
|
*/ |
31
|
83 |
|
public function register(): void |
32
|
|
|
{ |
33
|
83 |
|
$this->registerManager(); |
34
|
83 |
|
$this->registerSessionManager(); |
35
|
83 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Boot application services. |
39
|
|
|
*/ |
40
|
83 |
|
public function boot(): void |
41
|
|
|
{ |
42
|
|
|
/** @var Manager $manager */ |
43
|
83 |
|
$manager = $this->app['conversation']; |
44
|
|
|
|
45
|
83 |
|
foreach ($this->intents as $intent) { |
46
|
|
|
$manager->registerIntent($intent); |
47
|
|
|
} |
48
|
|
|
|
49
|
83 |
|
$manager->registerFallbackIntent($this->fallbackIntent); |
50
|
|
|
|
51
|
83 |
|
if (method_exists($this, 'configure')) { |
52
|
|
|
$this->configure(); |
|
|
|
|
53
|
|
|
} |
54
|
83 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Load intents from path. |
58
|
|
|
* |
59
|
|
|
* @param string $path |
60
|
|
|
*/ |
61
|
|
|
protected function load(string $path): void |
62
|
|
|
{ |
63
|
|
|
$namespace = $this->app->getNamespace(); |
64
|
|
|
|
65
|
|
|
/** @var SplFileInfo[] $files */ |
66
|
|
|
$files = (new Finder)->in($path)->files(); |
67
|
|
|
|
68
|
|
|
foreach ($files as $file) { |
69
|
|
|
$file = $namespace.str_replace( |
70
|
|
|
['/', '.php'], |
71
|
|
|
['\\', ''], |
72
|
|
|
Str::after($file->getPathname(), app_path().DIRECTORY_SEPARATOR) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if (is_subclass_of($file, Intent::class) && !(new ReflectionClass($file))->isAbstract()) { |
|
|
|
|
76
|
|
|
$this->app['conversation']->registerIntent($file); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function registerManager(): void |
82
|
|
|
{ |
83
|
83 |
|
$this->app->singleton('conversation', function () { |
84
|
83 |
|
return new ConversationManager; |
85
|
83 |
|
}); |
86
|
|
|
|
87
|
83 |
|
$this->app->alias('conversation', Manager::class); |
88
|
83 |
|
} |
89
|
|
|
|
90
|
|
|
private function registerSessionManager(): void |
91
|
|
|
{ |
92
|
83 |
|
$this->app->singleton(SessionManager::class, function () { |
93
|
|
|
return new SessionManager($this->app, resolve(Cache::class)); |
94
|
83 |
|
}); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.