Skip to content

k2gl/app-env

Repository files navigation

PHP AppEnv service

CI Latest Stable Version Total Downloads PHPStan Level License

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

About

PHP AppEnv service

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages