arrilot/dotenv-php 问题修复 & 功能扩展

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

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

arrilot/dotenv-php

Composer 安装命令:

composer require arrilot/dotenv-php

包简介

README 文档

README

Latest Stable Version Total Downloads Build Status Scrutinizer Quality Score

Simple dotenv for PHP

Introduction

Q: What's the point of this package? How is it any different from those vlucas/phpdotenv and josegonzalez/php-dotenv well-known packages?

A: Those great packages are NOT for production. They were always meant to be used during local development only. The main reasons are:

  1. Not fast enough
  2. Not secure enough

Many people are actually misuse those packages and use them to configure apps in production too.

In contrast this package IS for production. It uses plain old php array for .env content and doesn't touch $_ENV or $_SERVER by default. As a result it's fast and secure but has less features.

Installation

  1. composer require arrilot/dotenv-php

  2. Create .env.php file to store configuration settings that are environment specific or sensitive.

Example:

<?php

return [
    'DB_USER' => 'root',
    'DB_PASSWORD' => 'secret',
];

This file should NEVER be added to version control.

  1. Create .env.example.php file and add it to version control. This file should serve as an example for developers how .env.php file should look like.

  2. Load .env.php file

use Arrilot\DotEnv\DotEnv;
DotEnv::load('/path/to/.env.php'); 

Usage

Getting data

The most used case is to get dotenv variable.

$dbUser = DotEnv::get('DB_USER');

You may pass a second parameter, which is gonna be used as default if variable is not set.

$dbUser = DotEnv::get('DB_USER', 'admin');

Note This is the method you are going to use most of the time. It makes sense to add a global helper for it to avoid importing the class name and e.t.c.

function env($key, $default = null)
{
    return \Arrilot\DotEnv\DotEnv::get($key, $default);
}
...
$dbUser = env('DB_USER', 'admin');

You can also get all dotenv variables at once:

$variables = DotEnv::all();

Setting data

You can set or override specific variable like that:

DotEnv::set('DB_USER', 'admin');
DotEnv::set('DB_PASSWORD', 'secret');
// or
DotEnv::set([
    'DB_USER'     => 'root',
    'DB_PASSWORD' => 'secret',
]);

You can reload all variables entirely from file or array

DotEnv::load('/path/to/new/.env.php');
//or
DotEnv::load([
    'DB_USER'     => 'root',
    'DB_PASSWORD' => 'secret',
]);

Other methods

There is way to ensure that a specific dotenv variable exists. Example:

DotEnv::setRequired(['DB_USER', 'DB_PASSWORD']);

If the variable is not loaded an Arrilot\DotEnv\Exceptions\MissingVariableException will be thrown.

There are also convenient methods to copy all variables to putenv(), $_ENV or $_SERVER if you DO need it, but in most cases you don't

DotEnv::copyVarsToPutenv($prefix = 'PHP_'); // putenv()
DotEnv::copyVarsToEnv(); // $_ENV
DotEnv::copyVarsToServer() // $_SERVER

Testing

Q: Why are there so many static calls? How am I supposed to mock them in tests?

A: You shouldn't mock DotEnv class. Just override what you need using set or load methods. Note that load method understands arrays too.

统计信息

  • 总下载量: 37.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 36
  • 点击次数: 0
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 2
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-02-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固