diff --git a/Source/API/script/ExceptionHandle/TypeError.php b/Source/API/script/ExceptionHandle/TypeError.php new file mode 100644 index 0000000..102697c --- /dev/null +++ b/Source/API/script/ExceptionHandle/TypeError.php @@ -0,0 +1,12 @@ +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); - try { - $app->get('/', function (Request $request, Response $response) { - $parameters = $request->getQueryParams(); - if (empty($parameters['method'])){ - throw new TypeError("No method specified"); - }else{ - $method = $parameters['method']; - } - unset($parameters['method']); - $listGateway = array("\\Gateway\\GatewayForm", "\\Gateway\\GatewayKeyword", "\\Gateway\\GatewayQuestion"); - foreach ($listGateway as $gateway){ - if (method_exists($gateway, $method)) { - $response->getBody()->write(print_r((new $gateway)->$method($parameters))); - } - } - $temp = array(0 => array("id" => "1", - "0" => "1", - "title" => "Votre avis nous intéresse !", - "1" => "Votre avis nous intéresse !", - "description" => "Ce formulaire vous permet de candidater à une potentielle interview si votre profil nous intéresse.", - "2" => "Ce formulaire vous permet de candidater à une potentielle interview si votre profil nous intéresse." - )); - $response->getBody()->write(json_encode($temp)); - return $response->withHeader('Content-type', 'application/json'); - }); - }catch (TypeError $t){ - echo "Error, wrong parameters key, or no parameters write : ".$t->getMessage(); - } - - - // Run app - $app->run(); -}catch (HttpNotFoundException $e) { - echo "Error, not Found : " . $e->getMessage(); -} +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"); + foreach ($listGateway as $gateway){ + if (method_exists($gateway, $method)) { + $response->getBody()->write(json_encode((new $gateway)->$method($parameters))); + } + } + $temp = array(0 => array("id" => "1", + "0" => "1", + "title" => "Votre avis nous intéresse !", + "1" => "Votre avis nous intéresse !", + "description" => "Ce formulaire vous permet de candidater à une potentielle interview si votre profil nous intéresse.", + "2" => "Ce formulaire vous permet de candidater à une potentielle interview si votre profil nous intéresse." + )); + $response->getBody()->write(json_encode($temp)); + return $response->withHeader('Content-type', 'application/json'); +}); + + +// Run app +$app->run(); \ No newline at end of file