PHP Classes

File: src/Utils/Config/Reader.php

Recommend this page to a friend!
  Classes of Thierry Feuzeu   Jaxon   src/Utils/Config/Reader.php   Download  
File: src/Utils/Config/Reader.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change: Fixed translation keys in config.
The config reader checks if the filename is empty.

This allows for example a package to provide no config file without raising an exception.
Date: 3 years ago
Size: 1,833 bytes
 

Contents

Class file image Download
<?php

/**
 * Reader.php - Jaxon config reader
 *
 * @package jaxon-core
 * @author Thierry Feuzeu <thierry.feuzeu@gmail.com>
 * @copyright 2016 Thierry Feuzeu <thierry.feuzeu@gmail.com>
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
 * @link https://github.com/jaxon-php/jaxon-core
 */

namespace Jaxon\Utils\Config;

class
Reader
{
   
/**
     * Read options from a config file
     *
     * @param string $sConfigFile The full path to the config file
     *
     * @return array
     */
   
public function read($sConfigFile)
    {
        if(!(
$sConfigFile = trim($sConfigFile)))
        {
            return [];
        }

       
$sExt = pathinfo($sConfigFile, PATHINFO_EXTENSION);
        switch(
$sExt)
        {
        case
'php':
           
$aConfigOptions = Php::read($sConfigFile);
            break;
        case
'yaml':
        case
'yml':
           
$aConfigOptions = Yaml::read($sConfigFile);
            break;
        case
'json':
           
$aConfigOptions = Json::read($sConfigFile);
            break;
        default:
           
$sErrorMsg = jaxon_trans('errors.file.extension', ['path' => $sConfigFile]);
            throw new \
Jaxon\Utils\Config\Exception\File($sErrorMsg);
        }

        return
$aConfigOptions;
    }

   
/**
     * Read options from a config file and setup the library
     *
     * @param string $sConfigFile The full path to the config file
     * @param string $sConfigSection The section of the config file to be loaded
     *
     * @return void
     */
   
public function load($sConfigFile, $sConfigSection = '')
    {
       
$aConfigOptions = $this->read($sConfigFile);
       
// Setup the lib config options.
       
jaxon()->di()->getConfig()->setOptions($aConfigOptions, $sConfigSection);
    }
}