2015-12-06 01:52:35 +00:00
|
|
|
<?php
|
|
|
|
/*
|
2016-01-30 00:18:23 +00:00
|
|
|
* Router Wrapper
|
2015-12-06 01:52:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
2016-01-30 00:18:23 +00:00
|
|
|
use Phroute\Phroute\RouteCollector;
|
|
|
|
use Phroute\Phroute\Dispatcher;
|
|
|
|
|
2015-12-06 01:52:35 +00:00
|
|
|
/**
|
|
|
|
* Class Router
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
class Router
|
|
|
|
{
|
2016-01-30 00:18:23 +00:00
|
|
|
// Router container
|
|
|
|
protected static $router = null;
|
|
|
|
|
2016-01-30 13:25:18 +00:00
|
|
|
// Base path (unused for now)
|
2016-01-30 00:18:23 +00:00
|
|
|
protected static $basePath = null;
|
|
|
|
|
|
|
|
// Dispatcher
|
|
|
|
protected static $dispatcher = null;
|
|
|
|
|
|
|
|
// Request methods
|
|
|
|
protected static $methods = [
|
|
|
|
'GET',
|
|
|
|
'POST',
|
|
|
|
'PUT',
|
|
|
|
'PATCH',
|
|
|
|
'DELETE',
|
|
|
|
'HEAD',
|
|
|
|
'ANY'
|
|
|
|
];
|
|
|
|
|
|
|
|
// Add a handler
|
|
|
|
public static function __callStatic($name, $args)
|
|
|
|
{
|
|
|
|
// Check if the method exists
|
|
|
|
if (in_array($name = strtoupper($name), self::$methods)) {
|
|
|
|
$path = isset($args[2]) && $args !== null ? [$args[0], $args[2]] : $args[0];
|
2016-01-30 13:25:18 +00:00
|
|
|
$handler = is_callable($args[1]) || is_array($args[1]) ? $args[1] : explode('@', $args[1]);
|
2016-01-30 00:18:23 +00:00
|
|
|
$filter = isset($args[3]) ? $args[3] : [];
|
|
|
|
|
2016-01-30 13:25:18 +00:00
|
|
|
self::$router->addRoute($name, $path, $handler, $filter);
|
2016-01-30 00:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialisation function
|
|
|
|
public static function init($basePath = '/')
|
|
|
|
{
|
|
|
|
// Set base path
|
|
|
|
self::setBasePath($basePath);
|
|
|
|
|
|
|
|
// Create router
|
|
|
|
self::$router = new RouteCollector;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set base path
|
|
|
|
public static function setBasePath($basePath)
|
|
|
|
{
|
|
|
|
self::$basePath = $basePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the url
|
|
|
|
private static function parseUrl($url)
|
|
|
|
{
|
2016-01-30 13:25:18 +00:00
|
|
|
return parse_url($url, PHP_URL_PATH);
|
2016-01-30 00:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle requests
|
|
|
|
public static function handle($method, $url)
|
|
|
|
{
|
|
|
|
// Check if the dispatcher is defined
|
|
|
|
if (self::$dispatcher === null) {
|
|
|
|
self::$dispatcher = new Dispatcher(self::$router->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse url
|
|
|
|
$url = self::parseUrl($url);
|
|
|
|
|
|
|
|
// Handle the request
|
|
|
|
return self::$dispatcher->dispatch($method, $url);
|
|
|
|
}
|
2015-12-06 01:52:35 +00:00
|
|
|
}
|