if (isset($_REQUEST['url'])) { //si por ejemplo pasamos explode('/','////controller///method////args///') el resultado es un array con elem vacios; //Array ( [0] => [1] => [2] => [3] => [4] => controller [5] => [6] => [7] => method [8] => [9] => [10] => [11] => args [12] => [13] => [14] => ) $url = explode('/', trim($_REQUEST['url'])); //con array_filter() filtramos elementos de un array pasando función callback, que es opcional. //si no le pasamos función callback, los elementos false o vacios del array serán borrados //por lo tanto la entre la anterior función (explode) y esta eliminamos los '/' sobrantes de la URL $url = array_filter($url); $this->class = $this->instanceClass(strtolower(array_shift($url))); $this->method = strtolower(array_shift($url)); $this->arguments = $url; $func = $this->method; if ($this->class != NULL && (int) method_exists($this->class, $func) > 0) { if (count($this->arguments) > 0) { call_user_func_array(array($this->class, $this->method), $this->arguments); } else { //si no lo llamamos sin argumentos, al metodo del controlador call_user_func(array($this->class, $this->method)); } } else { $rest = new Rest(); $rest->processErrorResponse('', Rest::STATUS_METHOD_NOT_ALLOWED, self::REQUEST_NOT_FOUND); } } $rest = new Rest(); $rest->processErrorResponse('', Rest::STATUS_METHOD_NOT_ALLOWED, self::REQUEST_NOT_FOUND); } } $api = new Api(); $api->processRequest();
/** * Overrides parent::processRequest * */ protected function processRequest() { // Get request params $apiKey = self::getRequestVar('_api_key', '/^\\w+$/'); $method = self::getRequestVar('_method', '/^[\\w\\.]+$/'); $globals = self::getRequestVar('_globals'); $server = self::getRequestVar('_compiler'); // the compiler which is calling this api $configExists = Config::exists($this->configFile); // Check api-key if (!$apiKey) { throw new UserFeedback("You need to provide an api-key to use this API"); } if ($configExists && !$this->compiler->apiKey) { throw new UserFeedback("You need to set the api-key in your TinyQueries config-file"); } if ($configExists && $apiKey != $this->compiler->apiKey) { throw new UserFeedback("api-key does not match"); } // Ensure that there is only one compiler which is speaking with this api, otherwise queries might get mixed up if ($configExists && $server && strpos($this->compiler->server, $server) === false) { throw new UserFeedback('Compiler which is calling this api does not match with compiler in config'); } // Set global query params if ($this->db && $globals) { $globals = json_decode($globals); foreach ($globals as $name => $value) { $this->db->param($name, $value); } } // If no method is send, just do the default request handler for queries if (!$method) { return parent::processRequest(); } // Method mapper switch ($method) { case 'compile': return $this->compile(); case 'createView': return $this->createView(); case 'deleteQuery': return $this->deleteQuery(); case 'downloadQueries': return $this->downloadQueries(); case 'getDbScheme': return $this->getDbScheme(); case 'getInterface': return $this->getInterface(); case 'getProject': return $this->getProject(); case 'getSource': return $this->getSource(); case 'getSQL': return $this->getSQL(); case 'getStatus': return $this->getStatus(); case 'getTermParams': return $this->getTermParams(); case 'renameQuery': return $this->renameQuery(); case 'saveSource': return $this->saveSource(); case 'setup': return $this->setup(); case 'testApi': return array("message" => "Api is working"); } throw new \Exception('Unknown API method'); }