cookiebot
/
addons
/
inc
/
Dependencies
/
DI
/
Definition
/
Resolver
/
EnvironmentVariableResolver.php
AliasResolver.php
5 years ago
ArrayResolver.php
5 years ago
DecoratorResolver.php
5 years ago
DefinitionResolver.php
5 years ago
EnvironmentVariableResolver.php
5 years ago
FactoryResolver.php
5 years ago
InstanceInjector.php
5 years ago
ObjectCreator.php
5 years ago
ParameterResolver.php
5 years ago
ResolverDispatcher.php
5 years ago
StringResolver.php
5 years ago
ValueResolver.php
5 years ago
EnvironmentVariableResolver.php
95 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PHP-DI |
| 4 | * |
| 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) |
| 8 | */ |
| 9 | |
| 10 | namespace Cybot\Dependencies\DI\Definition\Resolver; |
| 11 | |
| 12 | use Cybot\Dependencies\DI\Definition\Definition; |
| 13 | use Cybot\Dependencies\DI\Definition\EnvironmentVariableDefinition; |
| 14 | use Cybot\Dependencies\DI\Definition\Exception\DefinitionException; |
| 15 | use Cybot\Dependencies\DI\Definition\Helper\DefinitionHelper; |
| 16 | |
| 17 | /** |
| 18 | * Resolves a environment variable definition to a value. |
| 19 | * |
| 20 | * @author James Harris <james.harris@icecave.com.au> |
| 21 | */ |
| 22 | class EnvironmentVariableResolver implements DefinitionResolver |
| 23 | { |
| 24 | /** |
| 25 | * @var DefinitionResolver |
| 26 | */ |
| 27 | private $definitionResolver; |
| 28 | |
| 29 | /** |
| 30 | * @var callable |
| 31 | */ |
| 32 | private $variableReader; |
| 33 | |
| 34 | public function __construct(DefinitionResolver $definitionResolver, $variableReader = 'getenv') |
| 35 | { |
| 36 | $this->definitionResolver = $definitionResolver; |
| 37 | $this->variableReader = $variableReader; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Resolve an environment variable definition to a value. |
| 42 | * |
| 43 | * @param EnvironmentVariableDefinition $definition |
| 44 | * |
| 45 | * {@inheritdoc} |
| 46 | */ |
| 47 | public function resolve(Definition $definition, array $parameters = []) |
| 48 | { |
| 49 | $this->assertIsEnvironmentVariableDefinition($definition); |
| 50 | |
| 51 | $value = call_user_func($this->variableReader, $definition->getVariableName()); |
| 52 | |
| 53 | if (false !== $value) { |
| 54 | return $value; |
| 55 | } elseif (!$definition->isOptional()) { |
| 56 | throw new DefinitionException(sprintf( |
| 57 | "The environment variable '%s' has not been defined", |
| 58 | $definition->getVariableName() |
| 59 | )); |
| 60 | } |
| 61 | |
| 62 | $value = $definition->getDefaultValue(); |
| 63 | |
| 64 | // Nested definition |
| 65 | if ($value instanceof DefinitionHelper) { |
| 66 | return $this->definitionResolver->resolve($value->getDefinition('')); |
| 67 | } |
| 68 | |
| 69 | return $value; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param EnvironmentVariableDefinition $definition |
| 74 | * |
| 75 | * {@inheritdoc} |
| 76 | */ |
| 77 | public function isResolvable(Definition $definition, array $parameters = []) |
| 78 | { |
| 79 | $this->assertIsEnvironmentVariableDefinition($definition); |
| 80 | |
| 81 | return $definition->isOptional() |
| 82 | || false !== call_user_func($this->variableReader, $definition->getVariableName()); |
| 83 | } |
| 84 | |
| 85 | private function assertIsEnvironmentVariableDefinition(Definition $definition) |
| 86 | { |
| 87 | if (!$definition instanceof EnvironmentVariableDefinition) { |
| 88 | throw new \InvalidArgumentException(sprintf( |
| 89 | 'This definition resolver is only compatible with EnvironmentVariableDefinition objects, %s given', |
| 90 | get_class($definition) |
| 91 | )); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 |