bubblegum-php/bubblegum-environment

Environment manager module for BUBBLEGUM.

1.0.0 2024-09-26 09:44 UTC

This package is auto-updated.

Last update: 2025-07-05 12:17:41 UTC


README

.env configuration module for BUBBLEGUM

Installation

Require this module with composer

composer require bubblegum-php/bubblegum-environment

Usage

Load environment

Getting environment variables

You can simply use the env helper to get the environment variable.

env('VARIABLE_NAME', 'DefaultValue')

You can define a default value for a variable, which will be returned if the environment variable with the same name is not set. If neither a default value nor an environment variable is set, this method returns null.

Get or Throw

Throws an exception if an environment variable is not found.

use Bubblegum\Environment;

Environment::getOrThrow('VARIABLE_NAME'); // Throws an EnvironmentException if there is no variable named 'VARIABLE_NAME'

Basic Load

use Bubblegum\Environment;

Environment::loadFromFile('path/to/env/file');

Load if not prevented

You can prevent the .env file loading if the environment already have the PREVENT_ENV_FILE_LOADING variable set to true.

name: bubblegum
services:
  php:
    build:
      context: ./docker/
      dockerfile: php.dockerfile
    env_file:
      - ./.env # we already have environment loading here
    environment:
      PREVENT_ENV_FILE_LOADING: true # so we don't need to load it

If loading is NOT prevented, this method will load the environment.

use Bubblegum\Environment;

Environment::loadIfNotPrevented('path/to/env/file');

Put variables locally

You can put variables locally without saving them to a file.

use Bubblegum\Environment;
$value = 'Hello There!'
Environment::put('VARIABLE_NAME', $value);
env(VARIABLE_NAME) // returns 'Hello There!'