Skip to content

Instantly share code, notes, and snippets.

@diloabininyeri
Last active November 7, 2018 12:24
Show Gist options
  • Save diloabininyeri/eef9076f7f8855cc62d17cfa790c25c0 to your computer and use it in GitHub Desktop.
Save diloabininyeri/eef9076f7f8855cc62d17cfa790c25c0 to your computer and use it in GitHub Desktop.
<?php
/**
* @author dılo sürücü <[email protected]>
* php pure from structure basic middleware example
*
*/
Class MiddlewareFirst
{
/**
* @param Closure $next
* @param stdClass $request
* @return mixed
*
*/
function handle(Closure $next, stdClass $request)
{
return $next($request);
}
}
class MiddlewareSecond
{
/**
* @param $next
* @param $request
* @return mixed
*
*/
function handle(Closure $next, stdClass $request)
{
return $next($request);
}
}
class MyController
{
function getRequestId(stdClass $request)
{
echo $request->id;
}
}
$middleWares = ["MiddlewareFirst", "MiddlewareSecond"];
$request = new stdClass();
$request->id = 23;
$countMiddleWare = count($middleWares);
for ($a = 0; $a < $countMiddleWare; $a++) {
call_user_func_array([new $middleWares[$a], "handle"], [function ($request) use ($middleWares, $a, $countMiddleWare) {
if ($middleWares[$a] == $middleWares[$countMiddleWare - 1]) {
return (new MyController())->getRequestId($request);
}
}, $request]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment