Loading environment values into PHP classes

libraryphpopensource

The Env library

The goal of this library is to be able to load Environment values into PHP Objects.

This library is a simple one where it loads basic types of values and does not cover arrays or any other logic except what is explained here.

Installation

If you have already set up your project in PHP and you want to install the library, then all you need to do is execute the following.

$ composer require didslm/env

Basic usage

After installing this library you can simply use it by importing it and passing the object.

The following example will show you, how you can use this with a simple object which contains attributes as a reference to the environment variable name.


<?php

use Env\EnvName;


class SomeApiClientConfig
{
    #[EnvName('EXAMPLE_API_BASE_URL')]
    public string $baseUrl;

    #[EnvName('EXAMPLE_API_ACCESS_TOKEN')]
    public string $accessToken;

    #[EnvName('EXAMPLE_API_SECRET_TOKEN')]
    public string $secretToken;
}

Advanced usage

In this other example you will see how you can use this library by having one central place for all your environment variables, distributed into their own objects.

The most important part here is the attribute inside objects which is named EnvName .


<?php

use Env\EnvName;
use Env\EnvObj;

class Config
{
    #[EnvObj]
    public DbConfig $mysqlConfig;

    #[EnvObj]
    public SomeApiClientConfig $apiExampleConfig
}

class SomeApiClientConfig
{
    #[EnvName('EXAMPLE_API_BASE_URL')]
    public string $baseUrl;
    #[EnvName('EXAMPLE_API_ACCESS_TOKEN')]
    public string $accessToken;
    #[EnvName('EXAMPLE_API_SECRET_TOKEN')]
    public string $secretToken;
}

class DbConfig
{
    #[EnvName('DB_HOST')]
    public string $dbHost;
    #[EnvName('DB_USER')]
    public string $username;
    #[EnvName('DB_PASSWORD')]
    public string $password;
    #[EnvName('DB_DATABASE')]
    public string $database;

}

$config = new Config();
Env::populate($config);

echo $config->mysqlConfig->dbHost;
$ some_host_123

Now you can see the attributes types in the objects which contain EnvObj and EnvName , the library will look and fill them with necessary data.

Contribution

The library is very new and does not contain much logic, if you are new into PHP and want to contribute to this project feel free to create a pull request and I will review your code.

Your chance to start contributing when a project is small are way higher because there is no learning curve there is just one method that does the job.

You can support by writing test scenarios which also help a lot on making the library more stable on future functionalities.

If you are not ready to contribute then I would appreciate any other support GitHub repository like give it a star ⭐ if you think it's worth it.

Conclusion

The env library will provide you with a simple functionality which can help you load your environment values into PHP objects.

You can easily start contributing into this library, by adding functionalities for supporting arrays.


If you enjoyed this, please give a clap or share with your network!

Suggested Posts

CQRS explained with examples

Read more →

Change your perspective on code review

Read more →

Kodi i pastër dhe çka duhet të konsideroni

Read more →