k2gl/app-env

PHP application environment service

1.0.2 2023-10-05 04:44 UTC

This package is auto-updated.

Last update: 2024-06-05 06:51:37 UTC


README

Allows you to do something special depending on the current environment.

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

Collaborate with pull requests