mattivdweem/phpdotenv
Composer 安装命令:
composer require mattivdweem/phpdotenv
包简介
Library to load in dotenv files
关键字:
README 文档
README
PHP dot env
PHP library to read envoirment variables.
.env?
.env files are envoirment files, files and data which you typacly don't want to share with the whole wide world. By the use of .env files you can easily give these files premissions and git ignore them.
There are tons of librarys and build in functions for this?
Well yea thats true, but this version works a bit different; We use an envoirment folder this makes it easy for switching envoirments, but will also help with keeping your code clean.
How to use it?
Prefered method: Composer:
require: "mattivdweem/phpdotenv": "1.1.3*@dev"
Optional method: clone or download the src folder and include it
require_once('src/Dotenv.php');
$Dotenv = new Dotenv;
Functions
Except the fact that its super easy to extend, and the part of reading folders intead of files.
Here is an example from my test files:
$Dotenv
->setScope(0)
->setOverwrite(false)
->useComponents()
->addRequired(array('CFOO'))
->load('.env/dev')
->load('.env/mysql.env')
->load('.env/github.json');
This is basicly everything it does in a small sum up.
You can read folders, .env files and json files to add more extensions go to 'extending phpdotenvi'.
You can change the scope and make the original envoirment files overwriteable. There is an option to useComponents defined loaded from the component folder (if not used, the defaults will be used).
You can add required items. (these will be ran on __destruction of the class)
And you can load files.
####Function by function
setScope(int)
Sets the depth of folders to be scanned for files.
0 means infinite
setOverwrite(bool)
When you have an other script running $_ENV vars you migth want to keep those, by turning the overwrite to false the vars won't be overwritten.
addComponents(string/array)
Files are matched against extension rules, make sure your prefered extension has a component to turn it into an array.
useComponents()
Developers are lazy guys, so if you want to be lazy and have no components in development phase, you can replace the systems extensions to all extensions found in the component folder.
addRequired(string/array)
You can match all your given inputs against required vars, this will throw an error if a required var does noet exists.
load(string)
This does what it says it does. it loads the given file.
Adding Components
The files get matched and run trough an component, the match is simply done with an extension. But getting an usable php array from an file is a bit more tricky, since every file is different.
I like working with json, so i added an json component (this is not magical or something, just an json_decode wrapped in a class). Since it is an dotenvi library there is also a little class to read .env files.
Creating a new component is easy, start off by simply duplicating the json example. Or by following the steps:
- create a new php file in the src/components folder named after your extension (e.g. json.php)
- create a class inside this php file called after your extension (e.g. json)
- create a
public function toArray($contents){}in your file, this function will be called and used to obtain the php array from you extension - inside the class you can do whatever you like as long the toArray function returns an array and recieves an string.
Example:
<?php
/*
* Custom component for custom extension
* @param extension = '.custom'
*/
class custom{
/*
* Default function to boot, this function should return an array of data
* @param string
* @return array
*/
public function toArray($contents){
return array();
}
}
I did this now what? You can register your component in different ways:
- set useComponents() on launch
- use addComponents('your comps')
- edit Dotenv.php //Not suggested
mattivdweem/phpdotenv 适用场景与选型建议
mattivdweem/phpdotenv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 135 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「environment」 「global」 「env」 「dotenv」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mattivdweem/phpdotenv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mattivdweem/phpdotenv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mattivdweem/phpdotenv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Environment processor and contexts autoloader
The tenancy/tenancy identification driver using environment variables
Multiple cascading environmental configuration files support for Laravel 5
An environment variable convenience library extension for vlucas/phpdotenv
Laravel dotenv manager
Utils to load, parse and work with configuration on Mezzio projects
统计信息
- 总下载量: 135
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-01-04