Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: vlucas/phpdotenv
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.1.0
Choose a base ref
...
head repository: vlucas/phpdotenv
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.1.1
Choose a head ref
  • 9 commits
  • 22 files changed
  • 3 contributors

Commits on Dec 15, 2019

  1. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    fdecd58 View commit details
  2. Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    f61ce48 View commit details
  3. Fixed CS

    GrahamCampbell committed Dec 15, 2019

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    420a2a8 View commit details

Commits on Dec 30, 2019

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    8d53579 View commit details

Commits on Jan 13, 2020

  1. Create .editorconfig (#404)

    Musa authored and GrahamCampbell committed Jan 13, 2020

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    53d71b0 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    92c402d View commit details

Commits on Mar 1, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    fe40858 View commit details
  2. PHPStan tweaks

    GrahamCampbell committed Mar 1, 2020

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    fd860ad View commit details
  3. Recommend ext-filter

    GrahamCampbell committed Mar 1, 2020

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    32bd5ca View commit details
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.{yml,yaml}]
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* text=auto

/tests export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -42,5 +42,5 @@ matrix:
- travis_retry docker run -it -w /data -v ${PWD}:/data:delegated --entrypoint composer registry.gitlab.com/grahamcampbell/php:7.4 install --no-suggest --prefer-dist -n -o
- travis_retry docker run -it -w /data -v ${PWD}:/data:delegated --entrypoint composer registry.gitlab.com/grahamcampbell/php:7.4 bin phpstan install --no-suggest --prefer-dist -n -o
script:
- docker run -it -w /data -v ${PWD}:/data:delegated --entrypoint vendor/bin/phpstan registry.gitlab.com/grahamcampbell/php:7.4 analyse src
- docker run -it -w /data -v ${PWD}:/data:delegated --entrypoint vendor/bin/phpstan registry.gitlab.com/grahamcampbell/php:7.4 analyse src -c phpstan.src.neon.dist
- docker run -it -w /data -v ${PWD}:/data:delegated --entrypoint vendor/bin/phpstan registry.gitlab.com/grahamcampbell/php:7.4 analyse tests -c phpstan.tests.neon.dist
81 changes: 46 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,55 @@
PHP dotenv
==========

Loads environment variables from `.env` to `getenv()`, `$_ENV` and
`$_SERVER` automagically.
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.

