[go: up one dir, main page]

Menu

[3822d7]: / core / Service.php  Maximize  Restore  History

Download this file

273 lines (235 with data), 7.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace ICT\Core;
/* * ***************************************************************
* Copyright © 2015 ICT Innovations Pakistan All Rights Reserved *
* Developed By: Nasir Iqbal *
* Website : http://www.ictinnovations.com/ *
* Mail : nasir@ictinnovations.com *
* *************************************************************** */
class Service
{
/** @const */
const SERVICE_FLAG = 0;
const SERVICE_TYPE = 'service';
const CONTACT_FIELD = 'phone';
const MESSAGE_CLASS = 'Message';
const GATEWAY_CLASS = 'Gateway';
const STATUS_READY = 0;
const STATUS_NEED_RELOAD = 1;
public function __construct()
{
// nothing to do
}
public static function capabilities()
{
$capabilities = array();
$capabilities['application'] = array(
'dial',
'message'
);
$capabilities['account'] = array();
$capabilities['provider'] = array();
return $capabilities;
}
public function is_supported($feature, $type = 'application')
{
$capabilities = $this->capabilities();
switch ($type) {
case 'application':
case 'account':
case 'provider':
default:
if (isset($capabilities[$type]) && in_array($feature, $capabilities[$type])) {
return TRUE;
} else {
return FALSE;
}
break;
}
}
/**
* ********************************************* Services related function **
*/
public static function _load()
{
static $serviceMap = null;
if (empty($serviceMap)) {
// manually load all available service classes
include_once_directory('Service');
$listService = list_available_classes('ICT\\Core\\Service');
foreach ($listService as $serviceClass) {
$flag = $serviceClass::SERVICE_FLAG;
$serviceMap[$flag] = $serviceClass;
}
}
return $serviceMap;
}
/**
* Get all available services
*
* @return Service[]
*/
public static function load_all()
{
$serviceMap = self::_load();
$serviceList = array();
foreach ($serviceMap as $className) {
$serviceList[] = new $className;
}
return $serviceList;
}
/**
* Get Service object by service_flag
* @param int $service_flag
*
* @return null|Service
*/
public static function load($service_flag)
{
$serviceMap = self::_load();
if (isset($serviceMap[$service_flag])) {
$className = $serviceMap[$service_flag];
$oService = new $className;
return $oService;
}
return null;
}
/**
* ******************************************* Default Gateway for service **
*/
public static function get_gateway() {
static $oGateway = NULL;
if (empty($oGateway)) {
$oGateway = new Gateway();
}
return $oGateway;
}
public static function get_route() {
$aFilter = array(
'active' => 1,
'service_flag' => static::SERVICE_FLAG
);
$listRoute = Provider::search($aFilter);
if (count($listRoute)) {
$aProvider = array_shift($listRoute);
$oProvider = Provider::load($aProvider['provider_id']);
return $oProvider;
}
$oGateway = static::get_gateway();
$oProvider = $oGateway->default_route();
if ($oProvider) {
Corelog::log('Using default gateway', Corelog::NOTICE);
return $oProvider;
}
throw new CoreException('404', 'No provider available');
}
/**
* ******************************************* Default message for service **
*/
public static function get_message() {
static $oMessage = NULL;
if (empty($oMessage)) {
$oMessage = new Message();
}
return $oMessage;
}
/**
* ***************************************** Application related functions **
*/
public static function template_path($template_name)
{
Corelog::log("Service->template_path demo. name: $template_name", Corelog::WARNING);
}
public function application_execute(Application $oApplication, $command = '', $command_type = 'string')
{
if (empty($command)) {
return;
}
// some applications require to provide last / disconnect application id
// to collect call status
if (($oApplication->defaultSetting & Application::REQUIRE_END_APPLICATION) == Application::REQUIRE_END_APPLICATION) {
// locate and assign end application id
$appList = $oApplication->search($oApplication->program_id, Application::ORDER_END);
foreach ($appList as $disconnectApp) {
$oApplication->disconnect_application_id = $disconnectApp['application_id'];
break; // only first
}
}
// initilize token cache
$oToken = new Token(Token::SOURCE_ALL);
$oToken->add('application', $oApplication);
// Some application require active provider to access / dial external extensions
if (($oApplication->defaultSetting & Application::REQUIRE_PROVIDER) == Application::REQUIRE_PROVIDER) {
// load provider
$oProvider = static::get_route();
$oToken->add('provider', $oProvider);
}
// Some application require immediate execution via gateway, instead of http response
if (($oApplication->defaultSetting & Application::REQUIRE_GATEWAY) == Application::REQUIRE_GATEWAY) {
// send it via gateway
$oGateway = $this->get_gateway();
$command = $oToken->render($command, $command_type); // render tokens
$oGateway->send($command, $oProvider);
} else { // default method is http response
// put it in response cache
$oSession = Session::get_instance();
$oSession->response->application_id = 'app_' . $oApplication->application_id;
$command = $oToken->render($command, $command_type); // render tokens
$oSession->response->application_data = $command;
}
}
/**
* *************************************** Configuration related functions **
*/
public static function config_status($new_status = null)
{
$status_variable = 'service:' . static::SERVICE_TYPE . '_status';
$current_status = Conf::get($status_variable, static::STATUS_READY);
if ($new_status === null) {
return $current_status;
}
$reference = array(
'class' => Conf::SYSTEM,
'node_id' => Conf::get('node:node_id', null)
);
Conf::set($status_variable, $new_status, true, $reference, Conf::PERMISSION_NODE_WRITE);
return Conf::get($status_variable, static::STATUS_READY);
}
public function config_update()
{
$status = $this->config_status();
if (($status & static::STATUS_NEED_RELOAD) == static::STATUS_NEED_RELOAD) {
$this->config_update_reload();
$this->config_status(static::STATUS_READY);
}
}
public function config_update_account(Account $oAccount)
{
Corelog::log("Service->config_update_account demo. account_id: " . $oAccount->account_id, Corelog::WARNING);
}
public function config_update_user(User $oUser)
{
Corelog::log("Service->config_update_user demo. user_id: " . $oUser->user_id, Corelog::WARNING);
}
public function config_update_provider(Provider $oProvider)
{
Corelog::log("Service->config_update_provider demo. provider_id: " . $oProvider->provider_id, Corelog::WARNING);
}
public function config_update_reload()
{
$oGateway = $this->get_gateway();
$oGateway->config_reload();
}
protected function config_save($config_type, Token $oToken, $config_name = 'default')
{
$template = $this->template_path($config_type);
$aSetting = $oToken->render_template($template);
$oGateway = $this->get_gateway();
$oGateway->config_save($config_type, $config_name, $aSetting);
}
protected function config_delete($config_type, $config_name = 'default')
{
$oGateway = $this->get_gateway();
$oGateway->config_delete($config_type, $config_name);
}
}