Skip to content

Instantly share code, notes, and snippets.

@makoru-hikage
Last active July 5, 2017 04:01
Show Gist options
  • Save makoru-hikage/803777bd0a42d9d398c92d96badc439b to your computer and use it in GitHub Desktop.
Save makoru-hikage/803777bd0a42d9d398c92d96badc439b to your computer and use it in GitHub Desktop.
Recursion...
<?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;
}
@makoru-hikage
Copy link
Author

setMainInstance was solely made for this function.

@TheoryOfNekomata
Copy link

call_user_func_array

but why

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment