PHP code example of k2gl / app-env

1. Go to this page and download the library: Download k2gl/app-env library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

k2gl / app-env example snippets



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

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