1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Core; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* JarvisPHP Main Class |
7
|
|
|
* @author Stefano Bianchini |
8
|
|
|
* @website http://www.stefanobianchini.net |
9
|
|
|
*/ |
10
|
|
|
class JarvisPHP { |
11
|
|
|
|
12
|
|
|
private static $active_plugins = array(); |
|
|
|
|
13
|
|
|
|
14
|
|
|
public static $slim = null; |
15
|
|
|
|
16
|
|
|
public static $TTS_name = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Bootstrap JarvisPHP core |
20
|
|
|
*/ |
21
|
|
|
public static function bootstrap() { |
22
|
|
|
//Autoloading classes |
23
|
|
|
spl_autoload_register(function($className) |
24
|
|
|
{ |
25
|
|
|
//Obtain the pure class name |
26
|
|
|
$pureClassName = JarvisPHP::getRealClassName($className); |
27
|
|
|
//Build the path |
28
|
|
|
$namespace = JarvisPHP::getNameSpace($className); |
29
|
|
|
if(file_exists($namespace.'/'.$pureClassName.'.php')) { |
30
|
|
|
include_once($namespace.'/'.$pureClassName.'.php'); |
31
|
|
|
} |
32
|
|
|
}); |
33
|
|
|
//Configure the Logger |
34
|
|
|
\Logger::configure('config/log4php.xml'); |
35
|
|
|
|
36
|
|
|
//Load config |
37
|
|
|
require 'config/Jarvis.php'; |
38
|
|
|
|
39
|
|
|
//Start session |
40
|
|
|
JarvisSession::start(); |
41
|
|
|
|
42
|
|
|
//Core localization |
43
|
|
|
JarvisLanguage::loadCoreTranslation(); |
44
|
|
|
JarvisPHP::getLogger()->debug('Loading "'._LANGUAGE.'" language file'); |
45
|
|
|
|
46
|
|
|
//Routing |
47
|
|
|
JarvisPHP::$slim = new \Slim\Slim(array('debug' => false)); |
48
|
|
|
|
49
|
|
|
//POST /answer route |
50
|
|
|
JarvisPHP::$slim->post('/answer/', function () { |
51
|
|
|
//Detect if the request forces a TTS |
52
|
|
|
$ttsFromPostRequest = JarvisPHP::$slim->request->post('tts'); |
53
|
|
|
if(!empty($ttsFromPostRequest) && file_exists('Speakers\\'.JarvisPHP::$slim->request->post('tts').'.php')) { |
54
|
|
|
$forcedTTS = JarvisPHP::$slim->request->post('tts'); |
55
|
|
|
} else { |
56
|
|
|
$forcedTTS = _JARVIS_TTS; |
57
|
|
|
} |
58
|
|
|
JarvisPHP::elaborateCommand(mb_strtolower(preg_replace("/\s+/", " ", JarvisPHP::$slim->request->post('command')), 'UTF-8'), $forcedTTS); |
59
|
|
|
}); |
60
|
|
|
|
61
|
|
|
//POST /answer route |
62
|
|
|
JarvisPHP::$slim->post('/say/', function () { |
63
|
|
|
JarvisPHP::say(mb_strtolower(preg_replace("/\s+/", " ", JarvisPHP::$slim->request->post('sentence')), 'UTF-8')); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
//Slim Framework Custom Error handler |
67
|
|
|
JarvisPHP::$slim->error(function (\Exception $e) { |
68
|
|
|
JarvisPHP::getLogger()->error('Code: '.$e->getCode().' - '.$e->getMessage().' in '.$e->getFile().' on line '.$e->getLine().''); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
JarvisPHP::$slim->run(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get Log4php object |
76
|
|
|
* @return Logger |
77
|
|
|
*/ |
78
|
|
|
public static function getLogger() { |
79
|
|
|
return \Logger::getLogger('JarvisPHP'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Load a plugin |
84
|
|
|
* @param string $plugin |
85
|
|
|
*/ |
86
|
|
|
public static function loadPlugin($plugin) { |
87
|
|
|
array_push(JarvisPHP::$active_plugins, 'JarvisPHP\Plugins\\'.$plugin.'\\'.$plugin); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns Jarvis Language setting |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public static function getLanguage() { |
95
|
|
|
return _LANGUAGE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Parse the command and execute the plugin |
100
|
|
|
* @param string $command |
101
|
|
|
*/ |
102
|
|
|
public static function elaborateCommand($command, $forcedTTS) { |
103
|
|
|
|
104
|
|
|
JarvisPHP::$TTS_name = $forcedTTS; |
105
|
|
|
|
106
|
|
|
//Jarvis tries to understand if the magic words that stop the session were pronounced |
107
|
|
|
if(preg_match(JarvisLanguage::translate('preg_match_magic_words_to_stop_session'),$command)) { |
108
|
|
|
JarvisSession::terminate(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
//Verify if there is an active plugin and the command session timeout |
112
|
|
|
if(JarvisSession::sessionInProgress() && (time() < (JarvisSession::get('last_command_timestamp')+_COMMAND_SESSION_TIMEOUT))) { |
113
|
|
|
JarvisPHP::getLogger()->debug('Detected active session: '.JarvisSession::getActivePlugin() . ' - last command '.JarvisSession::get('last_command_timestamp').', now is '.time()); |
114
|
|
|
$plugin_class = JarvisSession::getActivePlugin(); |
115
|
|
|
//Load plugin's languages |
116
|
|
|
JarvisLanguage::loadPluginTranslation($plugin_class); |
117
|
|
|
$plugin = new $plugin_class(); |
118
|
|
|
$plugin->answer($command); |
119
|
|
|
} |
120
|
|
|
else { |
121
|
|
|
//Clear all session variable |
122
|
|
|
JarvisSession::reset(); |
123
|
|
|
JarvisPHP::getLogger()->debug('Active session not detected or expired'); |
124
|
|
|
$max_priority_found=-9999; |
125
|
|
|
$choosen_plugin = null; |
126
|
|
|
//Cycling plugins |
127
|
|
|
foreach(JarvisPHP::$active_plugins as $plugin_class) { |
128
|
|
|
$plugin = new $plugin_class(); |
129
|
|
|
//Load plugin's languages |
130
|
|
|
JarvisLanguage::loadPluginTranslation($plugin_class); |
131
|
|
|
if($plugin->isLikely($command)) { |
132
|
|
|
JarvisPHP::getLogger()->debug('Maybe '.JarvisPHP::getRealClassName($plugin_class).', check priority'); |
133
|
|
|
if($plugin->getPriority() > $max_priority_found) { |
134
|
|
|
$max_priority_found = $plugin->getPriority(); |
135
|
|
|
$choosen_plugin = $plugin; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
if(!is_null($choosen_plugin)) { |
140
|
|
|
JarvisPHP::getLogger()->debug('Choosen plugin: '.JarvisPHP::getRealClassName(get_class($choosen_plugin))); |
141
|
|
|
if($choosen_plugin->hasSession()) { |
142
|
|
|
JarvisSession::setActivePlugin(get_class($choosen_plugin)); |
143
|
|
|
} |
144
|
|
|
$choosen_plugin->answer($command); |
145
|
|
|
} else { |
146
|
|
|
if(preg_match(JarvisLanguage::translate('preg_match_magic_words_to_stop_session'),$command)) { |
147
|
|
|
JarvisTTS::speak(JarvisLanguage::translate('response_to_magic_words_to_stop_session')); |
148
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse(JarvisLanguage::translate('response_to_magic_words_to_stop_session')); |
149
|
|
|
$response->send(); |
150
|
|
|
} |
151
|
|
|
else { |
152
|
|
|
JarvisBehaviourLanguage::loadBehaviourLanguage(); |
153
|
|
|
$answer = JarvisBehaviourLanguage::answer($command); |
154
|
|
|
if($answer) { |
155
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, 'none', true); |
156
|
|
|
JarvisTTS::speak($answer); |
157
|
|
|
} else { |
158
|
|
|
JarvisPHP::getLogger()->debug('No plugin found for command: '.$command); |
159
|
|
|
JarvisTTS::speak(JarvisLanguage::translate('core_command_not_understand')); |
160
|
|
|
//Log the command |
161
|
|
|
file_put_contents('notUnderstandCommands.log', '['.date('Y-m-d H:i:s').'] Command:'.$command. PHP_EOL , FILE_APPEND | LOCK_EX); |
162
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse(JarvisLanguage::translate('core_command_not_understand')); |
163
|
|
|
} |
164
|
|
|
$response->send(); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
//Update last command timestamp |
169
|
|
|
JarvisSession::set('last_command_timestamp', time()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Say the sentence |
174
|
|
|
* @param string $sentence |
175
|
|
|
*/ |
176
|
|
|
public static function say($sentence) { |
177
|
|
|
JarvisPHP::$TTS_name = _JARVIS_TTS; |
178
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($sentence, 'none', true); |
179
|
|
|
JarvisTTS::speak($sentence); |
180
|
|
|
$response->send(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public static function getRealClassName($fullClassName) { |
184
|
|
|
//Explode class name |
185
|
|
|
$classNameArray = explode('\\',$fullClassName); |
186
|
|
|
//Obtain the pure class name |
187
|
|
|
return end($classNameArray); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public static function getNameSpace($fullClassName) { |
191
|
|
|
//Explode class name |
192
|
|
|
$classNameArray = explode('\\',$fullClassName); |
193
|
|
|
//Remove the pure class name |
194
|
|
|
array_pop($classNameArray); |
195
|
|
|
//Remove the JarvisPHP main namespace |
196
|
|
|
array_shift($classNameArray); |
197
|
|
|
//Build the path |
198
|
|
|
$namespace = implode('/', $classNameArray); |
199
|
|
|
return $namespace; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
} //JarvisPHP |
This check marks private properties in classes that are never used. Those properties can be removed.