/** * Handle request * * router.template.php can be used as a template on how to create a controller for a request handler. * * Example code: * * <code> * require_once(dirname(__FILE__)."/autoload.php"); * if(file_exists("connection.php")){ * require("connection.php"); * } * * LudoDBRegistry::set('DEVELOP_MODE', true); * LudoDB::enableLogging(); * * $request = isset($_GET['request']) ? $_GET['request'] : $_POST['request']; * $data = isset($_POST['data']) ? $_POST['data'] : null; * * $handler = new LudoDBRequestHandler(); * echo $handler->handle($request, $data); * </code> * * @param String $request * @param Array|Null $data * @return string * @throws LudoDBObjectNotFoundException * @throws LudoDBServiceNotImplementedException * @throws LudoDBInvalidServiceException * @throws LudoDBInvalidArgumentsException */ public function handle($request, $data = null) { $this->request = $request; try { $data = $this->runAndRemoveProgressBar($data); if (!isset($this->request)) { throw new LudoDBInvalidArgumentsException("No request"); } $this->arguments = $this->getArguments(); $this->resourceName = $this->getClassName($request); $this->resource = $this->getResource($this->arguments); $this->validServices = $this->getValidServices(); $this->serviceName = $this->getServiceName(); if (!in_array($this->serviceName, $this->validServices)) { throw new LudoDBInvalidServiceException('Invalid service ' . $this->serviceName . ', resource: ' . $this->getClassName($request)); } if (!$this->resource->validateArguments($this->serviceName, $this->arguments)) { throw new LudoDBInvalidArgumentsException('Invalid constructor arguments for resource:' . $this->getClassName($request) . ', service:' . $this->serviceName . ", arguments: " . implode(",", $this->arguments)); } if (!$this->resource->validateServiceData($this->serviceName, $data)) { throw new LudoDBInvalidArgumentsException('Invalid service data/arguments for resource:' . $this->getClassName($request) . ', service:' . $this->serviceName . ", arguments: " . implode(",", $this->arguments)); } if ($this->serviceName === 'delete' || $this->serviceName === 'read') { if ($this->resource instanceof LudoDBModel && !$this->resource->getId()) { throw new LudoDBObjectNotFoundException('Object not found'); } } if (isset($this->authenticator)) { $success = $this->authenticator->authenticate($this->resourceName, $this->serviceName, $this->arguments, $data); if (!$success) { throw new LudoDBUnauthorizedException('Not authorized'); } } if (!method_exists($this->resource, $this->serviceName)) { throw new LudoDBServiceNotImplementedException("Service " . $this->serviceName . " not implemented"); } if ($this->resource->shouldCache($this->serviceName)) { return $this->toJSON($this->getCached($data)); } else { return $this->toJSON($this->resource->{$this->serviceName}($data)); } } catch (Exception $e) { $this->message = $e->getMessage(); $this->code = $e->getCode(); $this->success = false; return $this->toJSON(array()); } }
/** * Return "update" SQL. * @return string */ public function getUpdateSql() { return "update " . $this->obj->configParser()->getTableName() . " set " . $this->getUpdatesForSql($this->obj->getUncommitted()) . " where " . $this->obj->configParser()->getIdField() . " = '" . $this->obj->getId() . "'"; }