Skip to content

Commit

Permalink
Constructor options
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Beranek committed Aug 2, 2024
1 parent 3c7a25f commit d6d78bf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 18 additions & 5 deletions src/Output/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,34 @@

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;
}

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;
}
}
}
25 changes: 15 additions & 10 deletions src/Output/Configurator/QueryConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]);
}
}
}

0 comments on commit d6d78bf

Please sign in to comment.