Loading environmental parameters in Symfony 3.2 using env () at run time returns unresolved values

I have a settings file in an application using the Symfony 3.2 console, configuration, and the YAML component , and I'm trying to set external Settings from the environment in the Service Container settings.

I create a container constructor.

$container = new ContainerBuilder();

Use the file locator to search for resources:

$container = new ContainerBuilder();

Bootloader manager to load resources

LoaderResolver();

And using the boot method:

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

parameters.yml file:

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

After compiling the container and try checking the values ​​from the parameter package:

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

returns values ​​similar to this:

env_VAL1_3ec776edc429c1734ed780a29a0af538, env_VAL2_3ec776edc429c1734ed780a29a0af538

I think the container cannot resolve these values ​​from the environment.

. , :

$ export VAL1='SOME TEXT'

- , ?

+4
3

, "", ...

env(VAL1) thingy working - PHP PHPDumper, . , .

Symfony var/cache/dev/appDevDebugProjectContainer.php. getDynamicParameter,

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

, .

parameters.yml:

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

export :

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'

,

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()?

+6

:

parameters:
database:
    driver: pdo_mysql
    host: 127.0.0.1
    dbname: dbname
    user: '%env(VAL1)%'
    password: '%env(VAL2)%'
log_path: /logs

, . . : http://symfony.com/doc/current/configuration/external_parameters.html

+1

If you use Symfony \ Bundle \ FrameworkBundle \ Test \ WebTestCase instead of \ PHPUnit_Framework_TestCase Symfony3, do the magic for you.

$client = static::createClient();
$this->container = $client->getContainer();
$user = $container->getParameter('user'); // $user = 'abc'

take a look at http://symfony.com/doc/current/testing.html#your-first-functional-test

Using this method, you can also configure Symfony using * _test.yml specific files.

I think this is the right way.

-1
source

Source: https://habr.com/ru/post/1664956/


All Articles