PHP Classes

PHP Configuration Trait: Load and save class configuration using a trait

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
StarStarStarStarStar 82%Total: 435 All time: 6,283 This week: 36Up
Version License PHP version Categories
configuration-trait 2.9MIT/X Consortium ...5.3PHP 5, Language, Configuration, Traits
Description 

Author

This package can load and save class configuration using a trait.

It defines a trait that can load and save configuration values into a variable from files in different formats.

Currently it supports the configuration file formats using Zend framework configuration classes: INI, XML, PHP array, JSON and YAML.

Innovation Award
PHP Programming Innovation award nominee
September 2015
Number 5


Prize: One downloadable e-book of choice by O'Reilly
Many classes have behaviors that can be configured using external configuration files loaded at run-time.

This package provides a common PHP trait that can easily add load and save configuration capabilities to any class using files in many formats: INI, XML, PHP Array, JSON and YAML.

Manuel Lemos
Picture of Asher Wolfstein
  Performance   Level  
Name: Asher Wolfstein <contact>
Classes: 15 packages by
Country: United States United States
Age: 41
All time rank: 1121164 in United States United States
Week rank: 178 Up22 in United States United States Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php

require_once('../../Features/Configuration.php');

use
Falcraft\Features;

class
ConfiguredClass {
    use
Features\Configuration;
   
    public function
__construct($configure)
    {
       
$this->configure($configure);
    }
   
    public function
getChildNode1()
    {
        return
$this->conf->parentNode->childNode1;
    }
   
    public function
getChildNode2()
    {
        return
$this->conf->parentNode->childNode2;
    }
   
    public function
getSingleNode()
    {
        return
$this->conf->singleNode;
    }
   
    public function
getTestHost($branch)
    {
        return
$this->conf->$branch->database->params->host;
    }
   
    public function
getTestDBName($branch)
    {
        return
$this->conf->$branch->database->params->dbname;
    }
}

echo
"Falcraft\\Features\\Configuration.php Test\n";
echo
"----------------------------------------\n\n";

echo
"Instantiating Configuration Class With No Args -> ";

$success = true;

