Convert mixed action configs to unified structure

[
'ACTION_1' => [..config...],
'ACTION_2' => [..config...],
'ACTION_N' => [..config...]
]

@param array $actions Actions
@return array
541604
protected function _normalizeActions($actions)
    {
        $normalized = [];
        foreach ($actions as $key => $config) {
            if (is_array($config)) {
                $normalized[$key] = $config;
            } else {
                $normalized[$config] = [];
            }
        }

        return $normalized;
    }
1353034
public static function recursiveMerge(...$arrays)
    {
        $merged = array ();
        foreach($arrays as $array)
        {
            if (!is_array($array))
            {
                continue;
            }
            foreach ( $array as $key => $value )
            {
                if (is_string ( $key ))
                {
                    if (is_array ( $value ) && isset($merged[$key]) && is_array ( $merged [$key] ))
                    {
                        $merged [$key] = static::recursiveMerge ( $merged [$key], $value );
                    }
                    else
                    {
                        $merged [$key] = $value;
                    }
                }
                else
                {
                    $merged [] = $value;
                }
            }
        }
        return $merged;
    }
1037386
public static function map($actions=array(), $options=array())
    {
        if (empty($actions)) {
            return null;
        }

        $out   = array();
        $count = 0;
        $len   = count($actions);

        foreach ($actions as $label => $mixed) {

            $count++;

            $action = new Item($label, $mixed);

            if ($count == 1) {
                $action->isFirst = true;
            }

            if ($count == $len) {
                $action->isLast = true;
            }

            $out[] = $action;

        }

        return $out;
    }
938291
protected function gatherTableMoves($actions)
    {
        collection($actions)
            ->filter(function ($action) {
                return $action instanceof DropTable
                    || $action instanceof RenameTable
                    || $action instanceof ChangePrimaryKey
                    || $action instanceof ChangeComment;
            })
            ->each(function ($action) {
                $table = $action->getTable();
                $name = $table->getName();

                if (!isset($this->tableMoves[$name])) {
                    $this->tableMoves[$name] = new AlterTable($table);
                }

                $this->tableMoves[$name]->addAction($action);
            });
    }
166919
protected function mergeAsGroup(array $action, $as = null)
    {
        if (isset($as) && ! empty($as)) {
            if (isset($action['as'])) {
                $action['as'] = $as.'.'.$action['as'];
            } else {
                $action['as'] = $as;
            }
        }

        return $action;
    }
156232
protected function mergeConfigs(array $configs)
    {
        $merged = array();
        foreach ($configs as $config) {
            $merged = array_merge($merged, $config);
        }

        return $merged;
    }
1330116
public function merge($actions)
	{
		if (is_string($actions))
		{
			$actions = json_decode($actions, true);
		}

		if (is_array($actions))
		{
			foreach ($actions as $action => $identities)
			{
				$this->mergeAction($action, $identities);
			}
		}
		elseif ($actions instanceof Rules)
		{
			$data = $actions->getData();

			foreach ($data as $name => $identities)
			{
				$this->mergeAction($name, $identities);
			}
		}
	}
897030
protected function gatherConstraints($actions)
    {
        collection($actions)
            ->filter(function ($action) {
                return $action instanceof AddForeignKey
                    || $action instanceof DropForeignKey;
            })
            ->each(function ($action) {
                $table = $action->getTable();
                $name = $table->getName();

                if (!isset($this->constraints[$name])) {
                    $this->constraints[$name] = new AlterTable($table);
                }

                $this->constraints[$name]->addAction($action);
            });
    }
1224339
protected function gatherIndexes($actions)
    {
        collection($actions)
            ->filter(function ($action) {
                return $action instanceof AddIndex
                    || $action instanceof DropIndex;
            })
            ->reject(function ($action) {
                // Indexes for new tables are created inline
                // so we don't wan't them here too
                return isset($this->tableCreates[$action->getTable()->getName()]);
            })
            ->each(function ($action) {
                $table = $action->getTable();
                $name = $table->getName();

                if (!isset($this->indexes[$name])) {
                    $this->indexes[$name] = new AlterTable($table);
                }

                $this->indexes[$name]->addAction($action);
            });
    }
709960
protected function mergeNamespaceGroup(array $action)
    {
        if (isset($this->groupAttributes['namespace']) && isset($action['uses'])) {
            $namespace = trim($this->groupAttributes['namespace'], '\\');

            $action['uses'] = $namespace ? $namespace.'\\'.$action['uses'] : $action['uses'];
        }

        return $action;
    }
948103
protected function _getControllerActions()
    {
        $table = $entity = [];

        $actions = $this->_getAllowedActions();
        foreach ($actions as $actionName => $config) {
            list($scope, $actionConfig) = $this->_getControllerActionConfiguration($actionName, $config);
            ${$scope}[$actionName] = $actionConfig;
        }

        $actionBlacklist = [];
        $groups = $this->_action()->getConfig('scaffold.action_groups') ?: [];
        foreach ($groups as $group) {
            $group = Hash::normalize($group);
            foreach ($group as $actionName => $config) {
                if (isset($table[$actionName]) || isset($entity[$actionName])) {
                    continue;
                }
                if ($config === null) {
                    $config = [];
                }
                list($scope, $actionConfig) = $this->_getControllerActionConfiguration($actionName, $config);
                $realAction = Hash::get($actionConfig, 'url.action', $actionName);
                if (!isset(${$scope}[$realAction])) {
                    continue;
                }

                $actionBlacklist[] = $realAction;
                ${$scope}[$actionName] = $actionConfig;
            }
        }

        foreach ($actionBlacklist as $actionName) {
            unset($table[$actionName]);
            unset($entity[$actionName]);
        }

        return compact('table', 'entity');
    }
