Last active
July 5, 2017 04:01
-
-
Save makoru-hikage/803777bd0a42d9d398c92d96badc439b to your computer and use it in GitHub Desktop.
Recursion...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Accept an array of instances of | |
* MenuService. Each will process their | |
* input data then return their output | |
* data so the next instance can use | |
* the output data as their input data. | |
* When there is no instance left in | |
* the array, the last one shall be returned | |
* | |
* @return self | |
*/ | |
public function prepend(){ | |
$remainingMenuSvcs = func_get_args(); | |
$currentMenuSvc = array_shift($remainingMenuSvcs); | |
if ($this->mainInstance === null) { | |
$this->mainInstance = $this; | |
$remainingMenuSvcs[] = $this->mainInstance; | |
} | |
if (count($remainingMenuSvcs) > 0) { | |
$nextMenuSvc = $remainingMenuSvcs[0]; | |
$msData = $currentMenuSvc->execute()->prepareResponse(); | |
$nextMenuSvc->setCode($msData['code']); | |
$nextMenuSvc->setInputData($msData['data']); | |
$nextMenuSvc->setMainInstance($this->mainInstance); | |
//Calls prepend again... | |
return call_user_func_array([$nextMenuSvc, 'prepend'], $remainingMenuSvcs); | |
} | |
return $this; | |
} |
call_user_func_array
but why
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
setMainInstance
was solely made for this function.