@ 2017-07-13T10:10:52Z <?php
class Request {
private $method;
private $path;
function __construct($method, $path) {
$this->method = $method;
$this->path = $path;
}
function getMethod() {
return $this->method;
}
function getPath() {
return $this->path;
}
}
class Router {
private $routes = [
'get' => [],
'post' => []
];
function get($pattern, callable $handler) {
$this->routes['get'][$pattern] = $handler;
return $this;
}
function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}
function match(Request $request) {
$method = strtolower($request->getMethod());
if (!isset($this->routes[$method])) {
return false;
}
$path = $request->getPath();
foreach ($this->routes[$method] as $pattern => $handler) {
if ($pattern === $path) {
return $handler;
}
}
return false;
}
}
class Dispatcher {
private $router;
function __construct(Router $router) {
$this->router = $router;
}
function handle(Request $request) {
$handler = $this->router->match($request);
if (!$handler) {
echo "Could not find your resource!\n";
return;
}
$handler();
}
}
$router = new Router();
$router->get('foo', function() { echo "GET foo\n"; });
$router->post('bar', function() { echo "POST bar\n"; });
$router->post('toto',function() {écho 'POST toto is thé method';}
$dispatcher = new Dispatcher($router);
$dispatcher->handle(new Request('GET', 'foo'));
$dispatcher->handle(new Request('POST', 'bar'));
$dispatcher->handle(new Request('GET', 'qux'));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.1.7 0.052 0.011 15.02 7.1.6 0.052 0.014 33.03 7.1.5 0.079 0.017 32.86 7.1.4 0.062 0.013 32.46 7.1.3 0.065 0.011 32.54 7.1.2 0.058 0.014 32.67 7.1.1 0.035 0.012 14.54 7.1.0 0.035 0.011 14.62 7.0.20 0.044 0.011 14.61 7.0.19 0.057 0.009 14.66 7.0.18 0.043 0.014 14.17 7.0.17 0.048 0.010 14.18 7.0.16 0.043 0.009 14.24 7.0.15 0.039 0.013 14.16 7.0.14 0.041 0.012 14.37 7.0.13 0.032 0.010 14.58 7.0.12 0.033 0.013 14.49 7.0.11 0.032 0.012 14.39 7.0.10 0.040 0.008 14.37 7.0.9 0.028 0.010 14.27 7.0.8 0.034 0.007 14.33 7.0.7 0.034 0.012 14.25 7.0.6 0.041 0.013 14.14 7.0.5 0.035 0.011 14.34 7.0.4 0.033 0.010 14.51 7.0.3 0.036 0.011 14.50 7.0.2 0.028 0.012 14.52 7.0.1 0.034 0.011 14.50 7.0.0 0.028 0.011 14.42
preferences:dark mode live preview
140.42 ms | 1394 KiB | 7 Q