Recursive function to obtain associations of a table

Can you please guide me to make a recursive funtion to obtain associations of a table?
Right now this is my code to obtain that information:

$actionsAssocArray = [];
$actionView = [];
$actionsAssocAssocArray = [];
$actionViewAssoc = [];
$actionViewAssocAssoc = [];
$actionsAssocAssocAssocArray = [];
try{
	$table = TableRegistry::get($this->getName());
	$associations = $table->associations();
	foreach($associations as $association){
		$acosAssoc = $this->MenuGroups->Acos->find('all')->select(['id', 'alias'])->where(['alias' => $association->getName()])->first();
		if(!empty($acosAssoc->id)){
			if($association instanceof BelongsTo){
				$checkV1 = $this->Acl->check($profile, $acosAssoc->alias.'/view');
				$checkV2 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'create');
				$checkV3 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'read');
				$checkV4 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'update');
				$checkV5 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'delete');
				if(!empty($checkV1) || !empty($checkV2) || !empty($checkV3) || !empty($checkV4) || !empty($checkV5)) 
					$actionView[$association->getName()] = ['text' => 'Ver', 'action' => 'view'];
			}else{
				$actionsAssoc = $this->MenuGroups->Acos->find('translations')->where(['Acos.parent_id' => $acosAssoc->id, 'Acos.alias !=' => 'index', 'Acos.alias not like' => 'add%'])->all();
				foreach($actionsAssoc as $action){
					$checkAs1 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias);
					$checkAs2 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'create');
					$checkAs3 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'read');
					$checkAs4 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'update');
					$checkAs5 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'delete');
					if(!empty($checkAs1) || !empty($checkAs2) || !empty($checkAs3) || !empty($checkAs4) || !empty($checkAs5)){
						$text = $action->_translations['es']->alias;
						$actionsAssocArray[$association->getName()][] = ['text' => $text, 'action' => $action->alias];
					}
				}
			}
		}
		try{
			$tableAssoc = TableRegistry::get($association->getName());
			$associationsAssoc = $tableAssoc->associations();
			foreach($associationsAssoc as $associationAssoc){
				$acosAssoc = $this->MenuGroups->Acos->find('all')->select(['id', 'alias'])->where(['alias' => $associationAssoc->getName()])->first();
				if(!empty($acosAssoc->id)){
					if($associationAssoc instanceof BelongsTo){
						$checkV1 = $this->Acl->check($profile, $acosAssoc->alias.'/view');
						$checkV2 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'create');
						$checkV3 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'read');
						$checkV4 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'update');
						$checkV5 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'delete');
						if(!empty($checkV1) || !empty($checkV2) || !empty($checkV3) || !empty($checkV4) || !empty($checkV5)) 
							$actionViewAssoc[$associationAssoc->getName()] = ['text' => 'Ver', 'action' => 'view'];
					}else{
						$actionsAssoc = $this->MenuGroups->Acos->find('translations')->where(['Acos.parent_id' => $acosAssoc->id, 'Acos.alias not in' => ['add', 'index']])->all();
						foreach($actionsAssoc as $action){
							$checkAs1 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias);
							$checkAs2 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'create');
							$checkAs3 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'read');
							$checkAs4 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'update');
							$checkAs5 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'delete');
							if(!empty($checkAs1) || !empty($checkAs2) || !empty($checkAs3) || !empty($checkAs4) || !empty($checkAs5)){
								$text = $action->_translations['es']->alias;
								$actionsAssocAssocArray[$associationAssoc->getName()][] = ['text' => $text, 'action' => $action->alias];
							}
						}
					}
				}
				try{
					$tableAssocAssoc = TableRegistry::get($associationAssoc->getName());
					$associationsAssocAssoc = $tableAssocAssoc->associations();
					foreach($associationsAssocAssoc as $associationAssocAssoc){
						$acosAssocAssoc = $this->MenuGroups->Acos->find('all')->select(['id', 'alias'])->where(['alias' => $associationAssocAssoc->getName()])->first();
						if(!empty($acosAssocAssoc->id)){
							if($associationAssocAssoc instanceof BelongsTo){
								$checkV1 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/view');
								$checkV2 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/view', 'create');
								$checkV3 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/view', 'read');
								$checkV4 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/view', 'update');
								$checkV5 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/view', 'delete');
								if(!empty($checkV1) || !empty($checkV2) || !empty($checkV3) || !empty($checkV4) || !empty($checkV5)) 
									$actionViewAssocAssoc[$associationAssocAssoc->getName()] = ['text' => 'Ver', 'action' => 'view'];
							}else{
								$actionsAssocAssoc = $this->MenuGroups->Acos->find('translations')->where(['Acos.parent_id' => $acosAssocAssoc->id, 'Acos.alias not in' => ['add', 'index']])->all();
								foreach($actionsAssocAssoc as $action){
									$checkAs1 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/'.$action->alias);
									$checkAs2 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/'.$action->alias, 'create');
									$checkAs3 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/'.$action->alias, 'read');
									$checkAs4 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/'.$action->alias, 'update');
									$checkAs5 = $this->Acl->check($profile, $acosAssocAssoc->alias.'/'.$action->alias, 'delete');
									if(!empty($checkAs1) || !empty($checkAs2) || !empty($checkAs3) || !empty($checkAs4) || !empty($checkAs5)){
										$text = $action->_translations['es']->alias;
										$actionsAssocAssocAssocArray[$associationAssocAssoc->getName()][] = ['text' => $text, 'action' => $action->alias];
									}
								}
							}
						}
					}
				}catch(MissingTableClassException $exception){}
			}
		}catch(MissingTableClassException $exception){}
	}
}catch(MissingTableClassException $exception){}
$i = count($actionsAssocArray);
if(!empty($i)) $this->set(compact('actionsAssocArray'));
$i = count($actionView);
if(!empty($i)) $this->set(compact('actionView'));
$i = count($actionsAssocAssocArray);
if(!empty($i)) $this->set(compact('actionsAssocAssocArray'));
$i = count($actionViewAssoc);
if(!empty($i)) $this->set(compact('actionViewAssoc'));
$i = count($actionsAssocAssocAssocArray);
if(!empty($i)) $this->set(compact('actionsAssocAssocAssocArray'));
$i = count($actionViewAssocAssoc);
if(!empty($i)) $this->set(compact('actionViewAssocAssoc'));

