Merge configuration on a single level

@param  array $global
@param  array $builder

@return array
342322
protected function mergeConfiguration(array $global, array $builder)
    {
        foreach ($global as $name => &$value) {
            if (!array_key_exists($name, $builder) || is_null($builder[$name])) {
                continue;
            }

            if (!is_array($value)) {
                $value = $builder[$name];

                continue;
            }

            if (!is_array($builder[$name])) {
                throw new \InvalidArgumentException('Invalid generator');
            }

            $value = array_replace($value, $builder[$name]);
        }

        return array_merge($global, array_diff_key($builder, $global));
    }
1291353
public function mergeConfig($resources)
    {
        $configs = array();
        foreach (is_array($resources) ? $resources : func_get_args() as $resource) {
            $configs = ArrayUtils::merge($configs, $this->loadConfig($resource));
        }

        return $configs;
    }
1290886
protected function getConfig(): array
    {
        $loader = null;
        if ($this->isCacheEnabled() === true) {
            $loader = $this->getLoader();
            $cached = $loader->load();
            if (empty($cached) === false) {
                return $cached;
            }
        }

        if ($this->isFrameworkEnabled() === true) {
            $this->addFrameworkConfigs();
        }

        $merged = [];
        $merger = $this->getMerger();
        foreach ($this->getConfigs() as $config) {
            $merged = $merger->merge(
                $merged,
                $config->load()
            );
        }

        foreach ($this->getGlobalConfig() as $global) {
            $merged = $merger->merge($merged, $global);
        }

        foreach ($this->getModuleConfig() as $module) {
            $merged = $merger->merge($merged, $module);
        }

        if ($this->isCacheEnabled() === true) {
            if ($loader instanceof CacheLoader) {
                $loader->save($merged);
            }
        }

        return $merged;
    }
31793
public function getConfigTreeBuilder()
    {
        $builder = new TreeBuilder();
        $cors    = $builder->root('cors');
        {
            $cors->children()->scalarNode('pattern')->isRequired();
            $cors->children()->variableNode('origins')->beforeNormalization()->ifString()->then(
                function ($v) {
                    return [$v];
                }
            );
            $cors->children()->variableNode('headers');
            $cors->children()->variableNode('headers_exposed');
            $cors->children()->integerNode('max_age')->defaultValue(86400);
            $cors->children()->booleanNode('credentials_allowed')->defaultValue(false);
        }

        return $builder;
    }
780406
private static function arrayMergeRecursive($a, $b, $level = 0)
    {
        // merge arrays if both variables are arrays
        if (is_array($a) && is_array($b)) {
            // loop through each right array's entry and merge it into $a
            foreach ($b as $key => $value) {
                if (isset($a[$key])) {
                    if (is_int($key) && ! is_array($value)) {
                        $a[] = $value;
                    } else {
                        $a[$key] = self::arrayMergeRecursive($a[$key], $value, $level + 1);
                    }
                } else {
                    if ($key === 0) {
                        $a = array(
                            0 => self::arrayMergeRecursive($a, $value, $level + 1)
                        );
                    } else {
                        $a[$key] = $value;
                    }
                }
            }
        } else {
            // at least one of values is not an array
            $a = $b;
        }
        return $a;
    }
