/** * Remote procedure call. * * Execute api function. * * @param \stdObject $cdata * @return array * @throws \CB\Controller\Exception */ private function _doRpc($cdata) { $api = \CB\Config::getApiFunctions(); if (!isset($api[$cdata['action']])) { throw new \CB\Controller\Exception('Call to undefined action: ' . $cdata['action']); } $action = $cdata['action']; $a = $api[$action]; $this->_doAroundCalls($a['before'], $cdata); $method = $cdata['method']; $mdef = $a['methods'][$method]; if (!$mdef) { throw new \CB\Controller\Exception('Call to undefined method: ' . $method . ' on action $action'); } $this->_doAroundCalls($mdef['before'], $cdata); $r = array('type' => 'rpc', 'tid' => $cdata['tid'], 'action' => $action, 'method' => $method); // load api controller $Class = '\\CB\\Api\\' . $action; $Controller = $Class::getInstance(); // build function params if (isset($mdef['len'])) { $params = isset($cdata['data']) && is_array($cdata['data']) ? $cdata['data'] : array(); } else { $params = array($cdata['data']); } // prepare fields and values array_walk($params, function ($param) { (array) $param; }); // call api controller function $r['result'] = call_user_func_array(array($Controller, $method), $params); $this->_doAroundCalls($mdef['after'], $cdata, $r); $this->_doAroundCalls($a['after'], $cdata, $r); return $r; }
/** * Api info controller * * @access public * @return void */ public function __construct() { // convert API functions to Ext.Direct specs $actions = array(); $api = \CB\Config::getApiFunctions(); foreach ($api as $aname => &$a) { $methods = array(); foreach ($a['methods'] as $mname => &$m) { if (isset($m['len'])) { $md = array('name' => $mname, 'len' => $m['len']); } else { $md = array('name' => $mname, 'params' => $m['params']); } if (isset($m['formHandler']) && $m['formHandler']) { $md['formHandler'] = true; } $methods[] = $md; } $actions[$aname] = $methods; } // API specs $api = array('url' => \CB\Config::get('folder.root') . '/api/router/', 'namespace' => 'CB.api', 'type' => 'remoting', 'actions' => $actions); // send javascript headers header('Content-Type: text/javascript'); // init api $response = 'var CB = {}; CB.init = {};'; $response .= 'CB.init.API=' . json_encode($api) . ';'; // init config $config = $this->getApiController('Config')->read(); if ($config['success']) { $response .= 'CB.init.Config=' . json_encode($config['data']) . ';'; } // init session user $user = $this->getApiController('User')->readSession(); if ($user['success']) { $response .= 'CB.init.User='******'data']) . ';'; } /* $countries = $this->getApiController('Country')->read()['data']; $response .= 'CB.init.Countries=' . json_encode($countries) . ';'; $locations = $this->getApiController('Location')->read()['data']; $response .= 'CB.init.Locations=' . json_encode($locations) . ';'; $locationTypes = $this->getApiController('LocationType')->read()['data']; $response .= 'CB.init.LocationTypes=' . json_encode($locationTypes) . ';'; $gradeTypes = $this->getApiController('GradeTypes')->read()['data']; $response .= 'CB.init.GradeTypes=' . json_encode($gradeTypes) . ';'; */ echo $response; exit; die; }