function getRoutesFromDB() { $relations = array(); $relations = $this->getRelations(); $json_relations = JSONRouteRelation::getJSONRelations(); if (count($json_relations) > 0) { foreach ($json_relations as $route => $relation) { if (isset($relations[$route])) { $relations[$route] = array_merge($relations[$route], $relation); } else { $relations[$route] = $relation; } } } $result = DBController::Query("SHOW TABLES"); if ($result === false) { exit(ApiResponse::errorResponse(404)); } else { if (empty($result) === true) { exit(ApiResponse::errorResponse(204)); } else { foreach ($result as $k => $v) { $route = reset($v); $route = new Route(); $route->routeName = reset($v); if (isset($relations[$route->routeName])) { $route->routeFields = $this->getRouteFields($route, $relations[$route->routeName]); } else { $route->routeFields = $this->getRouteFields($route); } ResterUtils::Log("*** PRIMARY KEY: " . $route->routeName . " => " . $route->primaryKey->fieldName); $routes[$route->routeName] = $route; } } } ApiCacheManager::saveValueToCache(ROUTE_CACHE_KEY, $routes); return $routes; }
static function clear() { if (extension_loaded('apc') && ini_get('apc.enabled')) { apc_clear_cache(); apc_clear_cache('user'); ResterUtils::Log("Cleared cache"); } }
static function parseRelationsFromMySQL($relationsArray) { ResterUtils::Log(">>> Processing Relations from MySQL"); $relations = array(); foreach ($relationsArray as $r) { $relation = new MySQLRouteRelation(); $relation->relationName = $r["CONSTRAINT_NAME"]; $relation->route = $r["TABLE_NAME"]; $relation->field = $r["COLUMN_NAME"]; $relation->destinationRoute = $r["REFERENCED_TABLE_NAME"]; $relation->destinationField = $r["REFERENCED_COLUMN_NAME"]; $relations[$relation->route][] = $relation; } ResterUtils::Dump($relations); return $relations; }
static function getJSONRelations() { $relations = array(); if (file_exists(JSON_RELATIONS_FILE)) { ResterUtils::Log(">> Processing JSON Relations file"); $json_relations = file_get_contents(JSON_RELATIONS_FILE); $parsed_relations = json_decode($json_relations); foreach ($parsed_relations as $r) { $relation = new JSONRouteRelation(); $relation->relationName = $r->relationName; $relation->route = $r->route; $relation->field = $r->field; $relation->destinationRoute = $r->destinationRoute; $relation->destinationField = $r->destinationField; $relation->inverse = $r->inverse; $relations[$relation->route][] = $relation; if ($r->inverse) { $relations[$relation->destinationRoute][] = $relation; } } } return $relations; }
private function doResponse($data, $responseCode = 200) { if (isset($data["error"])) { ResterUtils::Log(">> Error Response: " . $data["error"]["code"] . " " . $data["error"]["status"] . " - " . $this->getCurrentRoute()); header("HTTP/1.1 " . $data["error"]["code"] . " " . $data["error"]["status"], true, $data["error"]["code"]); } if ($responseCode != 200) { switch ($responseCode) { case 201: header("HTTP/1.1 " . $responseCode . " Created"); break; } } $bitmask = 0; $options = array('UNESCAPED_SLASHES', 'UNESCAPED_UNICODE'); if (empty($_SERVER['HTTP_X_REQUESTED_WITH']) === true) { $options[] = 'PRETTY_PRINT'; } foreach ($options as $option) { $bitmask |= defined('JSON_' . $option) === true ? constant('JSON_' . $option) : 0; } if (($result = json_encode($data, $bitmask)) !== false) { $callback = null; if (array_key_exists('callback', $_GET) === true) { $callback = trim(preg_replace('~[^[:alnum:]\\[\\]_.]~', '', $_GET['callback'])); if (empty($callback) !== true) { $result = sprintf('%s(%s);', $callback, $result); } } if (headers_sent() !== true) { header(sprintf('Content-Type: application/%s; charset=utf-8', empty($callback) === true ? 'json' : 'javascript')); } } //ResterUtils::Dump($result); exit($result); }
/** * Looks for id into object, if not found, we can generate one * @param object $objectData source object * @return string|NULL id of the object */ function getInsertIDFromObject($objectData) { if ($this->primaryKey != NULL) { //if(!isset($data[$route->primaryKey->fieldName])) { //we have not passed an id by parameters if (!array_key_exists($this->primaryKey->fieldName, $objectData)) { ResterUtils::Log(">> NO KEY SET ON CREATE *" . $this->primaryKey->fieldName . "*"); if ($this->primaryKey->isAutoIncrement) { //put a dummy value to auto_increment do the job $objectData[$this->primaryKey->fieldName] = '0'; } else { $insertID = UUID::v4(); ResterUtils::Log(">> GENERATING NEW UUID " . $insertID); $objectData[$this->primaryKey->fieldName] = $insertID; //generate a UUID } } else { $insertID = $objectData[$this->primaryKey->fieldName]; } return $insertID; } return NULL; }