2014-08-29 11:20:08 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru;
|
|
|
|
|
2014-08-31 16:56:00 +02:00
|
|
|
class Router
|
2014-08-29 11:20:08 +02:00
|
|
|
{
|
|
|
|
private $routes;
|
|
|
|
|
|
|
|
public function get($query, $route)
|
|
|
|
{
|
|
|
|
$this->route('GET', $query, $route);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function put($query, $route)
|
|
|
|
{
|
|
|
|
$this->route('PUT', $query, $route);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($query, $route)
|
|
|
|
{
|
|
|
|
$this->route('DELETE', $query, $route);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function post($query, $route)
|
|
|
|
{
|
|
|
|
$this->route('POST', $query, $route);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function route($method, $query, $route)
|
|
|
|
{
|
2014-09-09 12:34:57 +02:00
|
|
|
$this->routes[$method][] = new Route($query, $route);
|
2014-08-29 11:20:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handle($method, $request)
|
|
|
|
{
|
|
|
|
if (!isset($this->routes[$method]))
|
|
|
|
throw new \DomainException('Unhandled request method: ' . $method);
|
|
|
|
|
|
|
|
foreach ($this->routes[$method] as $route)
|
|
|
|
{
|
2014-08-30 12:52:44 +02:00
|
|
|
if ($route->handle($request, $output))
|
|
|
|
{
|
|
|
|
return $output;
|
2014-09-09 12:34:57 +02:00
|
|
|
}
|
2014-08-29 11:20:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \DomainException('Unhandled request address: ' . $request);
|
|
|
|
}
|
|
|
|
}
|