1

Currently I am loading multiple config files containing PHP native arrays, within my bootstrap.

require "app/configuration/config-global.php";
require "app/configuration/config-other.php";

With this setup "config-other.php" is overwriting the $settings array of "config-global.php".

Could I please get some advice on the best way to append the array within my bootstrap please.

Tim

Update

Here is a cut down version of my bootstap file setup attempting to implement Nikolaos's suggestion.

class Application extends \Phalcon\Mvc\Application
{

    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    public function _registerServices()
    {

        //Define constants
        $di = new \Phalcon\DI\FactoryDefault();

        $loader = new \Phalcon\Loader();

        $di->set('registry', function () {
            return new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
        });

        //load our config into the registry
        //$di->set('config', $config);

        $this->setDI($di);
    }

    public function _loadConfig()
    {

        $di = \Phalcon\DI::getDefault();

        $this->processConfig('appConfig1');
        $this->processConfig('globalConfig');

        // Remember config_array is the merged array in the DI container
        $new_array = $di->registry->offsetGet('config_array');

        // Optional, store the config in your DI container for easier use
        $di->set('config', function () use ($new_array) {
                return new \Phalcon\Config($config);
            }
        );

    }

    public function main()
    {

        $this->_registerServices();
        $this->_loadConfig();

        echo $this->handle()->getContent();
    }

    public function processConfig($name)
    {

    $config = array();
    $di     = \Phalcon\DI::getDefault();

    if ($di->registry->offsetExists('config_array'))
    {
        $config = $di->registry->offsetGet('config_array');
    }

    // Now get the file from the config
    require "/config/{$name}.php";

    // $settings came from the previous require
    $new_config = array_merge($config, $settings);

    // Store it in the DI container
    $di->registry->offsetSet('config_array', $new_config);

    }

}

$application = new Application();
$application->main();

With the above config I get:

[02-Dec-2012 09:10:43] PHP Notice: Undefined property: Phalcon\DI\FactoryDefault::$registry in /public/frontend/index.php on line 127

[02-Dec-2012 09:10:43] PHP Fatal error: Call to a member function offsetExists() on a non-object in /public/frontend/index.php on line 127

Community
  • 1
  • 1
Tim
  • 3,091
  • 9
  • 48
  • 64
  • Is this the desired behavior? i.e. config-global.php contains let's say $settings as the array. And then the config-other.php has also another $settings array. The latter will overwrite the former. Do you want them merged? – Nikolaos Dimopoulos Nov 21 '12 at 19:51
  • Hi Nikolaos, Yes I need a merged $settings array from multiple files. The reasoning behind this is that my app is actually lots of apps each with its own database connection details. I hope this makes sense – Tim Nov 21 '12 at 23:29

1 Answers1

4

Try this:

Register a new service in the DI container

// Get the DI container
$di = \Phalcon\DI::getDefault();

$di->set(
    'registry', 
    function ()
    {
        return new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
    }
);

The above will be the "registry" that we will store the configuration files.

public function processConfig($name)
{
    $config = array();
    $di     = \Phalcon\DI::getDefault();

    if ($di->registry->offsetExists('config_array'))
    {
        $config = $di->registry->offsetGet('config_array');
    }

    // Now get the file from the config
    require ROOT_PATH . "app/configuration/{$name}.php";

    // $settings came from the previous require
    $new_config = array_merge($config, $settings);

    // Store it in the DI container
    $di->registry->offsetSet('config_array', $new_config);
}

And as usage you can do this:

processConfig('config-global');
processConfig('config-other');

// Remember config_array is the merged array in the DI container
$di = \Phalcon\DI::getDefault();

// Remember config_array is the merged array in the DI container
$new_array = $di->registry->offsetGet('config_array');

// Optional, store the config in your DI container for easier use
$di->set(
    'config', 
    function () use ($new_array)
    {
        return new \Phalcon\Config($config);
    }
};

The config array in the container now has the merged data.

You can also create a class that would encapsulate that functionality and have other helper functions in there to clear the DI container reference, to load the base array (global) all the time etc.

EDIT: Modified it slightly after comments

Nikolaos Dimopoulos
  • 11,495
  • 6
  • 39
  • 67
  • Hi Nikolaos, Thank-you so much for this. It works a treat! A quick related question. I define a $config constant just underneath my processConfig function calls, "$config = new \Phalcon\Config($new_config);". This of course doesn't work as it's outside the scope of the function where $new_config exists. I know that making $new_config a global variable is a bad idea but do you have any advice on how else I could define the new config variable? I have also tried adding the call to your processConfig function but it doesn't work. As you can tell I'm still very new at OOP principles. – Tim Nov 25 '12 at 21:10
  • You don't need to go through that trouble. Use the DI container for it. Check my edit above. – Nikolaos Dimopoulos Nov 27 '12 at 02:12
  • Hi Nikolaos, Thank-you for your answer! I did try to use the DI but I wasn't able to get it to work. I get the following when I call the following line. "$new_array = $di->get('config_array');" [Uncaught exception 'Phalcon\DI\Exception' with message 'Invalid service definition. Missing 'className' parameter']. I have tried the documentation on $di->get but I don't understand it. Sorry for being such a pain. http://docs.phalconphp.com/en/0.5.0/api/Phalcon_DI.html#methods – Tim Nov 27 '12 at 04:06
  • As this issue is not directly related to this question I have made a new one at http://stackoverflow.com/questions/13636858/invalid-service-definition-when-using-di-get-phalcon-php – Tim Nov 29 '12 at 23:46
  • @Tim I made some additional changes to the code after reading your other question. Please have a look and let me know if that works for you. – Nikolaos Dimopoulos Nov 30 '12 at 17:25
  • Hi Nikolaos, I am sure there is nothing wrong with your code, but I still have problems. Maybe it is with my implementation? I have added a cut down version of my bootstap file to my original question... Perhaps you could have a look and see if I am implementing it wrong. – Tim Dec 01 '12 at 22:18