2017 © Pedro Peláez
 

library environment-config

Environment Config class

image

horat1us/environment-config

Environment Config class

  • Friday, April 20, 2018
  • by Horat1us
  • Repository
  • 1 Watchers
  • 3 Stars
  • 1,751 Installations
  • PHP
  • 7 Dependents
  • 1 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 195 % Grown

The README.md

Environment Config

Latest Stable Version Total Downloads codecov Test & Lint, (*1)

Simple class to provide config using getenv function with prefix., (*2)

Compatibility: tested on PHP 7.1, PHP 8.1, (*3)

Changelog, (*4)

Installation

Using composer:, (*5)

composer require horat1us/environment-config

Usage

Implement your own config class:, (*6)

<?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:, (*7)

<?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:, (*8)

<?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, (*9)

Author

License

MIT, (*10)

The Versions

20/04 2018

dev-master

9999999-dev

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Letnikow

20/04 2018

1.2.1

1.2.1.0

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Letnikow

20/04 2018

dev-feature/null-method

dev-feature/null-method

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Letnikow

20/04 2018

1.2.0

1.2.0.0

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Letnikow

11/04 2018

1.1.1

1.1.1.0

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Letnikow

11/04 2018

1.1.0

1.1.0.0

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Letnikow

11/04 2018

1.0.1

1.0.1.0

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Letnikow

11/04 2018

1.0.0

1.0.0.0

Environment Config class

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by Alexander Letnikow