PHP Classes

File: src/loader/CliLoader.php

Recommend this page to a friend!
  Classes of Vitaly   Queasy PHP Config   src/loader/CliLoader.php   Download  
File: src/loader/CliLoader.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Queasy PHP Config
Read a configuration from files in several formats
Author: By
Last change:
Date: 3 years ago
Size: 1,935 bytes
 

Contents

Class file image Download
<?php

/*
 * Queasy PHP Framework - Configuration
 *
 * (c) Vitaly Demyanenko <vitaly_demyanenko@yahoo.com>
 *
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
 */

namespace queasy\config\loader;

use
Exception;
use
SimpleXMLElement;

/**
 * CLI configuration loader class
 */
class CliLoader extends AbstractLoader
{
   
/**
     * Load and return an array containing configuration.
     *
     * @return array Loaded configuration
     *
     * @throws ConfigLoaderException When file is corrupted
     */
   
public function load()
    {
        global
$argv;

       
$args = $argv;
       
array_shift($args);

       
$result = array();
        foreach (
$args as $arg) {
           
$argParts = explode('=', $arg);

           
$argName = array_shift($argParts);

           
$argValue = trim(array_shift($argParts), '"');
           
$argValue = $argValue
               
? $argValue
               
: true;

           
$argNameArray = explode('.', $argName);
            if (
1 === count($argNameArray)) {
               
$result[$argNameArray[0]] = $argValue;
            } else {
               
$i = 0;
               
$prevItem = &$result;
                do {
                    if (
$i === count($argNameArray) - 1) {
                       
$prevItem[$argNameArray[$i]] = $argValue;
                    } else {
                        if (!isset(
$prevItem[$argNameArray[$i]])) {
                           
$prevItem[$argNameArray[$i]] = array();
                        }

                       
$prevItem = &$prevItem[$argNameArray[$i]];
                    }
                } while (
$i++ < count($argNameArray) - 1);
            }
        }

        return
$result;
    }

    public function
check()
    {
        if (
php_sapi_name() !== 'cli') {
            throw new
CorruptedException('cli');
        }

        return
true;
    }
}