在 PhalconPHP 中附加多个配置数组



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

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

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

我能否获得一些关于在我的引导程序中附加数组的最佳方式的建议。

提姆

更新

这是我的引导文件设置的精简版本,试图实现尼古拉斯的建议。

class Application extends PhalconMvcApplication
{
    /**
     * 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 PhalconDIFactoryDefault();
        $loader = new PhalconLoader();
        $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 = PhalconDI::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 PhalconConfig($config);
            }
        );
    }
    public function main()
    {
        $this->_registerServices();
        $this->_loadConfig();
        echo $this->handle()->getContent();
    }
    public function processConfig($name)
    {
    $config = array();
    $di     = PhalconDI::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();

使用上述配置,我得到:

[2012 年 12 月 2 日 09:10:43] PHP 通知:未定义的属性:Phalcon\DI\FactoryDefault::$registry in/public/frontend/index.php on127路

[2012 年 12 月 2 日 09:10:43] PHP 致命错误:调用成员函数 offsetExists() 在/public/frontend/index 中的非对象上.php在第 127 行

试试这个:

在 DI 容器中注册新服务

// Get the DI container
$di = PhalconDI::getDefault();
$di->set(
    'registry', 
    function ()
    {
        return new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
    }
);

以上将是我们将存储配置文件的"注册表"。

public function processConfig($name)
{
    $config = array();
    $di     = PhalconDI::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);
}

作为用法,您可以这样做:

processConfig('config-global');
processConfig('config-other');
// Remember config_array is the merged array in the DI container
$di = PhalconDI::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 PhalconConfig($config);
    }
};

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

您还可以创建一个类来封装该功能,并在其中包含其他帮助程序函数来清除 DI 容器引用、始终加载基数组(全局)等。

编辑:评论后略微修改

最新更新