PHP Classes

PHP Configuration Loader: Load configuration from YAML files into arrays

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 91 All time: 9,943 This week: 69Up
Version License PHP version Categories
php-configuration-lo 1.0.0Custom (specified...5PHP 5, Files and Folders, Configuration
Description 

Author

This class can load configuration from YAML files into arrays.

It can load a given configuration file in YAML format and extract the values defined in it into arrays.

Configuration value names with space characters are converted to underscores.

Picture of Vurghus Minar
  Performance   Level  

 

Example

<?php
require dirname(__DIR__) . '/vendor/autoload.php';

$config = array(
'test_number' => 10, 'test' => 'bla'
);

$config_from_array = new \Am\Config\Config($config, 0, false);
echo
$config_from_array->debug;
var_dump($config_from_array);

echo
"<br/><br/><hr><br/><br/>";



$config_file = array(
   
dirname(__DIR__) . '/examples/configs/config.yaml',
   
dirname(__DIR__) . '/examples/configs/config-2.yaml',
    array(
       
'hotel'=>'5 star',
       
'rooms'=> 10
   
)
);
$config_from_file = new \Am\Config\Config($config_file,1,true);

var_dump($config_from_file);

echo
"<br/><br/><hr><br/><br/>";



$config_file_with_error = dirname(__DIR__) . '/examples/configs/config-contains-mistakes.yaml';
$config_from_file_with_error = new \Am\Config\Config($config_file_with_error,0,true);

var_dump($config_from_file_with_error);

echo
"<br/><br/><hr><br/><br/>";



$config_arrays_only = array(
    array(
       
'test_number1' => 10,
       
'test1' => 'bla'
   
),
    array(
       
'hotel1'=>'5 star',
       
'rooms1'=> 10
   
)
);

$config_from__arrays_only = new \Am\Config\Config($config_arrays_only, 1, false);
echo
$config_from__arrays_only->debug;
var_dump($config_from__arrays_only);



Details

PHP Configuration Loader Class

About

The PHP configuration loader class helps the management and loading of application configuration through arrays or Yaml configuration files.

Installation

Via Composer:

composer require am/config

Or simply download the code and add to your project.

Example Use

config.yaml

root_path: C:/xampp/htdocs
base_url: /
directories:
  controllers: controllers
  models: models
  views: views
  core: core
test_number: 10

config-2.yaml

this name: config 2
this number: 10
test_number: 10

<?php
require dirname(__DIR__) . '/vendor/autoload.php';

$config_mixed_src = array(
	dirname(__DIR__) . '/examples/configs/config.yaml',
	dirname(__DIR__) . '/examples/configs/config-2.yaml',
	array(
		'hotel'=>'5 star',
		'rooms'=> 10
	)
);
$config = new \Am\Config\Config($config_mixed_src,1,true);

var_dump($config);

Resulting dump:

<?php
object(Am\Config\Config)[2]
  public 'config' => 
    array (size=3)
      0 => string 'C:\xampp\htdocs\projects\composer\config/examples/configs/config.yaml' (length=69)
      1 => string 'C:\xampp\htdocs\projects\composer\config/examples/configs/config-2.yaml' (length=71)
      2 => 
        array (size=2)
          'hotel' => string '5 star' (length=6)
          'rooms' => int 10
  public 'src' => int 1
  public 'debug' => boolean true
  public 'root_path' => string 'C:/xampp/htdocs' (length=15)
  public 'base_url' => string '/' (length=1)
  public 'directories' => 
    array (size=4)
      'controllers' => string 'controllers' (length=11)
      'models' => string 'models' (length=6)
      'views' => string 'views' (length=5)
      'core' => string 'core' (length=4)
  public 'test_number' => int 10
  public 'this_name' => string 'config 2' (length=8)
  public 'this_number' => int 10
  public 'hotel' => string '5 star' (length=6)
  public 'rooms' => int 10

You can now retrieve application configuration within your file as public vars as follows:


echo $config->hotel; //returns "5 star"

foreach($config->directories as $dir){
	echo $dir;
}

Additional Resources

How to Install LibYAML

To allow PHP to parse YAML files, you will require php libyaml available.

https://github.com/LegendOfMCPE/LoM-CMS/wiki/How-to-Install-LibYAML

https://stackoverflow.com/questions/27122701/installing-php-yaml-extension-with-xampp-on-windows

Note for Windows Xampp users, it is useful to copy yaml.dll to your xampp php folder as well to allow it to run in CLI (important mainly while running tests).


  Files folder image Files (208)  
File Role Description
Files folder imagedocs (1 directory)
Files folder imageexamples (1 file, 1 directory)
Files folder imagesrc (1 file)
Files folder imagetests (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpdoc.dist.xml Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file readme.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:91
This week:0
All time:9,943
This week:69Up