didslm/env 问题修复 & 功能扩展

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

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

didslm/env

最新稳定版本:v1.1.0

Composer 安装命令:

composer require didslm/env

包简介

A library which helps you load your environment variables into php classes.

README 文档

README

The PHP Environment Variable Loader is a library that simplifies the process of loading environment variables into PHP classes. It provides a convenient and efficient way to read and store environment variables in your PHP application. By using this library, you can easily manage your application's configuration variables, such as API keys, database credentials, and other settings, without hardcoding them into your codebase.

Installation

To install the library, you can use Composer. If you don't have Composer installed, please follow the instructions on the official website.

Run the following command to install the PHP Environment Variable Loader library:

composer require didslm/env

Usage

To use the library in your PHP code, you need to instantiate the Didslm\Env class and call the populate method:

class Config 
{
    #[EnvObj]
    public DbConfig $mysqlConfig;
}


class DbConfig
{
    #[EnvName('DB_HOST')]
    public string $dbHost;
    #[EnvName('DB_USER')]
    public string $username;
    #[EnvName('DB_PASSWORD')]
    public string $password;
    #[EnvName('DB_DATABASE')]
    public string $database;

}

$config = new DbConfig();
//The next line will populate the class properties with the env variables if they exist.
Env::populate($config);

$allConfig = new Config();
Env::populate($allConfig);

Advanced Usage

  • Default values
    Provide a fallback that is used when the environment variable (and .env) do not provide a value.

    class Defaults
    {
        #[EnvName('APP_REGION', default: 'eu-west-1')]
        public string $region;
    }
  • Strong typing
    Scalar properties are automatically converted to the correct PHP type (int, float, bool, string). For booleans the loader understands 1/0, true/false, on/off, and yes/no.

  • Nested configuration objects
    Use EnvObj to hydrate nested objects. Optionally provide the class name if it differs from the property type.

    class Services
    {
        #[EnvObj(ServiceConfig::class)]
        public ServiceConfig $billing;
    }
  • Custom .env location
    Env::populate($config, new DotEnvLoader($projectRoot, 'custom.env')); lets you point the loader to a specific directory or file without changing the default behaviour.

  • Helpful errors
    When a non-nullable property cannot be populated, the library now throws Env\Exception\MissingEnvironmentValue with clear information about the missing variable.

Troubleshooting

If you encounter any issues while using the library, please check the troubleshooting section of the documentation. If your issue is not covered in the troubleshooting section, please create a new issue on the GitHub repository.

Contributing

If you would like to contribute to the PHP Environment Variable Loader library, please read the contributing guidelines before submitting a pull request.

Contact

If you have any questions or feedback on the library, please feel free to contact me.

License

The PHP Environment Variable Loader library is open-source software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固