try {
   
$configuredObject = new ConfiguredClass(array());
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
"Instantiating Configuration Class with Array -> ";

$success = true;

try {
   
$configuredObject = new ConfiguredClass(array (
       
'parentNode' => array(
           
'childNode1' => 'data1',
           
'childNode2' => 'data2',),
       
'singleNode' => 'data3'));
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
"Success!\n";
} else {
    echo
"Failure...\n";
}

echo
" Checking Configuration Variables -> \n";

$success = true;

$childNode1 = $childNode2 = $singleNode = null;

try {
   
$childNode1 = $configuredObject->getChildNode1();
   
$childNode2 = $configuredObject->getChildNode2();
   
$singleNode = $configuredObject->getSingleNode();
    if (!
$childNode1 || !$childNode2 || !$singleNode) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

if (
$success) {
    echo
" childNode1: $childNode1\n";
    echo
" childNode2: $childNode2\n";
    echo
" singleNode: $singleNode -- Success!\n";
} else {
    echo
" -- Failure!\n";
}

echo
"Instantiating Configuration Class with Ini File -> \n";

$success = true;

$childNode1 = $childNode2 = $singleNode = null;

try {
   
$configuredObject = new ConfiguredClass(array());
   
$configuredObject->configure(__DIR__ . '/test.ini');
   
$childNode1 = $configuredObject->getChildNode1();
   
$childNode2 = $configuredObject->getChildNode2();
   
$singleNode = $configuredObject->getSingleNode();
    if (!
$childNode1 || !$childNode2 || !$singleNode) {
       
$success = false;
    }
} catch (\
Exception $e) {
   
$success = false;
}

echo
" Checking Configuration Variables -> \n";

if (
$success) {
    echo
" childNode1: $childNode1\n";
    echo
" childNode2: $childNode2\n";
    echo
" singleNode: $singleNode -- Success!\n";
} else {
    echo
" -- Failure!\n";
}

echo
"Instantiating Configuration Class with XML File -> \n";

$success = true;

$testHostStaging = $testDBNameStaging = null;
$testHostProduction = null;

try {
   
$configuredObject = new ConfiguredClass(array());
   
$configuredObject->configure(__DIR__ . '/test.xml');
   
$testHostStaging = $configuredObject->getTestHost('staging');
   
$testDBNameStaging = $configuredObject->getTestDBName('staging');
   
$testHostProduction = $configuredObject->getTestHost('production');
} catch (\
Exception $e) {
   
$success = false;
}

echo
" Checking Configuration Variables -> \n";

if (
$success) {
    echo
" testHostStaging: $testHostStaging\n";
    echo
" testDBNameStaging: $testDBNameStaging\n";
    echo
" testHostProduction: $testHostProduction -- Success!\n\n";
} else {
    echo
" -- Failure!\n\n";
}

echo
"-- NOTE! testDBNameStaging may be empty, Zend may not be 'extending' properly\n\n";

echo
"Instantiating Configuration Class with JSON File -> \n";

$success = true;

$testHostStaging = $testDBNameStaging = null;
$testHostProduction = null;

//try {
   
$configuredObject = new ConfiguredClass(array());
   
$configuredObject->configure(__DIR__ . '/test.json');
   
   
$testHostStaging = $configuredObject->getTestHost('staging');
   
$testDBNameStaging = $configuredObject->getTestDBName('staging');
   
$testHostProduction = $configuredObject->getTestHost('production');
//} catch (\Exception $e) {
// $success = false;
//}

echo " Checking Configuration Variables -> \n";

if (
$success) {
    echo
" testHostStaging: $testHostStaging\n";
    echo
" testDBNameStaging: $testDBNameStaging\n";
    echo
" testHostProduction: $testHostProduction -- Success!\n\n";
} else {
    echo
" -- Failure!\n\n";
}

echo
"-- NOTE! testDBNameStaging may be empty, Zend may not be 'extending' properly\n\n";



  Files folder image Files (21)  
File Role Description
Files folder imageFalcraft (2 files, 2 directories)
Files folder imagesrc (1 directory)
Files folder imagetests (1 directory)
Accessible without login Plain text file composer.json Conf. composer
Accessible without login Plain text file LICENSE.txt Lic. MIT License
Accessible without login Plain text file phpunit.xml Test unit test config

  Files folder image Files (21)  /  Falcraft  
File Role Description
Files folder imageFeatures (2 files, 2 directories)
Files folder imagetests (1 directory)
  Accessible without login Plain text file composer.json Data Zend Framework
  Accessible without login Plain text file README.txt Doc. Important Version Information

  Files folder image Files (21)  /  Falcraft  /  Features  
File Role Description
Files folder imageException (2 files)
Files folder imageResource (1 file)
  Plain text file Configuration.php Class The Trait
  Accessible without login Plain text file falcraftLoad.php Aux. Loader for Falcraft

  Files folder image Files (21)  /  Falcraft  /  Features  /  Exception  
File Role Description
  Plain text file ClassDependencyException.php Class Local Class Dependency Exception
  Plain text file ExceptionInterface.php Class Local Exception Interface

  Files folder image Files (21)  /  Falcraft  /  Features  /  Resource  
File Role Description
  Plain text file ConfigurationInterface.php Class An interface for configuration implementors

  Files folder image Files (21)  /  Falcraft  /  tests  
File Role Description
Files folder imageFeatures (4 files)

  Files folder image Files (21)  /  Falcraft  /  tests  /  Features  
File Role Description
  Accessible without login Plain text file Configuration.php Example Test Script
  Accessible without login Plain text file test.ini Data ini format test file
  Accessible without login Plain text file test.json Data test json file
  Accessible without login Plain text file test.xml Data test xml file

  Files folder image Files (21)  /  src  
File Role Description
Files folder imagePhabstractic (1 file, 1 directory)

  Files folder image Files (21)  /  src  /  Phabstractic  
File Role Description
Files folder imageFeatures (1 file, 2 directories)
  Accessible without login Plain text file falcraftLoad.php Aux. class loader

  Files folder image Files (21)  /  src  /  Phabstractic  /  Features  
File Role Description
Files folder imageException (3 files)
Files folder imageResource (1 file)
  Plain text file ConfigurationTrait.php Class main class (trait)

  Files folder image Files (21)  /  src  /  Phabstractic  /  Features  /  Exception  
File Role Description
  Plain text file ClassDependencyException.php Class exception classification
  Plain text file ExceptionInterface.php Class basic exception interface
  Plain text file InvalidArgumentException.php Class exception classification

  Files folder image Files (21)  /  src  /  Phabstractic  /  Features  /  Resource  
File Role Description
  Plain text file ConfigurationInterface.php Class configuration interface

  Files folder image Files (21)  /  tests  
File Role Description
Files folder imagePhabstractic (1 directory)

  Files folder image Files (21)  /  tests  /  Phabstractic  
File Role Description
Files folder imageFeatures (1 file)

  Files folder image Files (21)  /  tests  /  Phabstractic  /  Features  
File Role Description
  Accessible without login Plain text file ConfigurationTrait.php Test unit test config

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 Reuses Unique User Downloads Download Rankings  
 0%5
Total:435
This week:0
All time:6,283
This week:36Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:100%StarStarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:83%StarStarStarStarStar
Videos:-
Overall:82%StarStarStarStarStar
Rank:8