loadEnv example


    public function bootEnv(string $path, string $defaultEnv = 'dev', array $testEnvs = ['test'], bool $overrideExistingVars = false): void
    {
        $p = $path.'.local.php';
        $env = is_file($p) ? include $p : null;
        $k = $this->envKey;

        if (\is_array($env) && ($overrideExistingVars || !isset($env[$k]) || ($_SERVER[$k] ?? $_ENV[$k] ?? $env[$k]) === $env[$k])) {
            $this->populate($env$overrideExistingVars);
        } else {
            $this->loadEnv($path$k$defaultEnv$testEnvs$overrideExistingVars);
        }

        $_SERVER += $_ENV;

        $k = $this->debugKey;
        $debug = $_SERVER[$k] ?? !\in_array($_SERVER[$this->envKey]$this->prodEnvs, true);
        $_SERVER[$k] = $_ENV[$k] = (int) $debug || (!\is_bool($debug) && filter_var($debug, \FILTER_VALIDATE_BOOL)) ? '1' : '0';
    }

    /** * Loads one or several .env files and enables override existing vars. * * @param string $path A file to load * @param string ...$extraPaths A list of additional files to load * * @throws FormatException when a file has a syntax error * @throws PathException when a file does not exist or is not readable */


        $composerFile = $projectDir.'/composer.json';
        $config += (is_file($composerFile) ? json_decode(file_get_contents($composerFile), true) : [])['extra']['runtime'] ?? [];
        $dotenvPath = $projectDir.'/'.($config['dotenv_path'] ?? '.env');
        $env = $input->getArgument('env') ?? $this->defaultEnv;
        $envKey = $config['env_var_name'] ?? 'APP_ENV';

        if ($input->getOption('empty')) {
            $vars = [$envKey => $env];
        } else {
            $vars = $this->loadEnv($dotenvPath$env$config);
            $env = $vars[$envKey];
        }

        $vars = var_export($vars, true);
        $vars = <<<EOF <?php // This file was generated by running "php bin/console dotenv:dump $env" return $vars;
putenv('EXISTING_KEY=EXISTING_VALUE');
        };

        @mkdir($tmpdir = sys_get_temp_dir().'/dotenv');

        $path = tempnam($tmpdir, 'sf-');

        // .env         file_put_contents($path, "FOO=BAR\nEXISTING_KEY=NEW_VALUE");

        $resetContext();
        (new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV');
        $this->assertSame('BAR', getenv('FOO'));
        $this->assertSame('dev', getenv('TEST_APP_ENV'));
        $this->assertSame('EXISTING_VALUE', getenv('EXISTING_KEY'));
        $this->assertSame('EXISTING_VALUE', $_ENV['EXISTING_KEY']);

        $resetContext();
        (new Dotenv())->usePutenv()->loadEnv($path, 'TEST_APP_ENV', 'dev', ['test'], true);
        $this->assertSame('BAR', getenv('FOO'));
        $this->assertSame('dev', getenv('TEST_APP_ENV'));
        $this->assertSame('NEW_VALUE', getenv('EXISTING_KEY'));
        $this->assertSame('NEW_VALUE', $_ENV['EXISTING_KEY']);

        
Home | Imprint | This part of the site doesn't use cookies.