Getting HMVC to play nicely with deeply nested config files in Sparks

I was recently debugging an issue with Codeigniter Payments and HMVC. The issue was that config files were not being loaded if they were deeply nested – for example:

config/payments/my_gateway.php

The MX_Config path explodes the filepath provided on forward slashes, so if you do anything other than this->load->config(‘whatever’), the file you want won’t get loaded, since only the last segment will be sent to the CI_Config class.

I was able to implement a workaround by modifying the MX_Config class, and by passing TRUE as the third param when I loaded a config file in a spark (this asks the config loader to fail gracefully, returning FALSE when a config file isn’t found instead of using the show_error helper).

Updated load function in MX_Config:

        public function load($file = 'config', $use_sections = FALSE, $fail_gracefully = FALSE, $_module = '') {
        		$original_file = $file;
                if (in_array($file, $this->is_loaded, TRUE)) return $this->item($file);

                $_module OR $_module = CI::$APP->router->fetch_module();
                list($path, $file) = Modules::find($file, $_module, 'config/');

                if ($path === FALSE) {
                        $try1 = parent::load($file, $use_sections, $fail_gracefully);
                        if($try1) return $this->item($file);

                        $try2 = parent::load($original_file, $use_sections, $fail_gracefully);
                        if($try2) return $this->item($original_file);
                }  

                if ($config = Modules::load_file($file, $path, 'config')) {
                        /* reference to the config array */
                        $current_config =& $this->config;

                        if ($use_sections === TRUE)     {

                                if (isset($current_config[$file])) {
                                        $current_config[$file] = array_merge($current_config[$file], $config);
                                } else {
                                        $current_config[$file] = $config;
                                }

                        } else {
                                $current_config = array_merge($current_config, $config);
                        }
                        $this->is_loaded[] = $file;
                        unset($config);
                        return $this->item($file);
                }
        }

This tries the original path if the config file isn’t found at the reformatted path. Kind of a hack, I know – but I couldn’t find a more elegant way around this. Any suggestions are appreciated – but if you just need it to work – I hope this helps.

This entry was posted in CodeIgniter, PHP, Programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted April 11, 2012 at 1:55 pm | Permalink

    I am trying to install the codeigniter-payments spark in CI Bonfire, but I’m having a lot of trouble.

    You can see the errors at http://dev.ezpublisher.org/

    1) I put config/payments.php into bonfire/application/config
    2) I put libraries/payments.php into bonfire/application/libraries
    3) I put src/ into bonfire/src/

    4) I noticed a dependency on php-payments/lib/payments.php
    – When I downloaded the spark, the src/php-payments folder was empty
    – I downloaded php-payments library and pasted the contents into bonfire/src/php-payments/

    5) I changed line 42 in libraries/payments.php from this:
    require(dirname(__DIR__).”/src/php-payments/lib/payments.php”);
    to this:
    require(base_url().’bonfire/application/src/php-payments/lib/payments.php’);

    I am still getting the errors found at http://dev.ezpublisher.org

    Thanks,
    Paul

  2. Calvin Froedge
    Posted April 11, 2012 at 2:50 pm | Permalink

    I’m not sure the errors on your page have anything to do with Codeigniter-Payments.

    Btw, you had an empty src/ dir because you downloaded the zip from the sparks site or github instead of using sparks to install or cloning the repo. PHP-Payments exists as a submodule.

    Not sure why you did step 5 or what’s up with your include path. If you need help debugging you could hire me to check it out for you.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>