lane4core / dotenv
Advanced functions dotEnv tool for reading .env files for standard global and protected contexts
Installs: 10
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/lane4core/dotenv
Requires
- php: >=8.1
- lane4core/contract: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.0.4
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.11.2
README
A support tool for reading .env files in global and protected contexts
Description
Lane4 core 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 loadPublic($path)
anf loadPrivate($path)
methods.
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 and typecasting
Example Code
use lane4core\DotEnv\DotEnv; $dotEnv = new DotEnv(); $dotEnv->loadPublic($appRootPath); $domainEnv = $dotEnv->loadPrivate($domainRootPath);
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
Contents in the GitHub Repository
- Source Files:
src
tests
- Support:
- Docker Compose
.env
pre-commit-hook.sh
Makefile
(Simply runmake
in the console)
- Documentation:
README.md