فهرست منبع

added Type_ApiRequest and use in Api

Piotr Labudda 8 سال پیش
والد
کامیت
a4a725d1f8
2فایلهای تغییر یافته به همراه110 افزوده شده و 49 حذف شده
  1. 5 49
      SE/se-lib/Api.php
  2. 105 0
      SE/se-lib/Type/ApiRequest.php

+ 5 - 49
SE/se-lib/Api.php

@@ -1,5 +1,7 @@
 <?php
 
+Lib::loadClass('Type_ApiRequest');
+
 class Api {
 
 	private $_apiUser;
@@ -19,7 +21,8 @@ class Api {
 		header('Access-Control-Allow-Origin: https://dev.procesy5.pl');
 		// header('Access-Control-Allow-Origin: *');
 		// header('Access-Control-Max-Age: 86400'); // cache for 1 day
-		$request = $this->parseUrl($url);
+		$request = Type_ApiRequest::build($url);
+		$this->_request = $request;
 
 		$apiRouterName = $request->apiRouterName;
 		IF(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">apiRouterName (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($apiRouterName);echo'</pre>';}
@@ -28,7 +31,7 @@ class Api {
 			case 'wfsqgis': $apiRouterName = 'WfsQgis'; break;
 			default: $apiRouterName = ucfirst(strtolower($apiRouterName)); break;
 		}
-		$apiRouterClassName = "Api_{$apiRouterName}";
+		$apiRouterClassName = "Api_{$apiRouterName}"; // Api_WfsData, Api_WfsQgis, Api_Wps
 		if (!Lib::tryLoadClass($apiRouterClassName)) {
 			throw new HttpException("Route not exists", 404);
 		}
@@ -42,53 +45,6 @@ class Api {
 		$this->response($responseDocument);
 	}
 
-	private function parseUrl($url) {
-		$request = new stdClass();
-		$request->url = trim($url, '/ ');
-		$urlParts = explode('?', $request->url);
-		$request->path = $urlParts[0];
-		$request->query = (count($urlParts) > 1)? $urlParts[1] : '';
-		$request->args = $_REQUEST;
-		$request->segments = array();
-		$urlPathParts = explode('/', $request->path);
-		foreach ($urlPathParts as $part) {
-			if (!empty($part)) $request->segments[] = $part;
-		}
-
-		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">url (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
-		$request->format = array_shift($request->segments);
-		if (empty($request->segments)) return null;
-
-		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">responseFormat (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request->format);echo'</pre>';}
-		if (!$this->checkResponseFormat($request->format)) {
-			throw new HttpException("Response format not allowed", 400);
-		}
-		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
-
-		$request->apiRouterName = array_shift($request->segments);
-		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
-
-		if (!empty($request->query)) {
-			$queryArgs = array();
-			parse_str($request->query, $queryArgs);
-			$request->query = $queryArgs;
-		}
-
-		$this->_request = $request;
-		return $this->_request;
-	}
-
-	private function checkResponseFormat($format) {
-		$allowedTypes = array();
-		$allowedTypes[] = 'xml';
-		$allowedTypes[] = 'json';
-		$allowedTypes[] = 'csv';
-		$allowedTypes[] = 'csv_file';
-		$allowedTypes[] = 'raw';
-		//$allowedTypes[] = 'xml-ns';// xml with namespace
-		return in_array($format, $allowedTypes);
-	}
-
 	private function response($document) {
 		if ('xml' == $this->_request->format) {
 			$this->responseXml($document);

+ 105 - 0
SE/se-lib/Type/ApiRequest.php

@@ -0,0 +1,105 @@
+<?php
+
+class Type_ApiRequest {
+
+	var $_data = [];
+
+	static function build($url) { // @return Type_ApiRequest
+		// GET https://biuro.biall-net.pl/dev-pl/se-master/wps.php?Request=GetCapabilities&identifier=&Service=WPS&Version=1.0.0
+		// $url: /xml/wps?Request=GetCapabilities&identifier=&Service=WPS&Version=1.0.0
+
+		$request = new Type_ApiRequest();
+		$request->url = trim($url, '/ ');
+		$urlParts = explode('?', $request->url);
+		$request->path = $urlParts[0];
+		$request->query = (count($urlParts) > 1)? $urlParts[1] : '';
+		$request->args = $_REQUEST;
+		$segments = array_filter(explode('/', $request->path), function ($part) {
+			return (!empty($part));
+		});
+		$request->format = array_shift($segments);
+		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">url (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
+		if (empty($segments)) return null;
+		$request->segments = $segments;
+
+		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">responseFormat (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request->format);echo'</pre>';}
+		if (!self::checkResponseFormat($request->format)) {
+			throw new HttpException("Response format not allowed", 400);
+		}
+		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
+
+		$request->apiRouterName = array_shift($request->segments);
+		IF(V::get('DBG','',$_GET)>1){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">u (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($request);echo'</pre>';}
+		$request->version = V::geti('version', '', $_REQUEST);
+		// if (!self::checkVersion($request->version, $request->apiRouterName)) { // version may be set in POST body
+		// 	throw new Exception("{$request->apiRouterName} API Version not supported ({$request->version})", 400);
+		// }
+
+		if (!empty($request->query)) {
+			$queryArgs = array();
+			parse_str($request->query, $queryArgs);
+			$request->query = $queryArgs;
+		}
+
+		return $request;
+	}
+
+	static function checkResponseFormat($format) {
+		$allowedTypes = array();
+		$allowedTypes[] = 'xml';
+		$allowedTypes[] = 'json';
+		$allowedTypes[] = 'csv';
+		$allowedTypes[] = 'csv_file';
+		$allowedTypes[] = 'raw';
+		return in_array($format, $allowedTypes);
+	}
+
+	static function checkVersion($version, $apiName) {
+		switch ($apiName) {
+			case 'wps': return self::checkWPSVersion($version);
+			case 'wfsQgis': return self::checkWFSQgisVersion($version);
+			case 'wfsData': return self::checkWFSDataVersion($version);
+			default: return false;
+		}
+	}
+	static function checkWPSVersion($version) {
+		switch ($version) {
+			case '1.0.0': return true; // QGIS client
+			case '2.0.0': return false; // TODO: implement WPS 2.0.0 - need working client to test
+			case '2.0.2': return false; // TODO: implement WPS 2.0.0 - need working client to test
+			default: return false;
+		}
+	}
+	static function checkWFSQgisVersion($version) {
+		return true; // TODO: restrict by version ?
+	}
+	static function checkWFSDataVersion($version) {
+		return true; // TODO: restrict by version ?
+	}
+
+	function __isset($name) {
+		return (array_key_exists($name, $this->_data));
+	}
+
+	function __get($name) {
+		if (array_key_exists($name, $this->_data)) {
+			return $this->_data[$name];
+		}
+		return null;
+	}
+
+	function __set($name, $value) {
+		$this->_data[$name] = $value;
+	}
+
+	function toArray() {
+		return $this->_data;
+	}
+
+	function __toString() {
+		return str_replace('"', '',
+			str_replace([ '{', '}', '":', ',"' ], [ '{ ', ' }', ': ', ', ' ], json_encode($this->_data))
+		);
+	}
+
+}