{"id":15029310,"url":"https://github.com/sebastiansulinski/dotenv","last_synced_at":"2025-04-09T20:32:36.406Z","repository":{"id":62541661,"uuid":"41477791","full_name":"sebastiansulinski/dotenv","owner":"sebastiansulinski","description":"DotEnv : Load environment variables from multiple .env files","archived":false,"fork":false,"pushed_at":"2020-12-17T07:13:45.000Z","size":48,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T22:26:11.298Z","etag":null,"topics":["dotenv","environment","environment-variables","environment-vars","php","php-vars","php71"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sebastiansulinski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-27T09:27:40.000Z","updated_at":"2025-01-16T07:13:51.000Z","dependencies_parsed_at":"2022-11-02T15:33:13.882Z","dependency_job_id":null,"html_url":"https://github.com/sebastiansulinski/dotenv","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiansulinski%2Fdotenv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiansulinski%2Fdotenv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiansulinski%2Fdotenv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sebastiansulinski%2Fdotenv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sebastiansulinski","download_url":"https://codeload.github.com/sebastiansulinski/dotenv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247721928,"owners_count":20985083,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotenv","environment","environment-variables","environment-vars","php","php-vars","php71"],"created_at":"2024-09-24T20:10:16.675Z","updated_at":"2025-04-09T20:32:36.378Z","avatar_url":"https://github.com/sebastiansulinski.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DotEnv\n\nPackage which enables to load environment variables from multiple .env files at multiple locations\n\nThis package is a work that derived from package published by [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv) with some additional functionality such as handling multiple `.env` files and setting up variables using instance of the class.\n\n[![Build Status](https://travis-ci.org/sebastiansulinski/dotenv.svg?branch=master)](https://travis-ci.org/sebastiansulinski/dotenv)\n\n## Installation\n\nInstall package using composer\n\n```\ncomposer require sebastiansulinski/dotenv\n```\n\n## Usage instructions\n\nTo use the plugin you'll need to have at least one `.env` file i.e.\n\n```php\n// .env\n\nDB_HOST=localhost\nDB_NAME=test\nDB_USER=user\nDB_PASS=password\n```\n\nYou load all your `.env` files when instantiating the `SSD\\DotEnv\\DotEnv` object.\n\n```php\nrequire \"vendor/autoload.php\";\n\nuse SSD\\DotEnv\\DotEnv;\n\n$dotEnv = new DotEnv(__DIR__ . DIRECTORY_SEPARATOR . '.env');\n```\n\nYou can pass a single `.env` file, path to a directory with `.env.*` files or multiple paths / directories\n\n```php\n$dotEnv = new DotEnv(__DIR__ . DIRECTORY_SEPARATOR . '.env');\n$dotEnv = new DotEnv(__DIR__);\n$dotEnv = new DotEnv(\n    __DIR__\n    'another/path',\n    'another/file/.env'\n);\n```\n\n### Loading variables\n\nTo load process the variables there are two methods `load()` and `overload()`.\n\nThe `load()` method will only set the variables that do not already exist, while `overload()` will set them all - overwriting any existing ones.\n\n```php\n$dotEnv = new DotEnv(__DIR__);\n\n// will only set variables\n// that are not already set\n$dotEnv-\u003eload();\n```\n\n```php\n$dotEnv = new DotEnv(__DIR__);\n\n// will set all variables from the files\n// overwriting any duplicates\n$dotEnv-\u003eoverload();\n```\n\n### Required variables\n\nTo ensure that your system has all necessary variables available you can use `required()` method, which takes either a single variable name or an array of required variables.\n\n```php\n$dotEnv = new DotEnv(__DIR__);\n\n// will only set variables\n// that are not already set\n$dotEnv-\u003eload();\n\n// either a single variable\n$dotEnv-\u003erequired('DB_HOST');\n```\n\n```php\n$dotEnv = new DotEnv(__DIR__);\n\n// will only set variables\n// that are not already set\n$dotEnv-\u003eload();\n\n// or an array of variables\n$dotEnv-\u003erequired([\n    'DB_HOST',\n    'DB_NAME',\n    'DB_USER',\n    'DB_PASS'\n]);\n```\n\nIf any of the required variables does not exist in any of the `.env.*`files - system will throw a `RuntimeException`.\n\n### Returning contents of `.env` file(s) as array\n\nUse `toArray()` method to fetch the contents of the `.env` file(s), with or without setting up the environment variables.\n\n```php\n$dotEnv = new DotEnv(__DIR__);\n\n// will not set environment variables\n$variables = $dotEnv-\u003etoArray();\n\nvar_dump($variables);\n\n// ['DB_HOST' =\u003e '127.0.0.1', 'DB_NAME' =\u003e 'blog', ...]\n\n\n// will set environment variables using load() method\n$variables = $dotEnv-\u003etoArray(DotEnv::LOAD);\n\nvar_dump($variables);\n\n// ['DB_HOST' =\u003e '127.0.0.1', 'DB_NAME' =\u003e 'blog', ...]\n\n\n// will set environment variables using overload() method\n$variables = $dotEnv-\u003etoArray(DotEnv::OVERLOAD);\n\nvar_dump($variables);\n\n// ['DB_HOST' =\u003e '127.0.0.1', 'DB_NAME' =\u003e 'blog', ...]\n```\n\n### Obtaining value stored in the variable\n\nYou can use a static `get()` method on the `DotEnv` object to retrieve the value stored in a given environment variable.\n\n```php\nDotEnv::get('DB_HOST');\n```\n\nWhen you associate the string `true`, `false` with the variables within your `.env` file, they will automatically be converted to boolean `true` / `false` when using `DotEnv::get`.\nThe same applies to the variable with `null` string, which will return `null` value.\n\nIf you specify a variable without any value associated (`MY_VARIABLE=`) - it will return an empty string `''`.\n\nYou can provide a second argument to the `get()` method, which will be returned if variable was not found.\nThe default value can be of a `scalar` or a `Closure` type.\n\n```php\nDotEnv::get('DB_HOST', 'localhost');\n\nDotEnv::get('DB_HOST', function() {\n\n    return DotEnv::get('ENVIRONMENT') == 'live' ? 'localhost' : 127.0.0.1;\n\n});\n```\n\n### Checking if exists and equals\n\nYou can check if variable exists by using `has()` and whether it stores a given value by using `is()` methods.\n\n```php\nDotEnv::has('NON_EXISTENT_VARIABLE');\n// false\n\nDotEnv::is('ENVIRONMENT', 'live')\n// true / false\n```\n\n### Setting variables\n\n```php\n$dotEnv = new DotEnv(__DIR__);\n$dotEnv-\u003eload();\n$dotEnv-\u003eset('CUSTOM_VARIABLE', 123);\n$dotEnv-\u003erequired('CUSTOM_VARIABLE');\n```\n\n### Variable referencing\n\nIf there is a variable that you'd like to inherit the value of you can use its name wrapped within the `${..}` i.e.\n\n```php\nMAIL_SMTP=true\nMAIL_USER=mail@mail.com\nMAIL_PASS=password\nMAIL_PORT=587\n\nMAIL_API_KEY=${MAIL_PASS}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiansulinski%2Fdotenv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastiansulinski%2Fdotenv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiansulinski%2Fdotenv/lists"}