647295
protected function parseAsSidebarActions(array $actions)
    {
        $sidebarActions = [];
        foreach ($actions as $ident => $action) {
            $ident  = $this->parseActionIdent($ident, $action);
            $action = $this->parseActionItem($action, $ident, true);

            if (!isset($action['priority'])) {
                $action['priority'] = $this->actionsPriority++;
            }

            if ($action['ident'] === 'view' && !$this->isObjViewable()) {
                $action['active'] = false;
            } elseif ($action['ident'] === 'save' && !$this->isObjSavable()) {
                $action['active'] = false;
            } elseif ($action['ident'] === 'reset' && !$this->isObjResettable()) {
                $action['active'] = false;
            } elseif ($action['ident'] === 'delete' && !$this->isObjDeletable()) {
                $action['active'] = false;
            }

            if ($action['isSubmittable'] && !$this->isObjSavable()) {
                $action['active'] = false;
            }

            if ($action['actions']) {
                $action['actions']    = $this->parseAsSidebarActions($action['actions']);
                $action['hasActions'] = !!array_filter($action['actions'], function ($action) {
                    return $action['active'];
                });
            }

            if (isset($sidebarActions[$ident])) {
                $hasPriority = ($action['priority'] > $sidebarActions[$ident]['priority']);
                if ($hasPriority || $action['isSubmittable']) {
                    $sidebarActions[$ident] = array_replace($sidebarActions[$ident], $action);
                } else {
                    $sidebarActions[$ident] = array_replace($action, $sidebarActions[$ident]);
                }
            } else {
                $sidebarActions[$ident] = $action;
            }
        }

        usort($sidebarActions, [ $this, 'sortActionsByPriority' ]);

        while (($first = reset($sidebarActions)) && $first['isSeparator']) {
            array_shift($sidebarActions);
        }

        while (($last = end($sidebarActions)) && $last['isSeparator']) {
            array_pop($sidebarActions);
        }

        return $sidebarActions;
    }
312695
public function set($actions)
    {
        if (! is_array($actions)) {
            $actions = func_get_args();
        }

        $this->clear();

        foreach ($actions as $action) {
            $this->push($action);
        }

        return $this->getDisplay();
    }
1099384
public function normalizeAction($actions): array
    {
        $addNamespace = function ($action) {
            if ($action = ltrim($action, '\\')) {
                return $action;
            }

            return app()->getNamespace().'\\Http\\Controllers\\'.$action;
        };

        return array_map($addNamespace, $actions);
    }
1196168
protected function _mapAction($action) {
		$actionMap = $this->getConfig('actionMap');
		if (isset($actionMap[$action]) && method_exists($this, $actionMap[$action]['method'])) {
			$pass = (array)$this->_getRequest()->param('pass');
			call_user_func_array([$this, $actionMap[$action]['method']], $pass);

			if ($this->_redirectResponse instanceof Response) {
				return $this->_redirectResponse;
			}

			if (is_string($actionMap[$action]['view'])) {
				try {
					return $this->getController()->render($this->getController()->request->param('action'));
				} catch (MissingTemplateException $e) {
					return $this->getController()->render($actionMap[$action]['view']);
				}
			}

				return $this->_getResponse();
			}

		return false;
	}
905170
protected function addGroupNamespaceToStringUses($action)
    {
        $groupStack = last($this->router->getGroupStack());

        if (isset($groupStack['namespace']) && strpos($action, '\\') !== 0) {
            return $groupStack['namespace'].'\\'.$action;
        }

        return $action;
    }
276655
protected function parseAsListActions(array $actions)
    {
        $listActions = [];
        foreach ($actions as $ident => $action) {
            $ident  = $this->parseActionIdent($ident, $action);
            $action = $this->parseActionItem($action, $ident, true);

            if (!isset($action['priority'])) {
                $action['priority'] = $this->actionsPriority++;
            }

            if ($action['ident'] === 'create') {
                $action['empty'] = true;

                if (!$this->isObjCreatable()) {
                    $action['active'] = false;
                }
            } else {
                $action['empty'] = (isset($action['empty']) ? boolval($action['empty']) : false);
            }

            if (is_array($action['actions'])) {
                $action['actions']    = $this->parseAsListActions($action['actions']);
                $action['hasActions'] = !!array_filter($action['actions'], function ($action) {
                    return $action['active'];
                });
            }

            if (isset($listActions[$ident])) {
                $hasPriority = ($action['priority'] > $listActions[$ident]['priority']);
                if ($hasPriority || $action['isSubmittable']) {
                    $listActions[$ident] = array_replace($listActions[$ident], $action);
                } else {
                    $listActions[$ident] = array_replace($action, $listActions[$ident]);
                }
            } else {
                $listActions[$ident] = $action;
            }
        }

        usort($listActions, [ $this, 'sortActionsByPriority' ]);

        while (($first = reset($listActions)) && $first['isSeparator']) {
            array_shift($listActions);
        }

        while (($last = end($listActions)) && $last['isSeparator']) {
            array_pop($listActions);
        }

        return $listActions;
    }