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
|
|
|
public function register(): void |
32
|
|
|
{ |
33
|
|
|
$this->registerManager(); |
34
|
|
|
$this->registerSessionManager(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Boot application services. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function boot(): void |
43
|
|
|
{ |
44
|
|
|
/** @var Manager $manager */ |
45
|
|
|
$manager = $this->app['conversation']; |
46
|
|
|
|
47
|
|
|
foreach ($this->intents as $intent) { |
48
|
|
|
$manager->registerIntent($intent); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$manager->registerFallbackIntent($this->fallbackIntent); |
52
|
|
|
|
53
|
|
|
if (method_exists($this, 'configure')) { |
54
|
|
|
$this->configure(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Load intents from path. |
60
|
|
|
* |
61
|
|
|
* @param string $path |
62
|
|
|
*/ |
63
|
|
|
protected function load(string $path): void |
64
|
|
|
{ |
65
|
|
|
$namespace = $this->app->getNamespace(); |
66
|
|
|
|
67
|
|
|
/** @var SplFileInfo[] $files */ |
68
|
|
|
$files = (new Finder)->in($path)->files(); |
69
|
|
|
|
70
|
|
|
foreach ($files as $file) { |
71
|
|
|
$file = $namespace.str_replace( |
72
|
|
|
['/', '.php'], |
73
|
|
|
['\\', ''], |
74
|
|
|
Str::after($file->getPathname(), app_path().DIRECTORY_SEPARATOR) |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
if (is_subclass_of($file, Intent::class) && !(new ReflectionClass($file))->isAbstract()) { |
|
|
|
|
78
|
|
|
$this->app['conversation']->registerIntent($file); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function registerManager(): void |
84
|
|
|
{ |
85
|
|
|
$this->app->singleton('conversation', function () { |
86
|
|
|
return new ConversationManager; |
87
|
|
|
}); |
88
|
|
|
|
89
|
|
|
$this->app->alias('conversation', Manager::class); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function registerSessionManager(): void |
93
|
|
|
{ |
94
|
|
|
$this->app->singleton(SessionManager::class, function () { |
95
|
|
|
return new SessionManager($this->app, resolve(Cache::class)); |
96
|
|
|
}); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.