devcirclede/env-reader

Simple Env Reader/Parser for PHP

Installs: 36

Dependents: 1

Suggesters: 0

Security: 0

Stars: 3

Watchers: 3

Forks: 0

Open Issues: 3

Type:project

0.4.3 2023-03-19 21:24 UTC

This package is auto-updated.

Last update: 2024-05-20 00:04:58 UTC


README

68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f646576636972636c652d64652f456e765265616465722e737667 workflow 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f546573742532307061636b6167652532306f6e2d706c61792e70687073616e64626f782e696f2d626c75652e737667

EnvReader

PHP Environment Reader

Simple Environment Reader which can parse the Value to a specific type. It tries to find the Value in $_ENV, $_SERVER and via getenv. The logic is leaned on the EnvVarProcessor from Symfony.

Installation

composer require devcirclede/env-reader

Supported Types

Actual included Types are:

  • integer
  • float
  • string
  • boolean
  • array
  • json

You can add your own Type by creating a class which implements the TypeInterface.

Example:

<?php

declare(strict_types=1);

namespace Company\EnvTypes;

use DevCircleDe\EnvReader\Types\TypeInterface;

class CustomType implements TypeInterface
{
    public function getName(): string
    {
        return 'custom';
    }
    
    public function convert(string $value): mixed
    {
        // convert the value to custom type
        return $value;
    }
}

Usage of the CustomType:

<?php

use Company\EnvTypes\CustomType;
use DevCircleDe\EnvReader\EnvParser;

$envParser = EnvParser::getInstance();
// add custom type
$envParser->getCollection()->addItem(new CustomType());

// read Env
$var = $envParser->parse('FOO', 'custom_type');