承接 devcirclede/attrenv 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

devcirclede/attrenv

最新稳定版本:0.3.0

Composer 安装命令:

composer require devcirclede/attrenv

包简介

Library to mark a property or class parseable for EnvReader

README 文档

README

workflow

Attrenv - Extension for DevCircleDe/EnvReader

With the Attrenv you can instantiate classes with Env Variable values.

Installation

composer require devcirclede/attrenv

(It is recommended that you have installed the EnvReader.)

Description

The Parser uses the property name as the ENV Name. But you can also pass a custom ENV Name to the Attribute. Each Parameter/Property must be Type hinted, or you must declare the Type in the EnvironmentValue Attribute.

The EnvironmentValue only supports Types which the EnvReader supports.

If you have created your classes you can automatically load the Values from Env and instantiate your class with the AttributeParser.

USAGE

Usage Attrenv:

<?php

use DevCircleDe\Attrenv\Attribute\EnvironmentValue;
use DevCircleDe\Attrenv\Attrenv;

class EnvConfiguredClass {

    #[EnvironmentValue]
    private string $secret;
    
    #[EnvironmentValue(envName: 'ENV_NAME')]
    private string $betterInternalName;
    
    #[EnvironmentValue(type: 'json', default: ['simple' => 'better'])]
    private array $configArrayJson;
    
    public function __construct(
        #[EnvironmentValue]
        private readonly string $databaseName,
        #[\SensitiveParameter]
        #[EnvironmentValue]
        string $secret,
        private readonly array $optional = []
    ) {
    
    }
    
}

$envConfiguredClass = (new Attrenv())->getParser()->parse(EnvConfiguredClass::class);

You have also the possibility to use your own EnvParser Implementation.

Create your own Parser and implement the DevCircleDe\Attrenv\Decorator\EnvParserInterface.

The Attrenv uses the php-di package for dependency injection.

For autoload the Config, create a config file DOCUMENT_ROOT/config/attrenv-config.php:

<?php

return [
    'DevCircleDe\Attrenv\Decorator\EnvParserInterface' => new CustomParser(),
];

If you want to load it manual, create your file anywhere you want and load it:

$envConfiguredClass = (new Attrenv('your/path/to/config.php'))->getParser()->parse(EnvConfiguredClass::class);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-17