【问题标题】:Parsing yml in symfony2在 symfony2 中解析 yml
【发布时间】:2016-03-17 15:08:59
【问题描述】:

我在理解如何在 symfony 中解析 yml 文件时遇到问题。

我像这样运行命令:dropcat --env=dev



    public function __construct()
    {
    $input = new ArgvInput();
    $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
    $running_path = getcwd();
    $config = Yaml::parse(
        file_get_contents($running_path .'/' . $env . '_dropcat.yml')
    );
    $this->configuration = $config;
    }

在我的文件 (dev_dropcat.yml) 中,我有:



    imports:
        - { resource: 'dropcat.yml' }
    remote:
      environment:
        server: myhosts

我的理解是解析dev_dropcat.yml的时候也应该导入dropcat.yml里面的内容,但是没有。谁能指出我正确的方向?

【问题讨论】:

    标签: symfony yaml


    【解决方案1】:

    方法Yaml::parse 的设计目的不是为了满足您的期望。它只需要yml 内容并将其更改为数组。它不 import 任何东西,因此您的 imports 子句将只是数组的一个键 - 仅此而已。

    【讨论】:

    • 那么,我该怎么做呢?
    • 你到底想达到/得到什么?
    【解决方案2】:

    您可能希望将这些配置放在 parameters.yml 中,或者您可能希望在 config.yml 中的导入中添加一行。

    除非出于某种原因您不希望它们自动加载?

    在 config.yml 顶部附近注入以下行应该这样做。

        - { resource: 'dropcat.yml' } 
    

    如果你想为不同的环境加载不同的版本,你可以使用config_prod.yml、config_test.yml和config_dev.yml。

    http://symfony.com/doc/current/cookbook/configuration/configuration_organization.html

    【讨论】:

    • 这个 dropcat 应用程序旨在成为网站的部署工具,所有网站都有自己的默认配置(dropcat.yml),不同的环境有自己的文件(如 dev_dropcat.yml)用于覆盖从默认值。 Dropcat 旨在从运行它的目录中获取配置,因此不需要 symfony 应用程序本身(dropcat)的默认配置,因为所有站点都有不同的配置。我想使用导入,因为我不想复制配置。
    • 那么您可能希望在 config.yml(或 config_prod.yml)中包含 dropcat.yml,在 config_dev.yml 中包含 dev_dropcat.yml,以及 dev_dropcat.yml。这里的关键是在resource: 之后添加更详细的路径,例如resource: '../../localapp/dropcat.yml' 或类似的东西。我认为。如果这就是你的意思,请告诉我,以便我更新我的答案。
    【解决方案3】:

    我没有在 yml 中使用导入来解决它,首先我加载默认的 yml (dropcat.yml),如果它确实存在,则加载环境文件 (dev_dropycat.yml)。

    public function __construct()
    {
        $input = new ArgvInput();
        $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
        $running_path = getcwd();
        if (file_exists($running_path . '/dropcat.yml')) {
            $default_config = Yaml::parse(
                file_get_contents($running_path . '/dropcat.yml')
            );
            $configs = $default_config;
        }
    
        // Check for env. dropcat file.
        if (file_exists($running_path . '/' . $env . '_dropcat.yml')) {
            $env_config = Yaml::parse(
                file_get_contents($running_path .'/' . $env . '_dropcat.yml')
            );
            // Recreate configs if env. exists.
            if (isset($default_config)) {
                $configs = array_replace_recursive($default_config, $env_config);
            } else {
                $configs = $env_config;
            }
        } else {
            echo "No configuration found for the specified environment $env, using default settings\n";
        }
        if (isset($configs)) {
            $this->configuration = $configs;
        } else {
            $this->configuration = null;
        }
    }
    

    我猜可以做得更漂亮,但它适用于我想要实现的目标。

    【讨论】:

      猜你喜欢
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      相关资源
      最近更新 更多