zero-to-prod/phpdotenv
Composer 安装命令:
composer require zero-to-prod/phpdotenv
包简介
Loads environment variables from `.env` to `getenv()`, `$_ENV`
关键字:
README 文档
README
Contents
- Introduction
- Requirements
- Installation
- Documentation Publishing
- Usage
- Local Development
- Contributing
Introduction
Loads environment variables from .env to getenv(), $_ENV
Requirements
- PHP 7.1 or higher.
Installation
Install Zerotoprod\Phpdotenv via Composer:
composer require zero-to-prod/phpdotenv
This will add the package to your project’s dependencies and create an autoloader entry for it.
Documentation Publishing
You can publish this README to your local documentation directory.
This can be useful for providing documentation for AI agents.
This can be done using the included script:
# Publish to default location (./docs/zero-to-prod/phpdotenv) vendor/bin/zero-to-prod-phpdotenv # Publish to custom directory vendor/bin/zero-to-prod-phpdotenv /path/to/your/docs
Automatic Documentation Publishing
You can automatically publish documentation by adding the following to your composer.json:
{
"scripts": {
"post-install-cmd": [
"zero-to-prod-phpdotenv"
],
"post-update-cmd": [
"zero-to-prod-phpdotenv"
]
}
}
Usage
Basic Usage
The Phpdotenv class provides two static methods for parsing environment variables:
1. Parse from String
Parse environment variables directly from a string:
use Zerotoprod\Phpdotenv\Phpdotenv; $env_content = "APP_NAME=MyApp\nAPP_ENV=production\nAPP_DEBUG=false"; $variables = Phpdotenv::parseFromString($env_content); // Result: // [ // 'APP_NAME' => 'MyApp', // 'APP_ENV' => 'production', // 'APP_DEBUG' => 'false' // ]
2. Parse from Array
Parse environment variables from an array of lines:
use Zerotoprod\Phpdotenv\Phpdotenv; $lines = [ 'APP_NAME=MyApp', 'APP_ENV=production', 'APP_DEBUG=false' ]; $variables = Phpdotenv::parse($lines); // Result: // [ // 'APP_NAME' => 'MyApp', // 'APP_ENV' => 'production', // 'APP_DEBUG' => 'false' // ]
Loading from .env File
use Zerotoprod\Phpdotenv\Phpdotenv; // Read .env file and parse it $env_file = file_get_contents(__DIR__ . '/.env'); $variables = Phpdotenv::parseFromString($env_file); // Set variables in environment foreach ($variables as $key => $value) { putenv("$key=$value"); $_ENV[$key] = $value; $_SERVER[$key] = $value; } // Now you can access them echo getenv('APP_NAME'); // MyApp echo $_ENV['APP_ENV']; // production
Supported .env File Format
The parser supports standard .env file syntax:
# Comments (lines starting with #) # Application Settings APP_NAME=MyApplication APP_ENV=production APP_DEBUG=false # Database Configuration DATABASE_URL="mysql://user:pass@localhost:3306/mydb" DATABASE_POOL_SIZE=10 # Empty values OPTIONAL_SETTING= # Values with equals signs CONNECTION_STRING=server=localhost;database=mydb;uid=user;pwd=pass # Quoted values API_KEY='secret-key-123' API_SECRET="another-secret" # Mixed quotes in values DESCRIPTION="App with 'single' quotes inside"
Features
Comments
Lines starting with # are treated as comments and ignored:
$result = Phpdotenv::parseFromString("# This is a comment\nAPP_NAME=MyApp"); // ['APP_NAME' => 'MyApp']
Quote Handling
The parser automatically strips surrounding quotes from values:
$result = Phpdotenv::parseFromString('API_KEY="secret-123"'); // ['API_KEY' => 'secret-123'] $result = Phpdotenv::parseFromString("API_KEY='secret-123'"); // ['API_KEY' => 'secret-123']
Whitespace Trimming
Leading and trailing whitespace is automatically trimmed from keys and values:
$result = Phpdotenv::parseFromString(' APP_NAME = MyApp '); // ['APP_NAME' => 'MyApp']
Empty Values
Keys can have empty values:
$result = Phpdotenv::parseFromString('OPTIONAL_KEY='); // ['OPTIONAL_KEY' => '']
Values with Equals Signs
Values can contain equals signs:
$result = Phpdotenv::parseFromString('CONNECTION=server=localhost;db=mydb'); // ['CONNECTION' => 'server=localhost;db=mydb']
Newline Format Support
The parseFromString method handles all newline formats:
// Unix newlines (\n) $result = Phpdotenv::parseFromString("KEY1=value1\nKEY2=value2"); // Windows newlines (\r\n) $result = Phpdotenv::parseFromString("KEY1=value1\r\nKEY2=value2"); // Mac newlines (\r) $result = Phpdotenv::parseFromString("KEY1=value1\rKEY2=value2"); // Mixed newlines $result = Phpdotenv::parseFromString("KEY1=value1\r\nKEY2=value2\nKEY3=value3");
Empty Lines
Empty lines are automatically filtered out:
$result = Phpdotenv::parseFromString("KEY1=value1\n\n\nKEY2=value2"); // ['KEY1' => 'value1', 'KEY2' => 'value2']
Duplicate Keys
If a key appears multiple times, the last value wins:
$result = Phpdotenv::parseFromString("API_KEY=first\nAPI_KEY=second"); // ['API_KEY' => 'second']
Invalid Lines
Lines without an equals sign are ignored:
$result = Phpdotenv::parseFromString("VALID=value\nINVALID_LINE\nANOTHER=value"); // ['VALID' => 'value', 'ANOTHER' => 'value']
API Reference
Phpdotenv::parseFromString(string $subject): array
Parses a string containing environment variables into an associative array.
Parameters:
$subject(string): The string containing environment variables (with newlines)
Returns:
array: Associative array of key-value pairs
Example:
$result = Phpdotenv::parseFromString("APP_NAME=MyApp\nAPP_ENV=production"); // ['APP_NAME' => 'MyApp', 'APP_ENV' => 'production']
Phpdotenv::parse(array $lines): array
Parses an array of lines into an associative array of key-value pairs.
Parameters:
$lines(array): Array of strings, each representing a line from an env file
Returns:
array: Associative array of key-value pairs
Example:
$result = Phpdotenv::parse(['APP_NAME=MyApp', 'APP_ENV=production']); // ['APP_NAME' => 'MyApp', 'APP_ENV' => 'production']
Pure Functions
Both methods are pure functions with no side effects. They:
- Do not modify the global environment (
$_ENV,$_SERVER,putenv()) - Do not read from the filesystem
- Simply parse and return an associative array
This design gives you full control over what happens with the parsed variables.
Contributing
Contributions, issues, and feature requests are welcome! Feel free to check the issues page if you want to contribute.
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch). - Commit changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature-branch). - Create a new Pull Request.
zero-to-prod/phpdotenv 适用场景与选型建议
zero-to-prod/phpdotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.78k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpdotenv」 「zero-to-prod」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zero-to-prod/phpdotenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zero-to-prod/phpdotenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zero-to-prod/phpdotenv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A tool to edit phpdotenv files
A wrapper with a fluent interface for new and old versions of vlucas/phpdotenv or symfony/dotenv, providing a common interface to easily read values from .env files
Enable env variables to Kirby
Array helpers.
A Generic Factory Pattern for Creating Arrays.
Build a Factory for Anything
统计信息
- 总下载量: 1.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-29
