/** * @see RedBean_BeanHelper::getModelForBean */ public function getModelForBean(RedBean_OODBBean $bean) { $modelName = $this->helper->getModelName($bean->getMeta('type'), $bean); if (!class_exists($modelName)) { return NULL; } $obj = $this->helper->factory($modelName); $obj->loadBean($bean); return $obj; }
/** * Handles a JSON RPC 2 request to perform a custom operation on a bean. * * @param string $id request ID, identification for request * @param string $beanType type of the bean you want to store * @param string $action action you want to invoke on bean model * @param array $data data array * * @return string */ private function custom($id, $beanType, $action, $data) { $modelName = $this->modelHelper->getModelName($beanType); if (!class_exists($modelName)) { return $this->resp(NULL, $id, self::C_JSONRPC2_METHOD_NOT_FOUND, 'No such bean in the can!'); } $beanModel = new $modelName(); if (!method_exists($beanModel, $action)) { return $this->resp(NULL, $id, self::C_JSONRPC2_METHOD_NOT_FOUND, "Method not found in Bean: {$beanType} "); } return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id); }
/** * Processes a JSON object request. * * @param array $jsonObject JSON request object * * @return mixed $result result */ public function handleJSONRequest($jsonString) { //Decode JSON string $jsonArray = json_decode($jsonString, true); if (!$jsonArray) { return $this->resp(null, null, -32700, 'Cannot Parse JSON'); } if (!isset($jsonArray['jsonrpc'])) { return $this->resp(null, null, -32600, 'No RPC version'); } if ($jsonArray['jsonrpc'] != '2.0') { return $this->resp(null, null, -32600, 'Incompatible RPC Version'); } //DO we have an ID to identify this request? if (!isset($jsonArray['id'])) { return $this->resp(null, null, -32600, 'No ID'); } //Fetch the request Identification String. $id = $jsonArray['id']; //Do we have a method? if (!isset($jsonArray['method'])) { return $this->resp(null, $id, -32600, 'No method'); } //Do we have params? if (!isset($jsonArray['params'])) { $data = array(); } else { $data = $jsonArray['params']; } //Check method signature $method = explode(':', trim($jsonArray['method'])); if (count($method) != 2) { return $this->resp(null, $id, -32600, 'Invalid method signature. Use: BEAN:ACTION'); } //Collect Bean and Action $beanType = $method[0]; $action = $method[1]; //May not contain anything other than ALPHA NUMERIC chars and _ if (preg_match('/\\W/', $beanType)) { return $this->resp(null, $id, -32600, 'Invalid Bean Type String'); } if (preg_match('/\\W/', $action)) { return $this->resp(null, $id, -32600, 'Invalid Action String'); } try { switch ($action) { case 'store': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean Object'); } $data = $data[0]; if (!isset($data['id'])) { $bean = RedBean_Facade::dispense($beanType); } else { $bean = RedBean_Facade::load($beanType, $data['id']); } $bean->import($data); $rid = RedBean_Facade::store($bean); return $this->resp($rid, $id); case 'load': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); return $this->resp($bean->export(), $id); case 'trash': if (!isset($data[0])) { return $this->resp(null, $id, -32602, 'First param needs to be Bean ID'); } $bean = RedBean_Facade::load($beanType, $data[0]); RedBean_Facade::trash($bean); return $this->resp('OK', $id); default: $modelName = $this->modelHelper->getModelName($beanType); if (!class_exists($modelName)) { return $this->resp(null, $id, -32601, 'No such bean in the can!'); } $beanModel = new $modelName(); if (!method_exists($beanModel, $action)) { return $this->resp(null, $id, -32601, "Method not found in Bean: {$beanType} "); } return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id); } } catch (Exception $exception) { return $this->resp(null, $id, -32099, $exception->getCode() . '-' . $exception->getMessage()); } }
public function __call($method, $args) { if (!isset($this->__info["model"])) { $modelName = RedBean_ModelHelper::getModelName($this->getMeta("type")); if (!class_exists($modelName)) { return null; } $obj = new $modelName(); $obj->loadBean($this); $this->__info["model"] = $obj; } if (!method_exists($this->__info["model"], $method)) { return null; } return call_user_func_array(array($this->__info["model"], $method), $args); }
/** * Processes a JSON object request. * * @param array $jsonObject * @return mixed $result */ public function handleJSONRequest($jsonString) { $jsonArray = json_decode($jsonString, true); if (!$jsonArray) { return $this->resp(null, null, -32700, "Cannot Parse JSON"); } if (!isset($jsonArray["jsonrpc"])) { return $this->resp(null, null, -32600, "No RPC version"); } if ($jsonArray["jsonrpc"] != "2.0") { return $this->resp(null, null, -32600, "Incompatible RPC Version"); } if (!isset($jsonArray["id"])) { return $this->resp(null, null, -32600, "No ID"); } $id = $jsonArray["id"]; if (!isset($jsonArray["method"])) { return $this->resp(null, $id, -32600, "No method"); } if (!isset($jsonArray["params"])) { $data = array(); } else { $data = $jsonArray["params"]; } $method = explode(":", trim($jsonArray["method"])); if (count($method) != 2) { return $this->resp(null, $id, -32600, "Invalid method signature. Use: BEAN:ACTION"); } $beanType = $method[0]; $action = $method[1]; if (preg_match("/\\W/", $beanType)) { return $this->resp(null, $id, -32600, "Invalid Bean Type String"); } if (preg_match("/\\W/", $action)) { return $this->resp(null, $id, -32600, "Invalid Action String"); } try { switch ($action) { case "store": if (!isset($data[0])) { return $this->resp(null, $id, -32602, "First param needs to be Bean Object"); } $data = $data[0]; if (!isset($data["id"])) { $bean = R::dispense($beanType); } else { $bean = R::load($beanType, $data["id"]); } $bean->import($data); $rid = R::store($bean); return $this->resp($rid, $id); break; case "load": if (!isset($data[0])) { return $this->resp(null, $id, -32602, "First param needs to be Bean ID"); } $bean = R::load($beanType, $data[0]); return $this->resp($bean->export(), $id); break; case "trash": if (!isset($data[0])) { return $this->resp(null, $id, -32602, "First param needs to be Bean ID"); } $bean = R::load($beanType, $data[0]); R::trash($bean); return $this->resp("OK", $id); break; default: $modelName = $this->modelHelper->getModelName($beanType); if (!class_exists($modelName)) { return $this->resp(null, $id, -32601, "No such bean in the can!"); } $beanModel = new $modelName(); if (!method_exists($beanModel, $action)) { return $this->resp(null, $id, -32601, "Method not found in Bean: {$beanType} "); } return $this->resp(call_user_func_array(array($beanModel, $action), $data), $id); } } catch (Exception $exception) { return $this->resp(null, $id, -32099, $exception->getCode() . "-" . $exception->getMessage()); } }