Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.0] Refactored to use environment variables object #300

Merged
merged 1 commit into from
Jan 2, 2019
Merged

[3.0] Refactored to use environment variables object #300

merged 1 commit into from
Jan 2, 2019

Conversation

GrahamCampbell
Copy link
Collaborator

@GrahamCampbell GrahamCampbell commented Jan 2, 2019

Closes #208. Closes #214.

This was referenced Jan 2, 2019
@GrahamCampbell
Copy link
Collaborator Author

Some examples.


Example 1

The following example assumes $path is the full path of the directory containing your .env file.

This example will load the .env file, returning the variables, AND will have the side effect of mutating your environment.

<?php

use Dotenv\Dotenv;

$dotenv = Dotenv::create($path);

$variables = $dotenv->load();

Example 2

The following example assumes $path is the full path of the directory containing your .env file.

Similar to the first example, only this time we avoid calling any non-thread-safe functions.

<?php

use Dotenv\Environment\Adapter\EnvConstAdapter;
use Dotenv\Environment\Adapter\ServerConstAdapter;
use Dotenv\Environment\DotenvFactory;
use Dotenv\Dotenv;

$factory = new DotenvFactory([new EnvConstAdapter(), new ServerConstAdapter()]);

Dotenv::create($path, null, $factory)->load();

EXAMPLE 3

The following example assumes $path is the full path of the directory containing your .env file.

This example will load the .env file, returning the variables, BUT will not mutate your current environment. This is because, in example 1, the default factory was used, so the adapters that mess with your actual environment are used.

<?php

use Dotenv\Environment\Adapter\ArrayAdapter;
use Dotenv\Environment\DotenvFactory;
use Dotenv\Dotenv;

$dotenv = Dotenv::create($path, null, new DotenvFactory([new ArrayAdapter()]));

$variables = $dotenv->load();

EXAMPLE 4

The following example assumes $content contains the actual content of your env file:

Once again, this example will not mutate your actual environment, allowing you to inspect the contents of an env file in isolation.

<?php

use Dotenv\Environment\Adapter\ArrayAdapter;
use Dotenv\Environment\DotenvFactory;
use Dotenv\Loader;

$loader = new Loader([], new DotenvFactory([new ArrayAdapter()]));

$variables = $loader->loadDirect($content));

@GrahamCampbell GrahamCampbell changed the title Refactored to use environment variables object [3.0] Refactored to use environment variables object Jan 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant