$value) { $path = self::parsePath($key); if (!is_array($path)) { $config[$key] = $value; } else { $loc = &$config; foreach ($path as $step) { $loc = &$loc[$step]; } $loc = !is_string($value) ? $value : addslashes($value); } } static::write((array) $config); } /** * Parse a string path to an array of config paths. * Will log a warning if a legacy path (using `/` is used). * * @param string $path Path to parse. * @return string|array Path split into sections or plain string if no section seperator was found. */ private static function parsePath(string $path) { if (str_contains($path, '.')) { return explode('.', $path); } // TODO: Remove for 2.1.0 if (str_contains($path, '/')) { ErrorHandler::logWarning("Legacy config path: {$path}. Please use periods to seperate paths."); return explode('/', $path); } return $path; } /** * Converts an array to a string to be inserted into the config file, with shorthand array syntax. * * @link https://gist.github.com/Bogdaan/ffa287f77568fcbb4cffa0082e954022 * @param array $config Config array to convert to string. * @return string PHP code for the config array */ private static function arrayToString(array $config): string { $export = var_export($config, true); $export = preg_replace("/^(' '*)(.*)/m", '$1$1$2', $export); $array = preg_split("/\r\n|\n|\r/", $export); $array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [null, ']$1', ' => ['], $array); return implode(PHP_EOL, array_filter(['['] + ($array ?: []))); } }