Improved request parsing

This commit is contained in:
Zi Xing 2022-01-05 17:09:48 -05:00
parent aff38e601d
commit c05ec35556
4 changed files with 39 additions and 8 deletions

View File

@ -3,7 +3,7 @@
"configuration": {
"logging_enabled": true,
"root_path": "/test",
"debugging_mode": true,
"debugging_mode": false,
"framework_signature": true,
"api_signature": true,
"khm_enabled": true,

View File

@ -203,7 +203,6 @@
*/
public function match(?string $requestUrl=null, string $requestMethod = null)
{
$params = array();
/** @noinspection PhpUnusedLocalVariableInspection */
$match = false;
@ -335,4 +334,12 @@
return "`^(?J)$route$`u";
}
/**
* @return string
*/
public function getBasePath(): string
{
return $this->basePath;
}
}

View File

@ -97,9 +97,7 @@
throw new ApiException('The API Environment must be initialized before using VerboseAdventure');
if(self::$VerboseAdventure == null)
{
self::$VerboseAdventure = new VerboseAdventure('KIMCHI_API_NAME');
}
self::$VerboseAdventure = new VerboseAdventure(KIMCHI_API_NAME);
return self::$VerboseAdventure;
}
@ -113,6 +111,7 @@
* @return void
* @throws Exceptions\UnsupportedResponseTypeExceptions
* @throws UnsupportedResponseStandardException
* @throws ApiException
* @noinspection PhpIssetCanBeReplacedWithCoalesceInspection
* @noinspection PhpRedundantCatchClauseInspection
*/
@ -120,21 +119,29 @@
{
// set Request Url if it isn't passed as parameter
if($requestUrl === null)
{
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
}
// strip base path from request url
$requestUrl = substr($requestUrl, strlen(KIMCHI_API_ROOT_PATH));
$requestUrl = substr($requestUrl, strlen($API->getRouter()->getBasePath()));
// Strip query string (?a=b) from Request Url
/** @noinspection SpellCheckingInspection */
if (($strpos = strpos($requestUrl, '?')) !== false)
{
$requestUrl = substr($requestUrl, 0, $strpos);
}
// set Request Method if it isn't passed as a parameter
if($requestMethod === null)
{
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET';
}
define('KIMCHI_API_REQUEST_URL', $requestUrl);
define('KIMCHI_API_REQUEST_METHOD', $requestMethod);
self::$VerboseAdventure->log(EventType::INFO, $requestMethod . ' ' . $requestUrl, KIMCHI_API_REQUEST_ID);
$match = $API->getRouter()->match($requestUrl, $requestMethod);
// call closure or throw 404 status
@ -146,12 +153,13 @@
}
catch(ApiMethodNotFoundException $e)
{
unset($e);
self::handle404();
}
catch(Exception $e)
{
self::$VerboseAdventure->logException($e, KIMCHI_API_REQUEST_ID);
self::getVerboseAdventure()->logException($e, KIMCHI_API_REQUEST_ID);
self::handleException($e);
}
}
@ -166,6 +174,7 @@
* @param string $response_standard
* @param string $response_type
* @return void
* @throws ApiException
* @throws Exceptions\UnsupportedResponseTypeExceptions
* @throws UnsupportedResponseStandardException
*/
@ -189,6 +198,7 @@
* @param string $response_standard
* @param string $response_type
* @return void
* @throws ApiException
* @throws Exceptions\UnsupportedResponseTypeExceptions
* @throws UnsupportedResponseStandardException
*/
@ -237,9 +247,11 @@
* @param Response $response
* @throws Exceptions\UnsupportedResponseTypeExceptions
* @throws UnsupportedResponseStandardException
* @throws ApiException
*/
public static function handleResponse(Response $response)
{
self::getVerboseAdventure()->log(EventType::INFO, KIMCHI_API_REQUEST_METHOD . ' ' . KIMCHI_API_REQUEST_URL . ' ' . $response->ResponseCode, KIMCHI_API_REQUEST_ID);
http_response_code($response->ResponseCode);
if($response->ResponseType == ResponseType::Automatic)

View File

@ -25,6 +25,18 @@
"version": "latest",
"source": "symfony@composer/yaml",
"required": true
},
{
"package": "com.symfony.uid",
"version": "latest",
"source": "symfony@composer/uid",
"required": true
},
{
"package": "net.intellivoid.verbose_adventure",
"version": "latest",
"source": "default@github/intellivoid/VerboseAdventure",
"required": true
}
],
"configuration": {