Welcome Guest, Not a member yet? Register   Sign In
Error loading config file from third_party package
#1

[eluser]ilNotturno[/eluser]
Hi to all,
I have been using CodeIgniter for a month, studying the Core and the architecture; so i don't know if it is a bug or not.
I'm trying to create a package to manage template in a easy way, but i have a problem loading config file. Reading the user guide this is what i did:


1) I've created this folder structure:
Code:
layer
|- third_party
|--| my_package
|----| config
|------| settings.php


2) I've loaded the package and the settings file in main controller:
Code:
$this->load->add_package_path(APPPATH.'third_party/my_package/');
$this->load->config('settings', TRUE, TRUE);

Here is where i found the problem, because CodeIgniter try to execute the file "layer/config/settings.php" and write an error on my view though i decided to hidden them with the third parameter:

Code:
A PHP Error was encountered
Message: include(layer/config/settings.php) [function.include]: failed to open stream: No such file or directory

Reading the core I think I've found the problem in the load function (i'm using version 2.0.2):
The framework sets 2 variables:
Code:
$found = FALSE;
$loaded = FALSE;
and then it does a "foreach" in which it uses the variables. The problem is:
- the first time the variables are FALSE
- if i found the file in the first path the framework sets them to TRUE
- the second time they are still TRUE because no one set them to FALSE.

I think that setting them to FALSE AFTER the "foreach" statement will resolve the problem. Here is how looks my Core/Config.php file:

Code:
/**
     * Load Config File
     *
     * @access    public
     * @param    string    the config file name
     * @param   boolean  if configuration values should be loaded into their own section
     * @param   boolean  true if errors should just return false, false if an error message should be displayed
     * @return    boolean    if the file was loaded correctly
     */
    function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
    {
        $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
        foreach ($this->_config_paths as $path)
        {
            $found = FALSE;
            $loaded = FALSE;

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

            foreach ($check_locations as $location)
            {
                $file_path = $path.'config/'.$location.EXT;

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

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

            if ($found === FALSE)
            {
                continue;
            }

            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->config[$file]))
                {
                    $this->config[$file] = array_merge($this->config[$file], $config);
                }
                else
                {
                    $this->config[$file] = $config;
                }
            }
            else
            {
                $this->config = array_merge($this->config, $config);
            }

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

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

        if ($loaded === FALSE)
        {
            if ($fail_gracefully === TRUE)
            {
                return FALSE;
            }
            show_error('The configuration file '.$file.EXT.' does not exist.');
        }

        return TRUE;
    }



Thank you for you time reading this post.
Best regards,
Marco Pace
#2

[eluser]besson3c[/eluser]
I'm experiencing this problem too... This problem does not seem to be apparent with CodeIgniter 2.0.1
#3

[eluser]besson3c[/eluser]
Thanks for providing your fix, Marco! This helped me, sort of... One problem with your patch is that your third party library has to be the last thing loaded, or else after completing the loop $loaded will be set to false and will trip the following error immediately after this loop:

Code:
show_error('The configuration file '.$file.EXT.' does not exist.');



A better fix for this might be after the $check_locations as $location foreach loop, above:

Code:
if ($found === FALSE)
{
//      continue;
}

if (file_exists($file_path)) {
    include($file_path);
}
else {
    continue;
}


I'm not really sure why the config file names and paths are getting criss-crossed like this, but this is definitely a 2.0.2 bug.
#4

[eluser]besson3c[/eluser]
It looks like the system/core/Config.php in CI 2.0.1 does something similar to my patch above...
#5

[eluser]ilNotturno[/eluser]
Hi besson3c, thanks for your answers!
Thanks for your solutions, it looks like better than the integration of the CI Config.php file ;-].

I've read the code of the function I've posted and I didn't see the error:
Code:
show_error('The configuration file '.$file.EXT.' does not exist.');
because if the the vars are FALSE i have 3 possibilities:

1) file doesn't exist: in this case $found remain FALSE, so i'll enter in this part of the code:
Code:
if ($found === FALSE)
{
    continue;
}

2) file exists and is already loaded: in this case $found remain FALSE and $loaded becomes TRUE, so i'll enter in the same part of point 1.

3) file exists and isn't already loaded: in this case $found become TRUE and $loaded remain FALSE, so the file will be loaded and the $loaded becomes TRUE.

But looking at your answer i see another problem: that if i delete every configuration file from the config directory, i'll have the error you are talking about, but only if $fail_gracefully was setting to true calling the function.

Thanks for your help, i've created an extended core library to fix this problem and i've created a library to auto-load packages with this fix ;-]
#6

[eluser]besson3c[/eluser]
Scratch the above, the best fix is the one already committed Smile

https://bitbucket.org/ellislab/codeignit...Config.php
#7

[eluser]ilNotturno[/eluser]
Thanks very much! :-)
#8

[eluser]Feathers And Down[/eluser]
Hi!, well I'm having trouble with this.

I want my library (inside a package) to load automatically its configuration file named as same as library but don't find it. Can you tell me how is the correct way to load that file?? Inside documentation it say that if there is a $param to the constructor it will try to load config file with same name from /config folder. Is this way correct or there's something more to do ??

Thanks in advance.




Theme © iAndrew 2016 - Forum software by © MyBB