在PhalconPHP中附加多个配置数组 [英] Appending multiple config arrays in PhalconPHP

查看:83
本文介绍了在PhalconPHP中附加多个配置数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在引导程序中加载多个包含PHP本机数组的配置文件.

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";

通过此设置,"config-other.php"将覆盖"config-global.php"的$ settings数组.

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.

蒂姆

更新

这是我的引导文件安装程序的简化版本,试图实现Nikolaos的建议.

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:

[2012年12月2日09:10:43] PHP注意:未定义的属性: Phalcon \ DI \ FactoryDe​​fault :: $ registry在/public/frontend/index.php上 第127行

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

[2012年12月2日09:10:43] PHP致命错误:呼叫成员 /public/frontend/index.php中非对象上的函数offsetExists() 在第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

推荐答案

尝试一下:

在DI容器中注册新服务

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);
    }
};

容器中的config数组现在具有合并的数据.

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

您还可以创建一个类,该类将封装该功能并在其中具有其他帮助程序功能,以清除DI容器引用,始终加载基本数组(全局)等.

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.

编辑:在评论后稍加修改

这篇关于在PhalconPHP中附加多个配置数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