mnapoli/PHP-DI — pull request #176 by jmalloc (2014-08-06 03:52:01+00:00)
Title: New definition type for reading environment variables.

### Overview

This PR contains an implementation of a new definition type `EnvironmentVariableDefinition` which allows for easy reading of a system environment variable with fallback to a default value if the environment variable is not defined.

### Rationale

I've had the need to create several container definitions that look something like the following:

```php
return [
'my-app.dsn' => DI\factory(
function (DI\Container $container) {
$url = isset($_SERVER['DATABASE_URL'])
? $_SERVER['DATABASE_URL']
: 'postgresql://user:pass@localhost/db';
}
)
];
```

I figured this might be a common situation for other developers, especially those deploying their applications to systems like Heroku, or otherwise following the principles of [12-factor applications](http://12factor.net/config).

### Usage

The following snippet is equivalent to the example given above:

```php
return [
'my-app.dsn' => DI\env('DATABASE_URL', 'postgresql://user:pass@localhost/db')
];
```

The first parameter to `DI\env()` is the name of the environment variable to read, the second parameter is the default value to use in the event that the environment variable is not defined.

If the environment variable is not defined and no default is provided, a `DefinitionException` is thrown when attempting to resolve the value.

Finally, default values can reference other definitions using `DI\link()`:

```php
return [
'my-app.default-dsn' => 'postgresql://user:pass@localhost/db',
'my-app.dsn' => DI\env('DATABASE_URL', DI\link('my-app.default-dsn'))
];
```

### Tests

Unit tests and integration tests have been implemented.

### Remaining Issues

I think this PR is usable as it stands, however one thing I was unsure of is how strict the `isResolvable()` implementation needs to be. For example, it does not currently follow references made with `DI\link()` to check that they are also resolvable.

Hopefully this a is a feature that you would consider desirable, and of course if there's anything that needs to be corrected please let me know.