在运行时使用env()在Symfony 3.2中加载外部环境参数,返回未解析的值 [英] Load external environment parameters in Symfony 3.2 using env() at runtime return unresolved values

查看:90
本文介绍了在运行时使用env()在Symfony 3.2中加载外部环境参数,返回未解析的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 symfony 3.2控制台,配置和YAML组件 的应用程序中有一个参数文件,我尝试设置服务容器参数中环境的外部参数

我创建了容器生成器。

$container = new ContainerBuilder();

Am使用文件定位器查找资源:

Am using file locator to locate resources :

$container = new ContainerBuilder();

加载资源的解析程序

LoaderResolver();

并使用加载方法:

$this->load('parameters.yml');

parameters.yml文件:

parameters:
  database:
    driver: pdo_mysql
    host: 127.0.0.1
    dbname: dbname
    user: env(VAL1)
    password: env(VAL2)
  Local: us-en

编译容器并尝试检查从参数bag获取值:

After compiling the container and try to check get values from parameters bag :

$container->getParameterBag()->all()

它向我返回这样的值:

env_VAL1_3ec776edc429c1734ed780a29a0af538 env_VAL2_3ec776edc429c1734ed780a29a0af538

我认为该容器不能从环境中解析这些值。

I think the container can't resolve those values from the environment .

注意:我使用以下命令设置环境变量:

Note : i set the environment variable using :

$ export VAL1='SOME TEXT'

任何人都知道为什么?

推荐答案

所以我花了一些时间。 e调查这个问题,这就是我发现的...

So I've spent some time investigating this 'issue' and here is what I found out...

获取 env(VAL1)要做的工作是使用 PHPDumper 将容器生成为PHP文件,然后使用生成的容器。没有其他方法可以使它起作用,因为它只能解析该文件中的环境。

The only way to get env(VAL1) thingy working is to generate the container to PHP file using PHPDumper and then use the generated container. There is no other way to make it working because it only resolves environment in that file.

在正常的Symfony项目中,会生成一个 var / cache / dev / appDevDebugProjectContainer.php 文件。有一个方法 getDynamicParameter 看起来像这样

In the normal Symfony project there is a generated var/cache/dev/appDevDebugProjectContainer.php file. There is method getDynamicParameter which looks like this

 private function getDynamicParameter($name)
    {
        switch ($name) {
            case 'kernel.root_dir': $value = ($this->targetDirs[3].'/app'); break;
            case 'kernel.logs_dir': $value = ($this->targetDirs[2].'/logs'); break;
            case 'user': $value = $this->getEnv('VAL1'); break;
            case 'session.save_path': $value = ($this->targetDirs[3].'/app/../var/sessions/dev'); break;
            case 'router.resource': $value = ($this->targetDirs[3].'/app/config/routing_dev.yml'); break;
            default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
        }
        $this->loadedDynamicParameters[$name] = true;

        return $this->dynamicParameters[$name] = $value;
    }

这是唯一的 env(VAL1 )被评估。

因此,对于您的情况,解决方案如下。

So for your case the solution is the following.

我使用简化的 parameters.yml

parameters:
    user: '%env(VAL1)%'

导出我这样做了:

export VAL1=abc

PHP代码:

$container = new ContainerBuilder();

$loader = new YamlFileLoader(
    $container,
    new FileLocator('.')
);

$loader->load('parameters.yml');

$container->compile();

$dumper = new PhpDumper($container);

$content = $dumper->dump(
    [
        'class' => 'DumpedContainer',
        'base_class' => 'Container',
        'file' => 'DumpedContainer.php',
        'debug' => true
    ]
);

// Use this code if you want to write file to the disk
$cache = new ConfigCache('DumpedContainer.php', true);
$cache->write($content, $container->getResources());
require_once $cache->getPath();

// ... otherwise use this code
//$content = str_replace('<?php', '', $content);
//eval($content);

$container = new DumpedContainer();

$user = $container->getParameter('user'); // $user = 'abc'

在生成的代码中,您将看到以下方法魔术

In the generated code you'll see the following method that do the magic

private function getDynamicParameter($name)
{
    switch ($name) {
        case 'user': $value = $this->getEnv('VAL1'); break;
        default: throw new InvalidArgumentException(sprintf('The dynamic parameter "%s" must be defined.', $name));
    }
    $this->loadedDynamicParameters[$name] = true;

    return $this->dynamicParameters[$name] = $value;
}

此解决方案可以正常工作,但查看它,我想知道是否确实需要你的项目? getenv()怎么了?

This solution works fine but looking at it I wonder if this is really needed in your project? What's wrong with getenv()?

这篇关于在运行时使用env()在Symfony 3.2中加载外部环境参数,返回未解析的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