@ 2015-12-09T23:00:24Z <?php
class objectDispatch {
public function routeIterator(&$path) {
while (!empty($path)) {
yield end($path);
array_pop($path);
}
}
public function __invoke($context, $root) {
$last = '';
$parent = null;
$current = $root;
$isEndpoint = false;
$path = $context->requestPath;
foreach($this->routeIterator($path) as $chunk) {
if (!is_object($current) && class_exists($current)) {
$current = new $current();
}
if (is_object($current)) {
$parent = $current;
}
if (in_array($chunk, get_class_methods($parent))) {
yield $parent->$chunk($path);
$isEndpoint = true;
break;
} else if (array_key_exists($chunk, get_object_vars($parent))) {
$current = $parent->$chunk;
} else if (method_exists($parent, 'lookup')) {
list($current, $consumed) = $parent->lookup($path);
$chunk = implode('/', $consumed);
$truncateAt = count($consumed);
array_splice($path, $truncateAt, ($truncateAt - count($path)));
} else {
header('Http/1.0 404 Not Found');
exit();
}
$last = $chunk;
}
//No parts remaining
if (!is_object($current) && class_exists($current)) {
$current = new $current();
}
if (!$isEndpoint) {
if (is_object($current)) {
yield $current();
} else if (is_object($parent)) {
yield $parent();
}
}
}
}
class context {
public $requestPath;
public $trailing;
public function __construct($path) {
$this->trailing = false;
$requestPath = explode('/', $path);
if (end($path) == '') {
$this->trailing = (count($path) > 2);
array_pop($path);
}
$path = array_reverse($path);
if (end($path) == '') {
//Eliminate leading slashes
array_pop($path);
}
$this->requestPath = $path;
}
}
class rootController {
public $admin = 'adminController';
public function __invoke($args = []) {
return "hello from root";
}
}
class adminController {
public $user = 'usersController';
public function __invoke($args = []) {
return "hello from admin";
}
}
class usersController {
public function __invoke($args = []) {
return "hello from users";
}
public function lookup($path) {
$current = new userController($path[0]);
return [$current, [$path[0]]];
}
}
class userController {
public $id;
public function __construct($id) {
$this->id = $id;
}
public function __invoke($args = []) {
return "hello from user " . $this->id;
}
}
$context = new context('/admin/user/');
$dispatch = new objectDispatch();
foreach ($dispatch($context, 'rootController') as $dispatchMessage) {
echo $dispatchMessage;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Fatal error: Uncaught TypeError: end(): Argument #1 ($array) must be of type array, string given in /in/YAmBT:61
Stack trace:
#0 /in/YAmBT(61): end('/admin/user/')
#1 /in/YAmBT(111): context->__construct('/admin/user/')
#2 {main}
thrown in /in/YAmBT on line 61
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
51.35 ms | 401 KiB | 8 Q