protected function processSource(Source $source) { $params = $source->getParameters(); $datasource = $source->getDatasource(); if (is_numeric($datasource)) { $datasource = $this->simu->getDatasourceById((int) $datasource); } else { $datasource = $this->simu->getDatasourceByName($datasource); } switch ($datasource->getType()) { case 'uri': $query = ""; $path = ""; $datas = array(); foreach ($params as $param) { $value = $this->formatParamValue($param); if ($value === null) { return null; } if ($param->getType() == 'path') { $path .= "/" . $value; } elseif ($param->getType() == 'data') { $name = $param->getName(); if (isset($datas[$name])) { $datas[$name][] = $value; } else { $datas[$name] = array($value); } } else { $query .= "&" . $param->getName() . "=" . $value; } } $uri = $datasource->getUri(); if ($path != "") { $uri .= $path; } if ($query != "") { $uri .= "?" . substr($query, 1); } if (isset($this->uricache[$uri])) { $result = $this->uricache[$uri]; } else { $client = Client::createClient(); if ($datasource->getMethod() == "GET") { $result = $client->get($uri); } else { $result = $client->post($uri, $data); } $this->uricache[$uri] = $result; } break; case 'database': case 'internal': $args = array(); $args[] = $source->getRequest(); foreach ($params as $param) { $value = $this->formatParamValue($param); if ($value === null) { return null; } $args[] = $value; } $query = call_user_func_array('sprintf', $args); $database = $this->simu->getDatabaseById($datasource->getDatabase()); $database->connect(); $result = $database->query($query); break; } switch ($source->getReturnType()) { case 'singleValue': return $result; case 'json': $returnPath = $source->getReturnPath(); $returnPath = $this->replaceVariables($returnPath); $json = json_decode($result, true); $result = ResultFilter::filter("json", $json, $returnPath); return $result; case 'assocArray': $returnPath = $source->getReturnPath(); $returnPath = $this->replaceVariables($returnPath); $keys = explode("/", $returnPath); foreach ($keys as $key) { if (preg_match("/^([^\\[]+)\\[([^\\]]+)\\]\$/", $key, $matches)) { $key1 = $matches[1]; if (!isset($result[$key1])) { break; } $result = $result[$key1]; $key = $matches[2]; } if (ctype_digit($key)) { $key = (int) $key; } if (!isset($result[$key])) { break; } $result = $result[$key]; } return $result; case 'html': $returnPath = $source->getReturnPath(); $returnPath = $this->replaceVariables($returnPath); $result = ResultFilter::filter("html", $result, $returnPath, $datasource->getNamespaces()); return $result; case 'xml': $returnPath = $source->getReturnPath(); $returnPath = $this->replaceVariables($returnPath); $result = ResultFilter::filter("xml", $result, $returnPath, $datasource->getNamespaces()); return $result; case 'csv': $returnPath = $source->getReturnPath(); $returnPath = $this->replaceVariables($returnPath); $result = ResultFilter::filter("csv", $result, $returnPath, null, $source->getSeparator(), $source->getDelimiter()); return $result; } return null; }
protected function processSource($source) { $ds = (string) $source['datasource']; if (is_numeric($ds)) { $datasources = $this->datasources->xpath("/DataSources/DataSource[@id='" . $ds . "']"); } else { $datasources = $this->datasources->xpath("/DataSources/DataSource[@name='" . $ds . "']"); } switch ((string) $datasources[0]['type']) { case 'uri': $uri = (string) $datasources[0]['uri']; $client = Client::createClient(); $data = array(); // TODO : add parameters elements in DataSources.xsd if ((string) $datasources[0]['method'] == "" || (string) $datasources[0]['method'] == "GET") { $result = $client->get($uri); } else { $result = $client->post($uri, $data); } break; case 'database': case 'internal': $databases = $this->datasources->xpath("/DataSources/Databases/Database[@id='" . (string) $datasources[0]['database'] . "']"); $database = new Database(null, (int) $databases[0]['id'], (string) $databases[0]['type'], (string) $databases[0]['name']); if ((string) $databases[0]['host'] != "") { $database->setHost((string) $databases[0]['host']); } if ((string) $databases[0]['port'] != "") { $database->setPort((int) $databases[0]['port']); } if ((string) $databases[0]['user'] != "") { $database->setUser((string) $databases[0]['user']); } if ((string) $databases[0]['password'] != "") { $database->setPassword((string) $databases[0]['password']); } elseif ((string) $databases[0]['user'] != "") { try { $host = $this->get('kernel')->getContainer()->getParameter('database_host'); $port = $this->get('kernel')->getContainer()->getParameter('database_port'); $user = $this->get('kernel')->getContainer()->getParameter('database_user'); if ((string) $databases[0]['host'] == $host && (string) $databases[0]['port'] == $port && (string) $databases[0]['user'] == $user) { $database->setPassword($this->get('kernel')->getContainer()->getParameter('database_password')); } } catch (\Exception $e) { } } $query = (string) $source['request']; $database->connect(); $result = $database->query($query); break; } switch ((string) $source['returnType']) { case 'json': $json = json_decode($result, true); return ResultFilter::filter("json", $json, (string) $source['returnPath']); case 'assocArray': return $this->filterResultByLines($result, (string) $source['returnPath']); case 'xml': return ResultFilter::filter("xml", $result, (string) $source['returnPath']); case 'csv': $result = ResultFilter::filter("csv", $result, "", null, (string) $source['separator'], (string) $source['delimiter']); return $this->filterResultByLines($result, (string) $source['returnPath']); } return null; }