712938
public function load(array $configs, ContainerBuilder $builder)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($builder, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yaml');
        $loader->load('services/factories.yaml');
        $loader->load('services/fields.yaml');
        $loader->load('services/resources.yaml');
        $loader->load('services/subscribers.yaml');
        $loader->load('services/twig.yaml');

        if ('dev' === $builder->getParameter('kernel.environment')) {
            $loader->load('services_dev.yaml');
        }
        $applicationConfig = [];
        $adminsConfig = [];
        $menusConfig = [];
        $enableExtraConfig = true;

        if (key_exists('application', $config)) {
            if (key_exists('enable_extra_configuration', $config['application'])) {
                $enableExtraConfig = $config['application']['enable_extra_configuration'];
            }
            $applicationConfig = $config['application'];
        }

        if (key_exists('admins', $config)) {
            $adminsConfig = $config['admins'];
        }

        if (key_exists('menus', $config)) {
            $menusConfig = $config['menus'];

            foreach ($menusConfig as $name => $menu) {
                if (null === $menu) {
                    $menusConfig[$name] = [];
                }
            }
        }
        $builder->setParameter('lag.admin.enable_extra_configuration', $enableExtraConfig);
        $builder->setParameter('lag.admin.application_configuration', $applicationConfig);
        $builder->setParameter('lag.admins', $adminsConfig);
        $builder->setParameter('lag.menus', $menusConfig);
    }
886723
public function setConfig($key, $value = null, $merge = true)
    {
        $this->_defaultConfig = [
            'messages' => [
                'success' => __d('core', 'Object has been moved'),
                'error'   => __d('core', 'Object could not been moved')
            ],
            'action' => 'index',
        ];

        return parent::setConfig($key, $value, $merge);
    }
497346
protected function _MergeDisabledPermissions(&$GlobalPermissions) {
		// Get the default permissions for junctions that are disabled.
		$DisabledPermissions = C('Garden.Permissions.Disabled');
		if(!$DisabledPermissions)
			return;

		$DisabledIn = array();
		foreach($DisabledPermissions as $JunctionTable => $Disabled) {
			if($Disabled)
				$DisabledIn[] = $JunctionTable;
		}

		if(!$DisabledIn)
			return;

		$DisabledData = $this->SQL
			->Select('*')
			->From('Permission p')
			->Where('p.RoleID', 0)
			->WhereIn('p.JunctionTable', $DisabledIn)
			->Get()->ResultArray();

		$DefaultRow =& $GlobalPermissions[0];

		// Loop through each row and add it's default definition to the global permissions.
		foreach($DisabledData as $PermissionRow) {
			foreach($PermissionRow as $ColumnName => $Value) {
				if(in_array($ColumnName, array('PermissionID', 'RoleID', 'JunctionTable', 'JunctionColumn', 'JunctionID')))
					continue;

				if($Value & 2) {
					$Setting = $Value | GetValue($ColumnName, $DefaultRow, 0);
					SetValue($ColumnName, $DefaultRow, $Setting);
				}
			}
		}
	}
1347912
public function merge(array $base, array $merge): array
    {
        $options = array_merge($base, $merge); // Base should include all existing.
        $options = array_intersect_key($options, $this->App->Config->§default_options);

        foreach ($this->App->Config->§default_options as $_key => $_default_option_value) {
            if (!isset($options[$_key])) {
                $options[$_key] = $_default_option_value;
            } elseif (!$this->App->Config->§specs['§is_pro'] && in_array($_key, $this->App->Config->§pro_option_keys, true)) {
                $options[$_key] = $_default_option_value;
            } elseif (!$this->App->Config->§specs['§is_elite'] && in_array($_key, $this->App->Config->§elite_option_keys, true)) {
                $options[$_key] = $_default_option_value;
            } else {
                settype($options[$_key], gettype($_default_option_value));
            }
        } // unset($_key, $_default_option_value);

        return $options;
    }
1216475
protected function mergeModuleConfig(string $key, array $config)
    {
        if (!empty($config)) {
            $current = $this->app['config']->get($key);
            if (is_array($current)) {
                $this->app['config']->set($key, array_merge($current, $config));
            } else {
                $this->app['config']->set($key, $config);
            }
        }
    }
777243
public static function merge($data, $merge) {
		foreach ($data as $key => $config) {
			$name = $key;
			if (is_numeric($name)) {
				$name = $config;
			}

			foreach ($merge as $k => $v) {
				if (in_array($name, array($k, $v), true)) {
					unset($data[$key]);
				}
			}

		}

		return array_merge($data, $merge);
	}
