2017 © Pedro Peláez
 

library phpconfig

Free PHP configuration file tools for neat and powerful projects!

image

miladrahimi/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  • Thursday, August 20, 2015
  • by miladrahimi
  • Repository
  • 2 Watchers
  • 2 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 4 % Grown

The README.md

Build Status, (*1)

PhpConfig

A PHP configuration tool, (*2)

Overview

PhpConfig is a minimal package for adding configuration tool to your PHP project. It provides different types of config repositries such as runtime array, php file, directory of php files, json file and directory of json files., (*3)

Installation

Run following command in the root directory of your project:, (*4)

composer require miladrahimi/phpconfig

Getting started

First of all, you need to provide configuration data to PhpConfig. It supports different types of repositories like php file, json file, and you can use anyone you are interested in., (*5)

As an example we use array repository as the configuration data source. The following example demonstrates how to use an array of data as the data source, how to access data in your code and also how to go deeper using . operator., (*6)

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\ArrayRepository;

$data = [
    'name' => 'MyProject',
    'mysql' => [
        'hostname' => '127.0.0.1',
        'username' => 'root',
        'password' => 'secret',
        'database' => 'my_database'
    ]
];

$repository = new ArrayRepository($data);
$config = new Config($repository);

$name = $config->get('name'); // "MyProject"
$mysql_username = $config->get('mysql.username', 'root'); // "root"
$mysql_password => $config->get('mysql.password', ''); // "secret"
$locale = $config->get('locale', 'en'); // "en" (Default)

// Runtime config setting/manipulating
$config->set('locale', 'fa');

$locale = $config->get('locale', 'en'); // "fa"

Repositories

The example above illustrates how to use a defined array as the respository for PhpConfig. In this section, we introduce some other repositories., (*7)

Config PHP File

You can put the data array into a separate PHP file this way:, (*8)

<?php

return [
    'name' => 'MyProject',
    'mysql' => [
        'hostname' => '127.0.0.1',
        'username' => 'root',
        'password' => 'secret',
        'database' => 'my_database'
    ]
];

And to use the config file:, (*9)

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\FileRepository;

$repository = new FileRepository('config.php');
$config = new Config($repository);

$mysql_username = $config->get('mysql.username');

Config Directory

You probably need to split your configuration data into multiple files in a larger scale. Don't worry about it, it will be handled so easy!, (*10)

Consider the config files in a directory like this:, (*11)

Project
- index.php
- configs
- - app.php
- - mysql.php

And app.php :, (*12)

<?php

return [
    'name' => 'MyProject',
    'locale' => [
        'code' => 'fa',
        'name' => 'Farsi (Persian)',
    ],
    'url' => 'https://example.com'
];

And mysql.php:, (*13)

<?php

return [
    'hostname' => '127.0.0.1',
    'username' => 'root',
    'password' => 'secret',
    'database' => 'my_database'
];

And the main code:, (*14)

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\DirectoryRepository;

$config = new Config(new DirectoryRepository('configs'));

$app_locale_code = $config->get('app.locale.code', 'en');
$mysql_username = $config->get('mysql.username', 'default-username');

Config JSON File

PHP array syntax looks very nice but you may prefer JSON, if so, there's a good news, you can write your configs in JSPN format like the following example., (*15)

The config.json:, (*16)

{
    "app": {
        "name": "MyProject",
        "locale": {
            "code": "fa",
            "name": "Farsi (Persian)"
        }
    }
}

And the main code:, (*17)

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\JsonFileRepository;

$config = new Config(new JsonFileRepository('config.json'));

$app_locale_code = $config->get('app.locale.code', 'en'); // "fa"

Config JSON Directory

A Single JSON configuration file still has the same single php file problem, so we have implemented another repository type for multiple JSON files., (*18)

Consider the config files in a directory like this:, (*19)

Project
- index.php
- configs
- - app.json
- - mysql.json

And app.json :, (*20)

{
    "name": "MyProject",
    "locale": {
        "code": "fa",
        "name": "Farsi (Persian)"
    }
}

And mysql.json:, (*21)

{
    "hostname": "localhost",
    "username": "root",
    "password": "secret",
    "database": "my_project_db"
}

And the main code:, (*22)

use MiladRahimi\PhpConfig\Config;
use MiladRahimi\PhpConfig\Repositories\JsonDirectoryRepository;

$config = new Config(new JsonDirectoryRepository('configs'));

$app_locale_code = $config->get('app.locale.code', 'en'); // "fa"
$mysql_username = $config->get('mysql.username', 'default-username');

Custom Repositories

You can also make your own repositories by implementing following interface:, (*23)

<?php

namespace MiladRahimi\PhpConfig\Repositories;

interface Repository
{
    /**
     * Get configuration data
     *
     * @return array
     */
    public function getData(): array;
}

License

PhpConfig is initially created by Milad Rahimi and released under the MIT License., (*24)

The Versions

20/08 2015

dev-master

9999999-dev http://miladrahimi.github.io/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting

20/08 2015

1.6

1.6.0.0 http://miladrahimi.github.io/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting

26/07 2015

1.5

1.5.0.0 http://miladrahimi.github.io/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting

26/07 2015

1.4

1.4.0.0 http://miladrahimi.github.io/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting

26/07 2015

1.3

1.3.0.0 http://miladrahimi.github.io/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting

10/07 2015

v1.2

1.2.0.0 http://miladrahimi.github.io/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting

10/07 2015

v1.1

1.1.0.0 http://miladrahimi.github.io/phpconfig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting

03/07 2015

v1.0

1.0.0.0 http://neatplex.com/project/phpcofnig

Free PHP configuration file tools for neat and powerful projects!

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

file configuration config setting