@ 2017-07-13T10:14:29Z <?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.015 0.002 16.94 7.1.6 0.012 0.026 35.00 7.1.5 0.061 0.011 32.57 7.1.4 0.023 0.014 34.33 7.1.3 0.018 0.023 34.42 7.1.2 0.019 0.013 34.36 7.1.1 0.013 0.006 16.25 7.1.0 0.013 0.006 16.36 7.0.20 0.015 0.003 16.40 7.0.19 0.000 0.017 14.43 7.0.18 0.009 0.009 15.98 7.0.17 0.004 0.014 16.00 7.0.16 0.004 0.009 16.21 7.0.15 0.037 0.005 14.21 7.0.14 0.011 0.018 14.20 7.0.13 0.004 0.013 16.43 7.0.12 0.000 0.018 16.37 7.0.11 0.006 0.017 15.94 7.0.10 0.007 0.012 16.33 7.0.9 0.006 0.012 16.06 7.0.8 0.006 0.013 16.26 7.0.7 0.039 0.014 14.38 7.0.6 0.010 0.010 16.03 7.0.5 0.004 0.014 16.18 7.0.4 0.004 0.013 16.28 7.0.3 0.034 0.013 14.46 7.0.2 0.032 0.007 14.52 7.0.1 0.034 0.016 16.53 7.0.0 0.006 0.012 16.19
preferences:dark mode live preview
145.73 ms | 1398 KiB | 7 Q