folded/config

Configuration utilities for your PHP web app.

v0.1.5 2020-10-03 12:38 UTC

This package is auto-updated.

Last update: 2024-10-17 17:30:30 UTC


README

Configuration and environment utilities for your PHP web app.

Packagist License Packagist PHP Version Support Packagist Version Build Status Maintainability TODOs

Summary

About

I created this library to get an easy to setup way to perform configuration/environment value fetch.

Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.

Features

  • Can get configuration or .env values (using dot syntax)
  • Can check if a configuration or an env value exist
  • Eager load the configuration, such as it is only booted when you get a configuration or an env value

Requirements

  • PHP version >= 7.4.0
  • Composer installed

Installation

1. Install the package

In your project root directory, run this command:

composer require folded/config

2. Set up the library

Right before getting the config or env values, integrate this script:

use function Folded\setConfigFolderPath;
use function Folded\setEnvFolderPath;

setEnvFolderPath(__DIR__);
setConfigFolderPath(__DIR__ . "/config");

You don't have to set up both if you only want to use one or the other. However, if you plan to use both, make sure that setEnvFolderPath() is called before setConfigFolderPath() in order to take advantage of env variables in configurations.

The env folder path is the folder that contains your .env file.

Examples

Since this library uses vlucas/phpdotenv for the env feature, you can learn more on this file format in the documentation.

1. Getting a configuration value

In this example, we will get a configuration value.

use function Folded\getConfig;

echo getConfig("app.name");

Asuming your configuration file is located at config/app.php, and contains:

return [
	"name" => "Folded",
];

2. Getting an env value

In this example, we will get a value stored in the .env file.

echo Folded\getEnv("APP_NAME");

Asuming your .env file contains:

APP_NAME="Your app name"

3. Checking if a configuration exist

In this example, we will check if a configuration key exist.

use function Folded\hasConfig;

if (hasConfig("app.name")) {
	echo "config exist";
} else {
	echo "config don't exist";
}

4. Check if an env value exist

In this example, we will check if an env variable exist.

use function Folded\hasEnv;

if (hasEnv("APP_NAME")) {
	echo "has env";
} else {
	echo "has not env";
}

5. Get all config variables

In this example, we will get all the key/values inside the config file app.php.

use function Folded\getAllConfig;

$app = getAllConfig("app");

echo $app["name"];

Version support