From d6d78bf3c0901df91251eedd3e6e7415b1acecaf Mon Sep 17 00:00:00 2001 From: Martin Beranek Date: Fri, 2 Aug 2024 10:15:07 +0200 Subject: [PATCH] Constructor options --- README.md | 2 +- src/Output/Configurator/EnvConfigurator.php | 23 +++++++++++++---- src/Output/Configurator/QueryConfigurator.php | 25 +++++++++++-------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1e20ab8..f164740 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Then register your preffered output configurator in *config.neon* services: ```neon services: - apiOutputConfigurator: Tomaj\NetteApi\Output\Configurator\QueryConfigurator + apiOutputConfigurator: Tomaj\NetteApi\Output\Configurator\DebuggerConfigurator ``` And add route to you RouterFactory: diff --git a/src/Output/Configurator/EnvConfigurator.php b/src/Output/Configurator/EnvConfigurator.php index cfc495c..cd8bdfb 100644 --- a/src/Output/Configurator/EnvConfigurator.php +++ b/src/Output/Configurator/EnvConfigurator.php @@ -8,10 +8,23 @@ class EnvConfigurator implements ConfiguratorInterface { + private string $envVariable = "APP_ENV"; + private string $productionValue = "production"; + + /** + * @param string $envVariable Which environment variable to check for production value + * @param string $productionValue Value that indicates production environment eg. "production" or "prod"... + */ + public function __construct(string $envVariable = "APP_ENV", string $productionValue = "production") + { + $this->envVariable = $envVariable; + $this->productionValue = $productionValue; + } + public function validateSchema(?Request $request = null): bool { - $appEnv = getenv("APP_ENV"); - if ($appEnv === "production") { + $appEnv = getenv($this->envVariable); + if ($appEnv === $this->productionValue) { return false; } return true; @@ -19,10 +32,10 @@ public function validateSchema(?Request $request = null): bool public function showErrorDetail(?Request $request = null): bool { - $appEnv = getenv("APP_ENV"); - if ($appEnv === "production") { + $appEnv = getenv($this->envVariable); + if ($appEnv === $this->productionValue) { return false; } return true; } -} \ No newline at end of file +} diff --git a/src/Output/Configurator/QueryConfigurator.php b/src/Output/Configurator/QueryConfigurator.php index afeb32d..c66964b 100644 --- a/src/Output/Configurator/QueryConfigurator.php +++ b/src/Output/Configurator/QueryConfigurator.php @@ -8,21 +8,26 @@ class QueryConfigurator implements ConfiguratorInterface { - /* - ### Disable schema validation in not production environment - Include get parameter no_schema_validate in your request to disable schema validation. This is useful for testing purposes. - * schema validation is disabled by default in production environment for performance reasons + private string $noSchemaValidateParam = "no_schema_validate"; + private string $errorDetailParam = "error_detail"; + + /** + * @param string $noSchemaValidateParam Name of get parameter to disable schema validation + * @param string $errorDetailParam Name of get parameter to show additional info in error response + */ + public function __construct(string $noSchemaValidateParam = "no_schema_validate", string $errorDetailParam = "error_detail") + { + $this->noSchemaValidateParam = $noSchemaValidateParam; + $this->errorDetailParam = $errorDetailParam; + } - ### Add additional info to error response - Include get parameter error_detail in your request to show additional info in error response. This is useful for debugging purposes. - */ public function validateSchema(?Request $request = null): bool { if ($request === null) { return false; } $getParams = $request->getParameters(); - return !isset($getParams['no_schema_validate']); + return !isset($getParams[$this->noSchemaValidateParam]); } public function showErrorDetail(?Request $request = null): bool @@ -31,6 +36,6 @@ public function showErrorDetail(?Request $request = null): bool return false; } $getParams = $request->getParameters(); - return isset($getParams['error_detail']); + return isset($getParams[$this->errorDetailParam]); } -} \ No newline at end of file +}