This is a PHP version of the original [Ruby
![Banner](https://user-images.githubusercontent.com/2829600/71564012-31105580-2a91-11ea-9ad7-ef1278411b35.png)

<p align="center">
<a href="https://travis-ci.org/vlucas/phpdotenv"><img src="https://img.shields.io/travis/vlucas/phpdotenv/master.svg?style=flat-square" alt="Build Status"></img></a>
<a href="LICENSE"><img src="https://img.shields.io/badge/license-BSD%203--Clause-brightgreen.svg?style=flat-square" alt="Software License"></img></a>
<a href="https://packagist.org/packages/vlucas/phpdotenv"><img src="https://img.shields.io/packagist/dt/vlucas/phpdotenv.svg?style=flat-square" alt="Total Downloads"></img></a>
<a href="https://github.com/vlucas/phpdotenv/releases"><img src="https://img.shields.io/github/release/vlucas/phpdotenv.svg?style=flat-square" alt="Latest Version"></img></a>
</p>


Why .env?
---------

**You should never store sensitive credentials in your code**. Storing
[configuration in the environment](http://www.12factor.net/config) is one of
the tenets of a [twelve-factor app](http://www.12factor.net/). Anything that is
likely to change between deployment environments – such as database credentials
or credentials for 3rd party services – should be extracted from the code into
environment variables.

Basically, a `.env` file is an easy way to load custom configuration variables
that your application needs without having to modify .htaccess files or
Apache/nginx virtual hosts. This means you won't have to edit any files outside
the project, and all the environment variables are always set no matter how you
run your project - Apache, Nginx, CLI, and even PHP 5.4's built-in webserver.
It's WAY easier than all the other ways you know of to set environment
variables, and you're going to love it!

* NO editing virtual hosts in Apache or Nginx
* NO adding `php_value` flags to .htaccess files
* EASY portability and sharing of required ENV values
* COMPATIBLE with PHP's built-in web server and CLI runner

PHP dotenv is a PHP version of the original [Ruby
dotenv](https://github.com/bkeepers/dotenv).

[![Build Status](https://travis-ci.org/vlucas/phpdotenv.svg?branch=master)](https://travis-ci.org/vlucas/phpdotenv)

Installation with Composer
--------------------------

Installation is super-easy via [Composer](https://getcomposer.org/):

```bash
$ composer require vlucas/phpdotenv
```

or add it by hand to your `composer.json` file.


UPGRADING FROM V3
@@ -46,37 +88,6 @@ For more details, please see the
the [upgrading guide](UPGRADING.md).


Why .env?
---------

**You should never store sensitive credentials in your code**. Storing
[configuration in the environment](http://www.12factor.net/config) is one of
the tenets of a [twelve-factor app](http://www.12factor.net/). Anything that is
likely to change between deployment environments – such as database credentials
or credentials for 3rd party services – should be extracted from the code into
environment variables.

Basically, a `.env` file is an easy way to load custom configuration variables
that your application needs without having to modify .htaccess files or
Apache/nginx virtual hosts. This means you won't have to edit any files outside
the project, and all the environment variables are always set no matter how you
run your project - Apache, Nginx, CLI, and even PHP 5.4's built-in webserver.
It's WAY easier than all the other ways you know of to set environment
variables, and you're going to love it!

* NO editing virtual hosts in Apache or Nginx
* NO adding `php_value` flags to .htaccess files
* EASY portability and sharing of required ENV values
* COMPATIBLE with PHP's built-in web server and CLI runner


Installation with Composer
--------------------------

```shell
composer require vlucas/phpdotenv
```

Usage
-----
The `.env` file is generally kept out of version control since it can contain
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -17,12 +17,13 @@
],
"require": {
"php": "^5.5.9 || ^7.0",
"phpoption/phpoption": "^1.7.1",
"phpoption/phpoption": "^1.7.2",
"symfony/polyfill-ctype": "^1.9"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0 || ^7.0",
"bamarni/composer-bin-plugin": "^1.3"
"ext-filter": "*",
"bamarni/composer-bin-plugin": "^1.3",
"phpunit/phpunit": "^4.8.35 || ^5.0 || ^6.0 || ^7.0"
},
"autoload": {
"psr-4": {
@@ -34,6 +35,9 @@
"Dotenv\\Tests\\": "tests/Dotenv/"
}
},
"suggest": {
"ext-filter": "Required to use the boolean validator."
},
"extra": {
"branch-alias": {
"dev-master": "4.1-dev"
7 changes: 1 addition & 6 deletions phpstan.neon.dist → phpstan.src.neon.dist
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
parameters:
level: max
ignoreErrors:
- '#Method Dotenv\\Loader\\Parser::processChar\(\) should return Dotenv\\Result\\Result<array<int, bool\|int\|string>, string> but return statement is missing.#'
- '#Method Dotenv\\Repository\\AdapterRepository::getInternal\(\) should return string\|null but return statement is missing.#'
- '#Variable \$defaults might not be defined.#'
- '/^Parameter \#1 \$readers of class Dotenv\\Repository\\[a-zA-Z]*Repository[a-zA-Z]* constructor expects/'
- '/^Parameter \#2 \$writers of class Dotenv\\Repository\\[a-zA-Z]*Repository[a-zA-Z]* constructor expects/'
- '#Method Dotenv\\Repository\\AbstractRepository::offset[a-zA-Z\(\)\$ ]* typehint specified.#'
- '#Method Dotenv\\Loader\\Lines::getCharPairs\(\) should return array\(array\(string, string\|null\)\) but returns array\<int, mixed\>.#'
- '#Unable to resolve the template type S in call to method PhpOption\\Option<mixed>::flatMap\(\)#'
- '#Method Dotenv\\Loader\\Parser::processChar\(\) should return Dotenv\\Result\\Result<array<int, bool\|int\|string>, string> but returns Dotenv\\Result\\Result<array<int, [a-z\|\,]+>, mixed>.#'
- '#Parameter \#1 \$callable of method PhpOption\\Option<Dotenv\\Loader\\Value|null>::map\(\) expects callable\(Dotenv\\Loader\\Value\|null\): mixed, Closure\(Dotenv\\Loader\\Value\): mixed given.#'
- '#Parameter \#1 \$callable of method PhpOption\\Option<Dotenv\\Loader\\Value\|null>::map\(\) expects callable\(Dotenv\\Loader\\Value\|null\): mixed, Closure\(Dotenv\\Loader\\Value\): mixed given.#'
- '#Parameter \#1 \$callable of method PhpOption\\Option<array\|null>::[a-z]+\(\) expects callable\(array\|null\): [a-z]+, Closure\(array\): mixed given.#'
- '#Method Dotenv\\Repository\\Adapter\\PutenvAdapter::get\(\) should return PhpOption\\Option<string> but returns PhpOption\\Option<bool\|string>.#'
- '#Method Dotenv\\Store\\File\\Reader::readFromFile\(\) should return PhpOption\\Option<string> but returns PhpOption\\Option<bool\|string>.#'
5 changes: 4 additions & 1 deletion phpstan.tests.neon.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
parameters:
level: 5
level: 6
checkMissingIterableValueType: false
ignoreErrors:
- '#Parameter \#1 \$name of method Dotenv\\Repository\\RepositoryInterface::[a-z]+\(\) expects string, [a-z]+ given.#'
- '#no return typehint specified#'
- '#with no typehint#'
1 change: 1 addition & 0 deletions src/Loader/Lines.php
Original file line number Diff line number Diff line change
@@ -111,6 +111,7 @@ private static function getCharPairs($line)
{
$chars = str_split($line);

/** @var array{array{string,string|null}} */
return array_map(null, $chars, array_slice($chars, 1));
}

3 changes: 3 additions & 0 deletions src/Loader/Parser.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
use Dotenv\Exception\InvalidFileException;
use Dotenv\Result\Error;
use Dotenv\Result\Success;
use RuntimeException;

class Parser
{
@@ -192,6 +193,8 @@ private static function processChar($state, $char)
}
case self::COMMENT_STATE:
return Success::create(['', false, self::COMMENT_STATE]);
default:
throw new RuntimeException('Parser entered invalid state.');
}
}

4 changes: 2 additions & 2 deletions src/Regex/Regex.php
Original file line number Diff line number Diff line change
@@ -78,8 +78,8 @@ public static function split($pattern, $subject)
*
* @template V
*
* @param callable(string): V $operation
* @param string $subject
* @param callable(string):V $operation
* @param string $subject
*
* @return \Dotenv\Result\Result<V,string>
*/
2 changes: 1 addition & 1 deletion src/Repository/Adapter/ApacheAdapter.php
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ public function isSupported()
*
* @param string $name
*
* @return \PhpOption\Option<string>
* @return \PhpOption\Option<string|null>
*/
public function get($name)
{
2 changes: 1 addition & 1 deletion src/Repository/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ public function isSupported()
*
* @param string $name
*
* @return \PhpOption\Option<string>
* @return \PhpOption\Option<string|null>
*/
public function get($name)
{
2 changes: 1 addition & 1 deletion src/Repository/Adapter/EnvConstAdapter.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ public function isSupported()
*
* @param string $name
*
* @return \PhpOption\Option<string>
* @return \PhpOption\Option<string|null>
*/
public function get($name)
{
3 changes: 2 additions & 1 deletion src/Repository/Adapter/PutenvAdapter.php
Original file line number Diff line number Diff line change
@@ -21,10 +21,11 @@ public function isSupported()
*
* @param string $name
*
* @return \PhpOption\Option<string>
* @return \PhpOption\Option<string|null>
*/
public function get($name)
{
/** @var \PhpOption\Option<string|null> */
return Option::fromValue(getenv($name), false);
}

2 changes: 1 addition & 1 deletion src/Repository/Adapter/ReaderInterface.php
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ interface ReaderInterface extends AvailabilityInterface
*
* @param string $name
*
* @return \PhpOption\Option<string>
* @return \PhpOption\Option<string|null>
*/
public function get($name);
}
2 changes: 1 addition & 1 deletion src/Repository/Adapter/ServerConstAdapter.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ public function isSupported()
*
* @param string $name
*
* @return \PhpOption\Option<string>
* @return \PhpOption\Option<string|null>
*/
public function get($name)
{
2 changes: 2 additions & 0 deletions src/Repository/AdapterRepository.php
Original file line number Diff line number Diff line change
@@ -51,6 +51,8 @@ protected function getInternal($name)
return $result->get();
}
}

return null;
}

/**
4 changes: 2 additions & 2 deletions src/Result/Error.php
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public function success()
*
* @template S
*
* @param callable(T): S $f
* @param callable(T):S $f
*
* @return \Dotenv\Result\Result<S,E>
*/
@@ -82,7 +82,7 @@ public function error()
*
* @template F
*
* @param callable(E): F $f
* @param callable(E):F $f
*
* @return \Dotenv\Result\Result<T,F>
*/
4 changes: 2 additions & 2 deletions src/Result/Result.php
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ public function getSuccess()
*
* @template S
*
* @param callable(T): S $f
* @param callable(T):S $f
*
* @return \Dotenv\Result\Result<S,E>
*/
@@ -62,7 +62,7 @@ public function getError()
*
* @template F
*
* @param callable(E): F $f
* @param callable(E):F $f
*
* @return \Dotenv\Result\Result<T,F>
*/
4 changes: 2 additions & 2 deletions src/Result/Success.php
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public function success()
*
* @template S
*
* @param callable(T): S $f
* @param callable(T):S $f
*
* @return \Dotenv\Result\Result<S,E>
*/
@@ -82,7 +82,7 @@ public function error()
*
* @template F
*
* @param callable(E): F $f
* @param callable(E):F $f
*
* @return \Dotenv\Result\Result<T,F>
*/
1 change: 1 addition & 0 deletions src/Store/File/Reader.php
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ private static function readFromFile($filePath)
{
$content = @file_get_contents($filePath);

/** @var \PhpOption\Option<string> */
return Option::fromValue($content, false);
}
}
2 changes: 1 addition & 1 deletion vendor-bin/phpstan/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"phpstan/phpstan": "^0.12.3"
"phpstan/phpstan": "^0.12.12"
}
}