senhung/config-read-write

This package is abandoned and no longer maintained. No replacement package was suggested.

A package for reading and writing config file

v2.0.0 2018-05-15 14:54 UTC

This package is not auto-updated.

Last update: 2020-01-24 17:53:06 UTC


README

Description

A PHP package for reading and setting configuration fast and easy.

Setup

  1. Add Dependency
$ composer require senhung/config-read-write
  1. Add a Configuration File

Create a file and input the configurations

For example:

# Some Comments
APP_NAME=config-read
VERSION=2.0.0

How To Use

Initialize

Configruation::initializeConfigs(
    [string $configFilePath [, bool $absolutePath [, string $separator]]]
): void

$configFilePath: the location your config file is placed (default: '.env')

$absolutePath: the path defined is absolute path or relative path (default: true)

$separator: the separator between config keys and values (default: '=')

Config Entry

Add the following code in your program's main entry if you want to specify a different config file name or separator other than the default ones

<?php

require_once 'vendor/autoload.php';

use Senhung\Config\Configuration;

/* Initialize config array in Configuration class */
Configuration::initializeConfigs('config_file_path', true, 'separator');

Note: you don't need to have this file if you are using .env as your config path and = as your separator.

Comment Config

Use # in config file to comment a line

# This is a comment, will not be read by the tool
OTHER_CONFIG=will-be-read

Note: Set config will overwrite comments and empty lines in original config file

Read Config

Configuration::read('<config-you-want-to-read>');

Write Config

Configuration::set('<config-you-want-to-write>', '<change-to>');

Note: Set config will overwrite comments and empty lines in original config file

Example

Configuration File

# App Configs
APP_NAME=config-read
VERSION=2.0.0

Config Reading and Writing

<?php

require_once 'vendor/autoload.php';

use Senhung\Config\Configuration;

/* Read config APP_NAME */
echo Configuration::read('APP_NAME') . "\n";

/* Read config VERSION */
echo Configuration::read('VERSION') . "\n";

/* Set APP_NAME to config-write */
Configuration::set('APP_NAME', 'config-write');

/* Read APP_NAME again to see the changes */
echo Configuration::read('APP_NAME') . "\n";

Output:

config-read
2.0.0
config-write