As you can see is very long but I know that with recursion I can shorten it, but don’t know where to make the recursive call to the function.
This is the code for the recursive function but is not working:

public function getAssociations($name, $profile)
{
	$actionsAssocArray = [];
	$actionView = [];
	try{
		$table = TableRegistry::get($name);
		$associations = $table->associations();
		foreach($associations as $association){
			$tableAssoc = TableRegistry::get($association->getName());
			$names = $tableAssoc->associations()->keys();
			if(count($names)>0) $this->getAssociations($association->getName(), $profile);
			$acosAssoc = $this->MenuGroups->Acos->find('all')->select(['id', 'alias'])->where(['alias' => $association->getName()])->first();
			if(!empty($acosAssoc->id)){
				if($association instanceof BelongsTo){
					$checkV1 = $this->Acl->check($profile, $acosAssoc->alias.'/view');
					$checkV2 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'create');
					$checkV3 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'read');
					$checkV4 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'update');
					$checkV5 = $this->Acl->check($profile, $acosAssoc->alias.'/view', 'delete');
					if(!empty($checkV1) || !empty($checkV2) || !empty($checkV3) || !empty($checkV4) || !empty($checkV5)) 
						$actionView[$association->getName()] = ['text' => 'Ver', 'action' => 'view'];
				}else{
					$actionsAssoc = $this->MenuGroups->Acos->find('translations')->where(['Acos.parent_id' => $acosAssoc->id, 'Acos.alias !=' => 'index', 'Acos.alias not like' => 'add%'])->all();
					foreach($actionsAssoc as $action){
						$checkAs1 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias);
						$checkAs2 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'create');
						$checkAs3 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'read');
						$checkAs4 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'update');
						$checkAs5 = $this->Acl->check($profile, $acosAssoc->alias.'/'.$action->alias, 'delete');
						if(!empty($checkAs1) || !empty($checkAs2) || !empty($checkAs3) || !empty($checkAs4) || !empty($checkAs5)){
							$text = $action->_translations['es']->alias;
							$actionsAssocArray[$association->getName()][] = ['text' => $text, 'action' => $action->alias];
						}
					}
				}
			}
		}
	}catch(MissingTableClassException $exception){}
	$i = count($actionsAssocArray);
	if(!empty($i)) $this->set(compact('actionsAssocArray'));
	$i = count($actionView);
	if(!empty($i)) $this->set(compact('actionView'));
}

How can I solve this issue?

This looks to be a reasonable tutorial explaining the principle:

Even before you make this recursive you could do some simple decomposition to simplify things. Making a method like this:

    protected function checkALotOfStuff($aro, $aco)
    {
        $checkV1 = !empty($this->Acl->check($aro, $aco));
        $checkV2 = !empty($this->Acl->check($aro, $aco, 'create'));
        $checkV3 = !empty($this->Acl->check($aro, $aco, 'read'));
        $checkV4 = !empty($this->Acl->check($aro, $aco, 'update'));
        $checkV5 = !empty($this->Acl->check($aro, $aco, 'delete'));

        return $checkV1 || $checkV2 || $checkV3 || $checkV4 || $checkV5;
    }

would then let you call it:

if ($this->checkALotOfStuff($profile, $acosAssoc->alias.'/view') {
  // do things on true
}

//and

if ($this->checkALotOfStuff($profile, $acosAssoc->alias.'/'.$action->alias') {
  // do things on true
}

Preliminary refactoring like this can make it much easier to see how to make recursion work too.

This book changed my life: