devcirclede / attrenv
Library to mark a property or class parseable for EnvReader
Requires
- php: ^8.1
- php-di/php-di: ^7.0
Requires (Dev)
- devcirclede/env-reader: *
- mockery/mockery: ^1.5
- overtrue/phplint: ^9.0
- phpunit/phpunit: ^10.0
- slevomat/coding-standard: ^8.8
- symfony/var-dumper: ^6.2
- vimeo/psalm: ^5.7
This package is auto-updated.
Last update: 2024-05-03 11:25:33 UTC
README
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);