datahihi1/tiny-env 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

datahihi1/tiny-env

Composer 安装命令:

composer require datahihi1/tiny-env

包简介

A simple environment variable loader for PHP applications

README 文档

README

A lightweight .env loader for PHP projects.

Fast, Safe, Simple — designed for small to medium projects.

Installation

composer require datahihi1/tiny-env:1.1.1

Quick Start

require 'vendor/autoload.php';

use Datahihi1\TinyEnv\TinyEnv;

$env = new TinyEnv(__DIR__);
$env->load();

echo env('DB_HOST', 'localhost');

.env file:

DB_HOST=127.0.0.1
DB_PORT=3306

Features

1. load() – Standard load

$env->load();                           // Load all
$env->load(specificKeys: ['DB_HOST']);  // Load specific keys
$env->load([], forceReload: true);      // Force reload (overwrite existing values)
$env->load([], noFile: true);           // Load without requiring .env file to exist

2. Fast load

$env = new TinyEnv(__DIR__, true); // Load immediately and populate $_SERVER|$_ENV but only .env and not recommended for production

3. Multiple .env files

$env->envfiles(['.env', '.env.production', '.env.local']); // Load in order, pre-declaration file has the highest priority by default (.env > .env.production > .env.local)
$env->envfiles(['.env.production', '.env.local'], prioritizeEnv: true); // Prioritize .env file by loading it first, allows overwriting other files

Allow specific stream wrappers (advanced)

By default, TinyEnv rejects some values that look like dangerous PHP stream wrappers (e.g. phar:, php://, data:) to reduce the chance that an env value is later used unsafely by your app.

If you intentionally need to use a wrapper such as phar://..., you can opt-in with an allowlist:

$env = new TinyEnv(__DIR__);
$env->allowWrapperSchemes(['phar']); // opt-in to allow phar://... values
$env->load();

Populate Superglobals

$env = new TinyEnv(__DIR__); // By default, superglobals are NOT populated to avoid unintended side effects. You can enable it explicitly:
$env->populateSuperglobals(); // Enable superglobals population
$env->populateServerglobals(); // Enable server globals population
$env->load();

Or use fastLoad which will always populate superglobals - But not recommended for production.

  • Getting Values
echo env('NAME');                // Get value
echo env('NOT_FOUND', 'backup'); // With default
print_r(env());                  // Get all (in .env file)
print_r(sysenv());               // Get all system variables
  • Validation

Using tiny-env-validator

  • Encryption

Using tiny-env-encryptor

Variable Interpolation

TinyEnv supports shell-style interpolation inside .env values:

DB_HOST=localhost
DB_PORT=3306
DB_URL=${DB_HOST}:${DB_PORT}

USER_NAME=
USER=${USER_NAME:-guest}   # default if unset or empty
ALT_USER=${USER_NAME-guest} # default if unset only
REQUIRED=${MISSING?Missing variable MISSING}

Result:

DB_URL="localhost:3306"

USER="guest" (because USER_NAME is empty)

ALT_USER="" (because USER_NAME exists but empty)

REQUIRED → throws Exception

Notes

  • Comments start with #.
  • Variable names: A-Z, 0-9, _.
  • Spaces around = still valid but not recommended.
  • Values are auto-parsed into correct types:
    • "true", "yes", "on"true
    • "false", "no", "off"false
    • "123"int
    • "12.3"float or double
    • "null" or empty → null
  • TinyEnv considers yes/no, on/off to be boolean values.
  • Use "/value/" to force string type.

datahihi1/tiny-env 适用场景与选型建议

datahihi1/tiny-env 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 473 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 datahihi1/tiny-env 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 datahihi1/tiny-env 我们能提供哪些服务?
定制开发 / 二次开发

基于 datahihi1/tiny-env 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 473
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-07