horat1us/environment-config

Environment Config class

Maintainers

👁 Horat1us

Package info

github.com/Horat1us/environment-config

pkg:composer/horat1us/environment-config

Statistics

Installs: 15 465

Dependents: 19

Suggesters: 1

Stars: 3

Open Issues: 1

1.6.0 2025-06-04 18:34 UTC

Requires

  • php: >=8.4

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT fbfcd811f03c87ea93c8e255d029c551f78b5c2f

  • Alexander Letnikow <reclamme.woop@gmail.com>

This package is auto-updated.

Last update: 2026-06-04 20:46:58 UTC


README

👁 Latest Stable Version
👁 Total Downloads
👁 codecov
👁 Test & Lint

Simple class to provide config using getenv function with prefix.

Compatibility: tested on PHP 7.1, PHP 8.1

Changelog

Installation

Using composer:

composer require horat1us/environment-config

Usage

Implement your own config class:

<?php

namespace App;

use Horat1us\Environment;

class Config extends Environment\Config {
 public function getTimeout(): int
 {
 return $this->getEnv($key = 'APP_TIMEOUT', $default = 10);
 }
 
 public function getSlow(): string
 {
 // default can be instance of \Closure or callable array, like [$this, 'calculate']
 return $this->getEnv($key = 'APP_KEY', $default = function(): string {
 return 'some-string'; // slow operation, may be fetching from DB 
 });
 }
 
 public function getNullValue(): ?string
 {
 /**
 * if you want to return null instead of throwing exceptio
 * if no environment variable found
 */
 return $this->getEnv('KEY', [$this, 'null']); 
 }
 
 public function getName(): string {
 return $this->getEnv($key = 'APP_NAME');
 }
}

then use it:

<?php

use App;

$config = new App\Config("PREFIX_");
$config->getTimeout(); // 10

putenv("PREFIX_APP_TIMEOUT=5");
$config->getTimeout(); // 5

$config->getSlow(); // some-string

// MissingEnvironmentException will be thrown because no default value provided
$config->getName(); 

MagicTrait

You can define your config keys/methods using MagicTrait:

<?php

use Horat1us\Environment;

class Config {
 use Environment\MagicTrait {
 getEnvironment as public getHost;
 }
 
 protected function getEnvironmentKeyPrefix(): string {
 return 'TEST_';
 }
}

$config = new Config;
$config->getHost(); // TEST_HOST environment key will be used to get value

Note: your environment getters should be named with prefix get and have camel case name

Author

License

MIT