$property)) { foreach ($this->$property as $key => $val) { if ($value = $this->getEnvValue("{$property}.{$key}", $prefix, $shortPrefix)) { if (is_null($value)) continue; if ($value === 'false') $value = false; elseif ($value === 'true') $value = true; $this->$property[$key] = $value; } } } else { if (($value = $this->getEnvValue($property, $prefix, $shortPrefix)) !== false) { if (is_null($value)) continue; if ($value === 'false') $value = false; elseif ($value === 'true') $value = true; $this->$property = $value; } } } $this->registerProperties(); } //-------------------------------------------------------------------- /** * Retrieve an environment-specific configuration setting * * @param string $property * @param string $prefix * @param string $shortPrefix * * @return mixed */ protected function getEnvValue(string $property, string $prefix, string $shortPrefix) { $shortPrefix = ltrim( $shortPrefix, '\\' ); switch (true) { case array_key_exists( "{$shortPrefix}.{$property}", $_ENV ): return $_ENV["{$shortPrefix}.{$property}"]; break; case array_key_exists( "{$shortPrefix}.{$property}", $_SERVER ): return $_SERVER["{$shortPrefix}.{$property}"]; break; case array_key_exists( "{$prefix}.{$property}", $_ENV ): return $_ENV["{$prefix}.{$property}"]; break; case array_key_exists( "{$prefix}.{$property}", $_SERVER ): return $_SERVER["{$prefix}.{$property}"]; break; default: $value = getenv( $property ); return $value === false ? null : $value; } } //-------------------------------------------------------------------- /** * Provides external libraries a simple way to register one or more * options into a config file. */ protected function registerProperties() { if (empty($this->registrars)) return; $shortName = (new \ReflectionClass($this))->getShortName(); // Check the registrar class for a method named after this class' shortName foreach ($this->registrars as $callable) { // ignore non-applicable registrars if ( ! method_exists($callable, $shortName)) continue; $properties = $callable::$shortName(); if ( ! is_array($properties)) { throw new \RuntimeException('Registrars must return an array of properties and their values.'); } foreach ($properties as $property => $value) { if (is_array($this->$property) && is_array($value)) { $this->$property = array_merge($this->$property, $value); } else { $this->$property = $value; } } } } //-------------------------------------------------------------------- }