如何使用 Zend_Config_Writer_Ini 保留 application.ini 路径

发布于 2024-11-19 20:57:10 字数 1332 浏览 6 评论 0原文

我目前正在 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 行?

I'm currently working on a build system in Phing that takes a Zend Framework project template and configures it according to Phing parameters. One problem that I've come across is when using Zend_Config_Writer_Ini.

My Phing task takes a pre-populated file from the repo called application.default.ini and modifies this using Zend_Config_Ini to add parameters from the build file (db details, etc). It then writes it to application.ini ready for the project to use. A simplified version of the related task code looks something like this:

$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();

This works fine as far as the database credentials go but when it comes to pre-populated paths that include defined constants something goes wrong. For example:

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"

becomes:

bootstrap.path = "APPLICATION_PATH/Bootstrap.php"

Is there any way to preserve these config lines when reading/writing to different ini files or should I restructure my build file to copy the file before running the task and only modify the ini lines that I need to change?

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

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

发布评论

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

评论(4

同尘 2024-11-26 20:57:10

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

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

更新:更具体。 Zend_Config_Ini::_parseIniFile() 使用 parse_ini_file() 读取 ini 文件,该文件将常量加载为真实路径。请参阅 php.net 文档示例#2

When you load the existing config all constants are already translated, i.e. if you look at the object with print_r you won't find your constants anymore. Hence, with the writer the full path is printed instead of the constants.

In your case I guess that the constants don't exist in your environment and therefore are printed as is.

Update: To be more specific. Zend_Config_Ini::_parseIniFile() uses parse_ini_file() to read the ini file which loads the constants as real paths. See php.net doc Example #2

哀由 2024-11-26 20:57:10

直接来自此 php.net 评论

如果 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 '*' (一个简单的正则表达式应该可以)。

Straight from this php.net comment:

Constants in ini files are not expanded if they are concatenated with
strings quoted with single quotes, they must be in double quotes only
to make constants expanded.

Example:

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

mypath = APP_PATH '/config' // Constant won't be expanded: [mypath] =>
APP_PATH '/config'

mypath = APP_PATH "/config" // Constant will be expanded: [mypath] =>
/some/path/config

So you could rewrite your pathes with single quotes ...
bootstrap.path = APPLICATION_PATH '/Bootstrap.php'

... and later replace all occurrences of APPLICATION_PATH '*' with double quotes (a simple Regex should do).

沉睡月亮 2024-11-26 20:57:10

作为替代方案,您可以使用 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} 的值

As an alternative you could use Phing's Filter to replace tokens in your configuration template.

A example task:

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

This task copies application/configs/application.ini.dist to application/configs/application.ini and replaces tokens like ##DB_HOSTNAME## with the value from the phing property ${db.host}

泡沫很甜 2024-11-26 20:57:10

我想要方便地使用 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));

I wanted the convenience of using Zend_Config while preserving the ability to use the APPLICATION_PATH constant so I ended up fixing the file with a simple regex after Zend_Config_Writer saves the file.

$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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文