Allows you to do something special depending on the current environment.
You can add this library as a local, per-project dependency to your project using Composer:
composer require k2gl/app-env
enum AppEnvironment: string
{
case DEV = 'dev';
case TEST = 'test';
case STAGE = 'stage';
case PROD = 'prod';
}
use K2gl\Component\AppEnv\Services\AppEnv;
$appEnv = new AppEnv('test');
$appEnv->is('test'); // true
$appEnv->is(AppEnvironment::TEST); // true
$appEnv->not(AppEnvironment::TEST); // false
$appEnv->not('miss'); // true
$appEnv->in(['miss', 'kiss']); // false
$appEnv->in(['miss', 'test', 'kiss']); // true
$appEnv->notIn(['miss', 'kiss']); // true
$appEnv->notIn(['miss', 'test', 'kiss']); // false
Makes AppEnv available to be used as services in services.yaml
services:
K2gl\Component\AppEnv\Services\AppEnv:
arguments: ['%kernel.environment%']
use K2gl\Component\AppEnv\Services\AppEnv;
class UserLoginProcessor
{
public function __construct(
private readonly AppEnv $appEnv,
) {
}
protected function getAuthenticationFailureResponse(AuthenticationException $exception): JsonResponse
{
$responseData = [ 'message' => 'Bad credentials' ];
if ($this->appEnv->not(AppEnvironment::PROD)) {
$responseData[ 'extended_message' ] = $exception->getMessage();
}
return new JsonResponse( data: $responseData, status: Response::HTTP_UNAUTHORIZED );
}
}