lane4hub/dotenv

Advanced functions dotEnv tool for reading .env files for global and protected contexts

1.0.0 2025-01-31 16:59 UTC

This package is auto-updated.

Last update: 2025-06-29 02:18:37 UTC


README

A support tool for reading .env files in global and protected contexts

Description

Jardis DotEnv enables reading .env* files according to the well-established rules for .env files. The extracted values can be made available either in the global $_ENV variable or in protected contexts, such as an application domain.

The .env* files must be placed in a subdirectory and read using the load($path, false) method. This approach allows for different settings compared to the global $_ENV without causing interference. This is particularly useful when refactoring a monolithic application and creating protected areas with their own values within an existing application.

Features

  • Read .env* files in a protected context
  • .env files with arrays

Example Code

use Jardis\DotEnv\DotEnv;

$dotEnv = new DotEnv();
// Load values into $_ENV
$dotEnv->load($appRootPath);

// Do NOT load values into $_ENV and return the result as an array
$domainEnv = $dotEnv->load($domainRootPath, false);

Data Types

The tool recognizes and processes the following data types:

  • string
  • bool
  • numerics (int, float)
  • array (with type casting of values)

A special feature is the support for both numeric and associative arrays in .env* files.

TYPE_INT=1
TYPE_BOOL=true
TYPE_STRING=teststring
TYPE_ARRAY=[1,2,3,test=>hello,test2=>true,test3=>[1,2,3,4]]

DB_HOST=testHost
DB_NAME=testName
HOME=~
DATABASE_URL=mysql://${DB_HOST}:${DB_NAME}@localhost

Special Features

You also have the option to customize three strategies for processing .env files via constructor injection.

    public function __construct(
        ?GetFilesFromPath $fileFinder = null,
        ?GetValuesFromFiles $fileContentReader = null,
        ?CastTypeHandler $castTypeHandler = null
    ) {
        $this->getFilesFromPath = $fileFinder ?? new GetFilesFromPath();
        $this->castTypeHandler = $castTypeHandler ?? new CastTypeHandler();
        $this->getValuesFromFiles = $fileContentReader ?? new GetValuesFromFiles($this->castTypeHandler);
    }

With this approach, you can customize how .env files are searched (GetFilesFromPath), how values are read from the files (GetValuesFromFiles), and how type conversion (CastTypeHandler) is handled.

Quickstart with Composer

composer require lane4hub/dotenv
make install

Quickstart via GitHub

git clone https://github.com/lane4hub/dotenv.git
cd dotenv
make test

Contents in the GitHub Repository

  • Source Files:
    • src
    • tests
  • Support:
    • Docker Compose
    • .env
    • pre-commit-hook.sh
    • Makefile (Simply run make in the console)
  • Documentation:
    • README.md

The structure of the Dockerfile for creating the PHP image is somewhat more extensive than necessary for this tool because the resulting PHP image is used in various Lane4 tools.

We also ensure that our images are as small as possible and do not leave unnecessary files on your system, even when images are rebuilt multiple times.

Jardis Framework

This tool is part of the development of our Domain-Driven Design framework Jardis (Just a reliable domain integration system).

Jardis consists of a collection of highly professional PHP software packages and concepts based on DDD, explicitly developed for the efficient and sustainable solution of complex business applications.

Our development is based on defined standards such as DDD and PSR, aiming to deliver the highest possible quality for functional and non-functional requirements.

For technical quality assurance, we use PhpStan Level 8, PhpCS, and generate full test coverage with PhpUnit.

Our software packages meet the following quality attributes:

  • Analyzability
  • Adaptability
  • Extensibility
  • Modularity
  • Maintainability
  • Testability
  • Scalability
  • High performance