Php DotEnv

GitHub license Maintenance GitHub release (latest by date)

A simple and standalone DotEnv parser for PHP 5.6+


Any contribution or feedback is highly welcomed, please feel free to create a pull request or submit a new issue.

Installation

Simply install the library using composer:

composer require bgaze/php-dotenv

Usage

Quick start

This package ships two classes :

  • Bgaze\Dotenv\Parser which does the parsing job.
  • Bgaze\Dotenv\Helpers which provides static functions for a quick usage of the parser.

To quickly parse Dotenv, use helper functions from the Helpers class:

use \Bgaze\Dotenv\Helpers as DotEnv;

try {
    var_dump(DotEnv::fromString('a dotenv string', [ /* some default values */ ]));
} catch (\Exception $e) {
    echo "<pre>{$e}</pre>";
}

try {
    var_dump(DotEnv::fromFile('path/to/dotenv/file', [ /* some default values */ ]));
} catch (\Exception $e) {
    echo "<pre>{$e}</pre>";
}

You can also directly use the Parser class:

use \Bgaze\Dotenv\Parser;

$parser = new Parser();

if ($parser->parseString('a dotenv string')) {
    var_dump($parser->get());
} else {
    var_dump($parser->errors());
}

if ($parser->parseFile('path/to/dotenv/file')) {
    var_dump($parser->get());
} else {
    var_dump($parser->errors());
}

Helpers class

fromString:

Parse the provided string, throw an exception if invalid, otherwise return parsed content as a key-value array.

/**
 * @param string $string The string to parse
 * @param array $defaults An array of default values
 * @return array The parsed content
 *
 * @throws \UnexpectedValueException
 */
public static function fromString($string, array $defaults = []);

fromFile:

Parse the provided file, throw an exception if invalid, otherwise return parsed content as a key-value array.

/**
 * @param string $path The file to parse
 * @param array $defaults An array of default values
 * @return array The parsed content
 *
 * @throws \InvalidArgumentException
 * @throws \UnexpectedValueException
 */
public static function fromFile($path, array $defaults = []);

Parser class

parseString:

Reset parser then parse the provided string.

/**
 * @param string $string The string to parse
 * @return boolean
 */
public function parseString($string);

parseFile:

Reset parser then parse the provided file.

/**
 * @param string $path Path of the file to parse
 * @return boolean
 */
public function parseFile($path);

get:

Get parsed content array.

/**
 * @return array
 */
public function get();

errors:

Get parsing errors array.

/**
 * @return array
 */
public function errors();

Other packages

Feel free to visit my other packages:

bgaze/bootstrap-form

Bootstrap 4 & 5 forms builder for Laravel 12+

This package simplifies Bootstrap 4 & 5 forms creation in Laravel applications 12+
It renders Bootstrap 5 markup by default and fully supports Bootstrap 4 for backward compatibility.
Model form binding and automatic error display are supported, as well as most Bootstrap form features: form layouts, custom fields, input groups, and more.

Github Documentation

bgaze/laravel-kvstore

Key-value store for Laravel 12 & 13

A database-backed key-value store for Laravel 12 & 13, sized for settings: read on most requests, written rarely.
The whole store is cached as one single entry, so a cold read costs one query for every key at once.
Each entry carries its own cast, so a value always reads back as what it was written as.

Github Documentation

SnapStack

100% local browser captures for your AI assistant

SnapStack is a browser extension that captures any tab in one click and stacks it locally, so your AI assistant can read the screenshots on demand over MCP.
Nothing is ever uploaded: captures go only to a small server on your own machine. No account, no telemetry.
Works with any MCP-capable client (Claude Code and others), on Chrome, Edge and Firefox.

Github Documentation

@bgaze/color-palette

A grid colour picker in the spirit of Google Docs

Vanilla TypeScript, with a single runtime dependency for the positioning.
It works standalone, ships optional Bootstrap 4 and Bootstrap 5 themes, and is accessible on purpose.

Github Documentation