Login   Register  
PHP Classes
elePHPant
Icontem

File: IniParser-Sample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Yves Feupi Lepatio  >  IniFileParser  >  IniParser-Sample.php  >  Download  
File: IniParser-Sample.php
Role: Example script
Content type: text/plain
Description: Sample File
Class: IniFileParser
Manipulate INI file configuration values
Author: By
Last change: This example script shows you how to use this package to create and manipulate INI config.
Date: 3 years ago
Size: 5,365 bytes
 

Contents

Class file image Download
<style type="text/css">
    * { font-family: Verdana, sans-serif; font-size: 11px;} table { margin-bottom: 10px; border-collapse: collapse; width: 500px; } caption { background-color: #F7F7F7; } th { text-align: left; font-weight: normal; width: 250px; } th, td, caption { border: 1px solid gray; padding: 5px; } .style1 { color: green; } .style2 { color: olive; } .style3 { color: red; }
</style>

<?php

/** @see FLY_IniParser */
require_once 'FLY/IniParser.php';

/** @see FLY_IniParser_SectionManagement */
require_once 'FLY/IniParser/SectionManagement.php';

/** @see FLY_IniParser_Section */
require_once 'FLY/IniParser/Section.php';

/** @see FLY_IniParser_Key */
require_once 'FLY/IniParser/Key.php';


// ============================================================================

/**
 * Returns the string that represents a HTML form of sections list.
 *
 * @param FLY_IniParser $iniParser
 * @return string
 */
function ToHtml(FLY_IniParser $iniParser)
{
    
$sectionManagement $iniParser->getSectionManagement();

    if ( ! 
$sectionManagement->hasSections())
    {
        return 
'<p class="style3">No sections!</p>';
    }

    
$output '<h2>Text Form:</h2>'
            
'<pre>' $sectionManagement->toString() . '</pre>'
            
'<h3>HTML Form:</h3>';

    
/* @var $section FLY_IniParser_ISection */
    
foreach ($sectionManagement->getSections() as $section)
    {
        
$output .= '<table><caption>' $section->getName() . '</caption>';

        if ( ! 
$section->hasKeys())
        {
            
$output .= '</table>';
            continue;
        }

        
$output .= '<tbody>';

        
/* @var $key FLY_IniParser_IKey */
        
foreach ($section->getKeys() as $key)
        {
            
$keyName $key->getName();
            foreach (
$key->getValues() as $value)
            {
                
$output .= '<tr><th>' $keyName '</th><td>' $value '</td></tr>';
            }
        }

        
$output .= '</tbody></table>';
    }

    return 
$output;
}

// ============================================================================

/*-
 * Build a short configuration manually.
 */

$sectionManagement =
    
FLY_IniParser_SectionManagement::CreateSectionManagement()
    ->
addSection(
        
FLY_IniParser_Section::CreateSection('production')
        ->
addKey(
            new 
FLY_IniParser_Key('phpSettings.display_startup_errors''0')
        )
        ->
addKey(
            new 
FLY_IniParser_Key('phpSettings.display_errors''0')
        )
    )
    ->
addSection(
        new 
FLY_IniParser_Section('staging : production')
    )
    ->
addSection(
        
FLY_IniParser_Section::CreateSection('testing : production')
        ->
addKey(
            new 
FLY_IniParser_Key('phpSettings.display_startup_errors''1')
        )
        ->
addKey(
            new 
FLY_IniParser_Key('browsers[]', array('Mozilla''Chrome''Opera'))
            
/*
            OR:
            FLY_IniParser_Key::CreateKey('browsers[]')
            ->addValue('Mozilla')
            ->addValue('Chrome')
            ->addValue('Opera')

            OR:
            FLY_IniParser_Key::CreateKey('browsers[]')
            ->addValue(array('Mozilla', 'Chrome', 'Opera'))
            */
        
)
    );

echo 
'<h2>Build a short config manually:</h2>',
     
'<pre>'$sectionManagement->toString(), '</pre>',
     
'<p>----------------------------------</p>';

$iniParser =
    
FLY_IniParser::CreateIniParser(
        
$sectionManagement /* try 'new FLY_IniParser_SectionManagement()' and note the logical difference in the display */
    
)->parseFromString(<<<CONTENT
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
bootstrap.class = "Bootstrap"
appnamespace = "Application"

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
resources.view[] = Home
resources.view[] = Account
resources.view[] = Article
zendX.activate = off
CONTENT
);

echo 
ToHtml($iniParser);

$sectionManagement $iniParser->getSectionManagement();

$sectionManagement->removeSection('production');
$iniParser->toFile('IniParser-Sample.ini');

echo <<<HTML
<div>
    <p class="style1">Remove the 'production' section and save modifications in the ini file (check it !)</p>
    <p class="style2">Load the ini file and make some modifications (see below)</p>
</div>
HTML;

$iniParser->parseFromFile('IniParser-Sample.ini');

$sectionManagement
    
->removeSection('development : production')
    ->
retrieveSection('staging : production')
    ->
addKey(new FLY_IniParser_Key('terminal.display''2'))
    ->
addKey(new FLY_IniParser_Key('app.views[]', array('index''form')));

$sectionManagement
    
->retrieveSection('testing : production')
    ->
removeKey(array(
        
'phpSettings.display_startup_errors',
        
'resources.frontController.params.displayExceptions',
        
'browsers[]'
    
))
    ->
addKey(new FLY_IniParser_Key('rootApplication', array('APP_DATA . "Portlo"')))
    ->
addKey(new FLY_IniParser_Key('sample'''));

echo 
ToHtml($iniParser);

// You can decide to work with array.
$r $iniParser->toArray();

echo 
'<pre>'print_r($r), '</pre>';