/** * @Laravel * * Sensibly merge configuration arrays. * * * @param array ...$args * @return array */ function config_merge() { $result = []; foreach (func_get_args() as $arg) { foreach ($arg as $key => $value) { if (is_numeric($key)) { $result[] = $value; } elseif (array_key_exists($key, $result) && is_array($result[$key]) && is_array($value)) { $result[$key] = config_merge($result[$key], $value); } else { $result[$key] = $value; } } } return $result; }
/** * Merge two configs. * * @param mixed $target Target config * @param mixed $source Source config * @param bool $appendArrays if true use `array_merge` * * @return mixed The merged config */ function config_merge($target, $source, $appendArrays = false) { if (is_array($target) && is_array($source)) { return $appendArrays ? array_merge($target, $source) : $source; } if (!is_object($target) || !is_object($source)) { return $source; } $target = clone $target; foreach (get_object_vars($source) as $key => $value) { if (property_exists($target, $key)) { $target->{$key} = config_merge($target->{$key}, $value); continue; } $target->{$key} = $value; } return $target; }
/** * @param array $config * * @return object */ public function setConfig(array $config) { return $this->config = config_merge($this->config, $config); }
/** * Apply any cascades to an array of package options. * * @param string $env * @param string $package * @param string $group * @param array $items * @return array */ public function cascadePackage($env, $package, $group, $items) { // First we will look for a configuration file in the packages configuration // folder. If it exists, we will load it and merge it with these original // options so that we will easily "cascade" a package's configurations. $file = "packages/{$package}/{$group}.php"; if ($this->files->exists($path = $this->defaultPath . '/' . $file)) { $items = config_merge($items, $this->getRequire($path)); } // Once we have merged the regular package configuration we need to look for // an environment specific configuration file. If one exists, we will get // the contents and merge them on top of this array of options we have. $path = $this->getPackagePath($env, $package, $group); if ($this->files->exists($path)) { $items = config_merge($items, $this->getRequire($path)); } return $items; }