Welcome Guest, Not a member yet? Register   Sign In
load external config file
#1

[eluser]lauryb84[/eluser]
Hi All,
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:
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;
}
}

Any ideas?
Thank you very much!!!
#2

[eluser]CroNiX[/eluser]
Have you tried CI's read_file()?
#3

[eluser]CroNiX[/eluser]
Also, $config probably needs to be $this->config
Code:
$this->ext_config = array_merge($this->ext_config, $config);
#4

[eluser]lauryb84[/eluser]
Hi cronix, thanks for your reply.
There's no $config property in My_loader , so i can't use $this ->config. In that code, $config contains the items contained in the external file.

I don't know how ti merge current items with alla others item loaded in core\Config.php
#5

[eluser]CroNiX[/eluser]
I think what you want is the /system/core/Config.php LIBRARY, not Loader. That's where the configs are loaded.

At the top, there's this line:
Code:
var $_config_paths = array(APPPATH);

You probably just need to add your common path there to that array. Right now it only searches within the application folder. Haven't tried any of this... Not sure if there's a way to do that at runtime without a MY_Config (instead of MY_Loader) so that you don't need to alter the base class, at least if you autoload them.
#6

[eluser]lauryb84[/eluser]
Thank you CroNiX,
Adding my shared folder path to $_config_paths array, config file are loaded correctly.
But how can i add new paths programmatically without modify the core CI_Config class? I didn't found any other solution.

Ideas?

thanks,
Laura
#7

[eluser]CroNiX[/eluser]
Did you try
Code:
$this->config->_config_paths = array(APPPATH, '/your/other/path');
$this->config->load('your_config');
#8

[eluser]CroNiX[/eluser]
If you meant how do you hardcode it, you could create a MY_Config and just change that value.

Code:
Class MY_Config extends CI_Config {
  public function __construct()
  {
    parent::__construct();
    $this->_config_paths = array(APPPATH, '/your/other/path');
  }
}




Theme © iAndrew 2016 - Forum software by © MyBB