【问题标题】:How to preserve application.ini paths using Zend_Config_Writer_Ini如何使用 Zend_Config_Writer_Ini 保留 application.ini 路径
【发布时间】:2011-10-04 00:27:28
【问题描述】:

我目前正在 Phing 中开发一个构建系统,该系统采用 Zend Framework 项目模板并根据 Phing 参数对其进行配置。我遇到的一个问题是在使用 Zend_Config_Writer_Ini 时。

我的 Phing 任务从存储库中获取一个名为 application.default.ini 的预填充文件,并使用 Zend_Config_Ini 对其进行修改,以从构建文件中添加参数(数据库详细信息等)。然后将其写入 application.ini 以供项目使用。相关任务代码的简化版本如下所示:

$appConfig = new Zend_Config_Ini(
    $appDefaultConfigPath, 
    null, 
    array(
        'skipExtends' => true,
        'allowModifications' => true
    )
);

$appConfig->production->resources->db->params->host = $buildProperties->db->host;
$appConfig->production->resources->db->params->username = $buildProperties->db->username;
$appConfig->production->resources->db->params->password = $buildProperties->db->password;
$appConfig->production->resources->db->params->dbname = $buildProperties->db->dbname;

$writer = new Zend_Config_Writer_Ini();
$writer->setConfig($appConfig)
       ->setFilename($appConfigPath)
       ->write();

就数据库凭据而言,这可以正常工作,但是当涉及到包含已定义常量的预填充路径时,就会出现问题。例如:

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"

变成:

bootstrap.path = "APPLICATION_PATH/Bootstrap.php"

有没有办法在读取/写入不同的 ini 文件时保留这些配置行,或者我应该在运行任务之前重组我的构建文件以复制文件并只修改我需要更改的 ini 行?

【问题讨论】:

  • 您说您的数据库运行良好。如何向我们展示不工作的数据的代码而不是似乎工作的代码?不适合您的引导配置代码在哪里?
  • 没关系,我明白了。我的错!

标签: php zend-framework ini phing zend-config


【解决方案1】:

我希望在使用 Zend_Config 方便的同时保留使用 APPLICATION_PATH 常量的能力,所以我最终在 Zend_Config_Writer 保存文件后用一个简单的正则表达式修复了文件。

$writer->write();

// Zend_Config_Writer messes up the settings that contain APPLICATION_PATH
$content = file_get_contents($filename);

file_put_contents($filename, preg_replace('/"APPLICATION_PATH(.*)/', 'APPLICATION_PATH "$1', $content));

【讨论】:

    【解决方案2】:

    作为替代方案,您可以使用 Phing 的 Filter 替换配置模板中的令牌。

    一个示例任务:

    <target name="setup-config" description="setup configuration">
        <copy file="application/configs/application.ini.dist" tofile="application/configs/application.ini" overwrite="true">
            <filterchain>
                <replacetokens begintoken="##" endtoken="##">
                    <token key="DB_HOSTNAME" value="${db.host}"/>
                    <token key="DB_USERNAME" value="${db.user}"/>
                    <token key="DB_PASSWORD" value="${db.pass}"/>
                    <token key="DB_DATABASE" value="${db.name}"/>
                </replacetokens>
            </filterchain>
        </copy>
    </target>
    

    此任务将application/configs/application.ini.dist 复制到application/configs/application.ini,并将##DB_HOSTNAME## 等标记替换为来自phing 属性${db.host} 的值

    【讨论】:

    • 这看起来是一种更简洁的方式,而不是创建我自己的任务。我对 phing 很陌生,不知道过滤器/令牌功能。谢谢!
    【解决方案3】:

    直接来自这个php.net comment

    如果 ini 文件中的常量与 用单引号括起来的字符串,它们只能用双引号 使常量扩展。

    例子:

    define ('APP_PATH', '/some/path');

    mypath = APP_PATH '/config' // 常量不会被扩展:[mypath] => APP_PATH '/config'

    mypath = APP_PATH "/config" // 常量将被扩展:[mypath] => /一些/路径/配置

    所以你可以用单引号重写你的路径...... bootstrap.path = APPLICATION_PATH '/Bootstrap.php'

    ...然后用双引号替换所有出现的APPLICATION_PATH '*'(应该使用简单的正则表达式)。

    【讨论】:

      【解决方案4】:

      当您加载现有配置时,所有常量都已被翻译,即如果您使用 print_r 查看对象,您将不再找到您的常量。因此,使用 writer 打印完整路径而不是常量。

      在您的情况下,我猜这些常量在您的环境中不存在,因此按原样打印。

      更新:更具体。 Zend_Config_Ini::_parseIniFile() 使用 parse_ini_file() 读取将常量加载为真实路径的 ini 文件。见php.net doc Example #2

      【讨论】:

      • 谢谢阿德里安,你说得对。我忘记正确引导我的 phing 任务以包含 ZF 应用程序常量。在同一主题上,Zend 框架是否最好在配置文件中包含 PHP 常量?我觉得有点乱。
      • 配置中使用php常量是很常见的。甚至ZF Quick Start Guide 也使用它们。
      猜你喜欢
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-16
      相关资源
      最近更新 更多