5 * @link http://mnapoli.github.com/PHP-DI/
6 * @copyright Matthieu Napoli (http://mnapoli.fr/)
7 * @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
10 namespace DI\Definition\Resolver;
12 use DI\Definition\Definition;
13 use DI\Definition\EnvironmentVariableDefinition;
14 use DI\Definition\Exception\DefinitionException;
15 use DI\Definition\Helper\DefinitionHelper;
18 * Resolves a environment variable definition to a value.
20 * @author James Harris <james.harris@icecave.com.au>
22 class EnvironmentVariableResolver implements DefinitionResolver
25 * @var DefinitionResolver
27 private $definitionResolver;
32 private $variableReader;
34 public function __construct(DefinitionResolver $definitionResolver, $variableReader = 'getenv')
36 $this->definitionResolver = $definitionResolver;
37 $this->variableReader = $variableReader;
41 * Resolve an environment variable definition to a value.
43 * @param EnvironmentVariableDefinition $definition
47 public function resolve(Definition $definition, array $parameters = [])
49 $value = call_user_func($this->variableReader, $definition->getVariableName());
51 if (false !== $value) {
55 if (!$definition->isOptional()) {
56 throw new DefinitionException(sprintf(
57 "The environment variable '%s' has not been defined",
58 $definition->getVariableName()
62 $value = $definition->getDefaultValue();
65 if ($value instanceof DefinitionHelper) {
66 return $this->definitionResolver->resolve($value->getDefinition(''));
73 * @param EnvironmentVariableDefinition $definition
77 public function isResolvable(Definition $definition, array $parameters = [])