Packages · utilities
k2gl/app-env
A small, typed helper for reading application environment.
Read and branch on the app environment without scattering getenv() and string checks.
Install
composer require k2gl/app-env Reach for it when
- You branch on the application environment and want it typed in one place.
Look elsewhere when
- You need a full configuration system — use a config component.
PHP AppEnv service
Run code conditionally on the current application environment (prod, dev, test).
Installation
You can add this library as a local, per-project dependency to your project using Composer:
composer require k2gl/app-env
Usage:
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
Configuration as Symfony service
Makes AppEnv available to be used as services in services.yaml
services:
K2gl\Component\AppEnv\Services\AppEnv:
arguments: ['%kernel.environment%']
Usage example:
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 );
}
}
Pull requests are always welcome
API
Public classes and methods, generated from the source.
K2gl\Component\AppEnv\Services\AppEnv class
__construct( protected readonly string $env )is(BackedEnum|string $env): boolnot(BackedEnum|string $env): boolin(array $envs): boolnotIn(array $envs): bool