Zend_Config_ini 动态添加一个部分和(或)键?

发布于 2024-12-07 04:57:37 字数 2128 浏览 2 评论 0原文

我的系统上的每个客户端都有一个 ini 文件,并添加要动态创建的新部分和密钥。

我需要这样的想法:

当前ini文件:

[section_a]
key_a=1
key_b-2

并且需要更改此(使用php/zend)代码:

[section_a]
key_a =1
key_b = 2
[section_b]
key_a = 1

需要添加一个名为section_b的新部分,其中有一个名为key_a的新键,但我没有找到任何Zend_Ini_Config 的方法类似于“$ini->add('section_b','key_a')”。

奥布斯:

Php "magic" like $ini->$new_prop->$new_prop = "1" , dont work to!

任何帮助!

更新

<?php
class SystemConfigHelper
{
    public static $data;

    public static function load()
    {
        if(!defined("ACCOUNT_ID"))
            return true;

        try
        {
            $url = explode('.', $_SERVER['HTTP_HOST']);
            self::$data = new Zend_Config_Ini(ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini",null,array("allowModifications" => true));
            return true;
        }
        catch(Exception $e)
        {
            die($e->getMessage());
            return false;
        }
    }

    public static function save()
    {
        try
        {
            $url = explode('.', $_SERVER['HTTP_HOST']);
            $writer = new Zend_Config_Writer_Ini(array('config'   => self::$data,
                                       'filename' => ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini"));
            $writer->write();

        }
        catch(Exception $e)
        {
            die($e->getMessage());
            return false;
        }

    }

    public static function getParam($section,$key)
    {
        return self::$data->$section->$key;
    }

    public static function sync($data)
    {
        //self::$data = $data;
        //return;

        foreach(self::$data as $section => $param)
        {
            foreach($param as $key => $value)
            {
               self::$data->$section->$key = $data[$section][$key];
            }
        }

    }

    public static function getParamAs($section,$key,$as)
    {
        return self::$data->$section->$key==1?$as:"";
    }




}
?>

I have an ini file for each client on my system, and add a new section and key to be created on the fly.

I need some think like this:

Current ini file:

[section_a]
key_a=1
key_b-2

And need to change this (with a php/zend) code, to that:

[section_a]
key_a =1
key_b = 2
[section_b]
key_a = 1

Need to add a new section named section_b whith a new key named key_a , but i don't find any mthod on Zend_Ini_Config like "$ini->add('section_b','key_a')".

Obs:

Php "magic" like $ini->$new_prop->$new_prop = "1" , dont work to!

Any Help!!

Update

<?php
class SystemConfigHelper
{
    public static $data;

    public static function load()
    {
        if(!defined("ACCOUNT_ID"))
            return true;

        try
        {
            $url = explode('.', $_SERVER['HTTP_HOST']);
            self::$data = new Zend_Config_Ini(ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini",null,array("allowModifications" => true));
            return true;
        }
        catch(Exception $e)
        {
            die($e->getMessage());
            return false;
        }
    }

    public static function save()
    {
        try
        {
            $url = explode('.', $_SERVER['HTTP_HOST']);
            $writer = new Zend_Config_Writer_Ini(array('config'   => self::$data,
                                       'filename' => ACCOUNTS_PATH . "/" . ACCOUNT_ID . "/system-config.ini"));
            $writer->write();

        }
        catch(Exception $e)
        {
            die($e->getMessage());
            return false;
        }

    }

    public static function getParam($section,$key)
    {
        return self::$data->$section->$key;
    }

    public static function sync($data)
    {
        //self::$data = $data;
        //return;

        foreach(self::$data as $section => $param)
        {
            foreach($param as $key => $value)
            {
               self::$data->$section->$key = $data[$section][$key];
            }
        }

    }

    public static function getParamAs($section,$key,$as)
    {
        return self::$data->$section->$key==1?$as:"";
    }




}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀梦 2024-12-14 04:57:37

您阅读过 Zend_Config_Writer 上的文档吗?假设您已经加载了整个配置文件(而不仅仅是一部分),您应该能够执行此操作(直接从文档中提取):

$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                       'filename' => 'config.ini'));
$writer->write();

Have you read the docs on Zend_Config_Writer? Assuming you've loaded the entire config file (and not just a section) you should be able to do this (pulled right from the docs):

$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                       'filename' => 'config.ini'));
$writer->write();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文