initEnvValue($this->$property, $property, $prefix, $shortPrefix); } if (defined('ENVIRONMENT') && ENVIRONMENT !== 'testing') { // well, this won't happen during unit testing // @codeCoverageIgnoreStart $this->registerProperties(); // @codeCoverageIgnoreEnd } } //-------------------------------------------------------------------- /** * Initialization an environment-specific configuration setting * * @param mixed &$property * @param string $name * @param string $prefix * @param string $shortPrefix * * @return mixed */ protected function initEnvValue(&$property, string $name, string $prefix, string $shortPrefix) { if (is_array($property)) { foreach ($property as $key => $val) { $this->initEnvValue($property[$key], "{$name}.{$key}", $prefix, $shortPrefix); } } else { if (($value = $this->getEnvValue($name, $prefix, $shortPrefix)) !== false) { if (! is_null($value)) { if ($value === 'false') { $value = false; } elseif ($value === 'true') { $value = true; } $property = is_bool($value) ? $value : trim($value, '\'"'); } } } return $property; } //-------------------------------------------------------------------- /** * 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. * * @throws \ReflectionException */ protected function registerProperties() { if (! static::$moduleConfig->shouldDiscover('registrars')) { return; } if (! static::$didDiscovery) { $locator = \Config\Services::locator(); $registrarsFiles = $locator->search('Config/Registrar.php'); foreach ($registrarsFiles as $file) { $className = $locator->getClassname($file); static::$registrars[] = new $className(); } static::$didDiscovery = true; } $shortName = (new \ReflectionClass($this))->getShortName(); // Check the registrar class for a method named after this class' shortName foreach (static::$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 (isset($this->$property) && is_array($this->$property) && is_array($value)) { $this->$property = array_merge($this->$property, $value); } else { $this->$property = $value; } } } } //-------------------------------------------------------------------- }