You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.8 KiB
63 lines
1.8 KiB
<?php
|
|
|
|
use ExceptionHandle\HttpNotFoundError;
|
|
use ExceptionHandle\PDOError;
|
|
use ExceptionHandle\TypeErrorMethod;
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
|
use Slim\Factory\AppFactory;
|
|
|
|
require 'Config/vendor/autoload.php';
|
|
|
|
/**
|
|
* Instantiate App
|
|
*/
|
|
$app = AppFactory::create();
|
|
|
|
// Add Routing Middleware
|
|
$app->addRoutingMiddleware();
|
|
|
|
/**
|
|
* Add Error Handling Middleware
|
|
*
|
|
* @param bool $displayErrorDetails -> Should be set to false in production
|
|
* @param bool $logErrors -> Parameter is passed to the default ErrorHandler
|
|
* @param bool $logErrorDetails -> Display error details in error log
|
|
*/
|
|
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
|
|
|
|
/**
|
|
* Add a route for the API
|
|
*/
|
|
$app->get('/', function (Request $request, Response $response) {
|
|
$parameters = $request->getQueryParams();
|
|
if (empty($parameters['method'])){
|
|
throw new TypeErrorMethod($request);
|
|
}else{
|
|
$method = $parameters['method'];
|
|
}
|
|
unset($parameters['method']);
|
|
$listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion");
|
|
$ok = false;
|
|
foreach ($listGateway as $gateway){
|
|
try {
|
|
if (method_exists($gateway, $method)) {
|
|
$ok = true;
|
|
$rep = ((new $gateway)->$method($parameters));
|
|
#$response->getBody()->write(json_encode($rep, JSON_UNESCAPED_UNICODE));
|
|
$response->getBody()->write($rep);
|
|
}
|
|
}catch (PDOException $e){
|
|
throw new PDOError($request,$e->getMessage(),$e);
|
|
}
|
|
}
|
|
if (!$ok){
|
|
throw new HttpNotFoundError($request);
|
|
}
|
|
return $response->withHeader('Content-type', 'application/json');
|
|
});
|
|
|
|
|
|
// Run app
|
|
$app->run();
|