一个用于从环境变量中序列化环境的库

1.0.0 2020-05-24 20:04 UTC

This package is auto-updated.

Last update: 2024-09-19 23:30:12 UTC


README

Build Status Total Downloads Coverage Status License Scrutinizer Code Quality

php-env

🌎 PHP 的环境抽象 🌎

使用场景

如果您想在您和您的运行时环境之间设置边界,这个库可以帮助您。如果您喜欢使用 .env 文件,这个库也可以帮助您。

安装

composer require remotelyliving/php-env

使用方法

创建应用程序环境

<?php
// needs to be set before hand through docker or could grab the app environment from cli args
// EnvironmentType is an enum that you can extend to add more values to
$envType = new EnvironmentType(getenv('ENVIRONMENT'));
$envFile = "/my/app/envs/.{$envType}.env";

// we can now create the app environment
$env = Environment::createWithEnvFile($envType, $envFile);

// tells you which environment you're in
$env->is(EnvironmentType::DEVELOPMENT()); // true
$env->is(EnvironmentType::PRODUCTION()); // false

// tells you if a var exists
$env->has('FOO'); // true

// returns a value caster of a value that can then be called to get stricter types 
$env->get('FOO')->asArray();
$env->get('BAR')->asBoolean();
$env->get('BAR')->asInteger();