1
votes

I'm working with CodeIgniter and I’d like to load one or more config files located in an external folder, shared by different CI installation. Is it possible?

I tried to extend the Loader Class, and call the new method: $this -> load -> external_config(‘MY\EXTERNAL\PATH’);

Load is successful, but i can’t retrieve config items in my controller, because in MY_Loader class the core\Loader config property is not visible, and i can’t merge it with the new loaded values.

This is my code:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

 /**
  * List of all loaded config files
  *
  * @var array
  */
 var $is_config_loaded = array();

 /**
  * List of all loaded config values
  *
  * @var array
  */
 //var $ext_config = array();

 function __construct(){
        parent::__construct();
    }


    /**
  * Loads an external config file
  *
  * @param string
  * @param bool
  * @param  bool
  * @return void
  */
 public function external_config($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
 {
  $file = ($file == '') ? 'config' : str_replace('.php', '', $file);
  $found = FALSE;
  $loaded = FALSE;

  $check_locations = defined('ENVIRONMENT')
   ? array(ENVIRONMENT.'/'.$file, $file)
   : array($file);


   foreach ($check_locations as $location)
   {
    $file_path = $file.'.php';

    if (in_array($file_path, $this->is_config_loaded, TRUE))
    {
     $loaded = TRUE;
     continue 2;
    }

    if (file_exists($file_path))
    {
     $found = TRUE;
     break;
    }
   }

   if ($found === FALSE)
   {
    if ($fail_gracefully === TRUE)
    {
     return FALSE;
    }
    show_error('The configuration file '.$file.'.php does not exist.');
   }
   else{
    include($file_path);

    if ( ! isset($config) OR ! is_array($config))
    {
     if ($fail_gracefully === TRUE)
     {
      return FALSE;
     }
     show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
    }

    if ($use_sections === TRUE)
    {
     if (isset($this->ext_config[$file]))
     {
      $this->ext_config[$file] = array_merge($this->ext_config[$file], $config);
     }
     else
     {
      $this->ext_config[$file] = $config;
     }
    }
    else
    {
     $this->ext_config = array_merge($this->ext_config, $config);
    }

    $this->is_config_loaded[] = $file_path;
    unset($config);

    $loaded = TRUE;
    log_message('debug', 'Config file loaded: '.$file_path);
   }

  return TRUE;
 }
} 

I found in core\Config.php a property var $_config_paths = array(APPPATH);

With the paths in wich look for config files. How can i add paths to the array without chance the core classe code? Any ideas? Thank you very much!!!

1
Your config must exist in the same config directory.AlphaMale

1 Answers

1
votes

CodeIgniter makes assumptions about the config file being loaded, its location, its file ext etc. You may load other files that is: from the same directory (support seems to be limited to this usage scenario).

However, you can (looking at the chapter https://ellislab.com/codeigniter/user-guide/libraries/config.html (Setting a Config Item)) iterate your file values and set them like so

(todo: add some checks if file exists, is readable, no decoding errors etcetera)

$myConfigValues = json_decode(file_get_contents($configKeyValuesFromOuterSpace));
foreach($myConfigValues as $key => $value){
  $CI->config->set_item('item_name', 'item_value');
}

As for the exposure of the config object, in the loader you may anytime examine its public parts in more or less subtle manners, by using the code igniter singleton instance retrieval method:

$CI =& get_instance();
die(print_r($CI->config,true));

This will be a CI_Config object. You may also create a MY_Config extension.