907926
public function merge(Zend_Config $merge)
    {
        foreach($merge as $key => $item) {
            if(array_key_exists($key, $this->_data)) {
                if($item instanceof Zend_Config && $this->$key instanceof Zend_Config) {
                    $this->$key = $this->$key->merge(new Zend_Config($item->toArray(), !$this->readOnly()));
                } else {
                    $this->$key = $item;
                }
            } else {
                if($item instanceof Zend_Config) {
                    $this->$key = new Zend_Config($item->toArray(), !$this->readOnly());
                } else {
                    $this->$key = $item;
                }
            }
        }

        return $this;
    }
1321773
public function merge(array $metadata): array
	{
		$tables = [];
		foreach ($metadata as $entity) {
			$tables[$entity->getTableName()][] = $entity;
		}

		$merged = [];
		foreach ($tables as $table => $metadata) {

			if (count($metadata) === 1) {
				$merged[] = array_shift($metadata);
				continue;
			}

			$primary = $this->resolvePrimaryMetadata($metadata);
			foreach ($metadata as $secondary) {
				$primary = $this->mergeMetadata($primary, $secondary);
			}

			$primary->fieldMappings = $this->mappingCleanup($primary->fieldMappings);
			$primary->associationMappings = $this->mappingCleanup($primary->associationMappings);
			$merged[] = $primary;
		}

		return $merged;
	}
780216
public function registerGlobalScopes($builder)
    {
        foreach ($this->getGlobalScopes() as $identifier => $scope) {
            $builder->withGlobalScope($identifier, $scope);
        }

        return $builder;
    }
651339
public function setConfig(array $config, $merge = false)
    {
        $this->config = ($merge)
            ? array_merge($this->config, $config)
            : $config;

        $this->validateConfig();

        return $this;
    }
419519
public function mergeConstraintsFrom(Builder $from)
    {
        $whereBindings = array_get(
            $from->getQuery()->getRawBindings(), 'where', []
        );
        // Here we have some other query that we want to merge the where constraints from. We will
        // copy over any where constraints on the query as well as remove any global scopes the
        // query might have removed. Then we will return ourselves with the finished merging.
        return $this->withoutGlobalScopes(
            $from->removedScopes()
        )->mergeWheres(
            $from->getQuery()->wheres, $whereBindings
        );
    }
508041
private function loadParameters(array $mergedConfig, ContainerBuilder $container): BaseExtension
    {
        /*
         * No configuration?
         * Nothing to do
         */
        if (empty($mergedConfig)) {
            return $this;
        }

        // Getting the keys or paths on which building names of parameters should stop
        $keysToStop = $this->getKeysToStopLoadingParametersOn();
        $globalKeysToStop = $this->getGlobalKeysToStopLoadingParametersOn();

        // Merging standard with global keys and paths
        $keysToStop = Arrays::makeArray($keysToStop);
        $globalKeysToStop = Arrays::makeArray($globalKeysToStop);
        $stopIfMatchedBy = array_merge($keysToStop, $globalKeysToStop);

        // Let's get the last elements' paths and load values into container
        $parameters = Arrays::getLastElementsPaths($mergedConfig, '.', '', $stopIfMatchedBy);

        /** @var ConfigurationInterface $configuration */
        $configuration = $this->getConfiguration($mergedConfig, $container);

        // Getting slug of bundle's name
        $bundleShortName = $configuration
            ->getConfigTreeBuilder()
            ->buildTree()
            ->getName()
        ;

        foreach ($parameters as $name => $value) {
            if (!\is_array($value)) {
                $value = Miscellaneous::trimSmart($value);
            }

            // Loading parameter into container, prefixed by slug of bundle's name
            $prefixedName = sprintf('%s.%s', $bundleShortName, $name);
            $container->setParameter($prefixedName, $value); // e.g. simple_bundle.foo.bar.something => 'my-value'
        }

        return $this;
    }