This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/libraries/Router.php

133 lines
2.8 KiB
PHP
Raw Normal View History

<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the router class.
*
* @package Sakura
*/
namespace Sakura;
2016-01-30 00:18:23 +00:00
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
/**
2016-02-02 21:04:15 +00:00
* Sakura Wrapper for Phroute.
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class Router
{
2016-02-02 21:04:15 +00:00
/**
* Container for RouteCollector
*
* @var RouteCollector
*/
2016-01-30 00:18:23 +00:00
protected static $router = null;
2016-02-02 21:04:15 +00:00
/**
* Base path of the router.
*
* @var string
*/
2016-01-30 00:18:23 +00:00
protected static $basePath = null;
2016-02-02 21:04:15 +00:00
/**
* Container for the Dispatcher
*
* @var Dispatcher
*/
2016-01-30 00:18:23 +00:00
protected static $dispatcher = null;
2016-02-02 21:04:15 +00:00
/**
* Collection of handled HTTP request types.
*
* @var array
*/
2016-01-30 00:18:23 +00:00
protected static $methods = [
'GET',
'POST',
'PUT',
'PATCH',
'DELETE',
'HEAD',
'ANY'
];
2016-02-02 21:04:15 +00:00
/**
* Method aliases for adding routes.
*
* @param string $name A HTTP method.
* @param array $args The arguments.
*/
2016-01-30 00:18:23 +00:00
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
}
}
2016-02-02 21:04:15 +00:00
/**
* Initialisation.
*
* @param string $basePath The base path of the router.
*/
2016-01-30 00:18:23 +00:00
public static function init($basePath = '/')
{
// Set base path
self::setBasePath($basePath);
// Create router
self::$router = new RouteCollector;
}
2016-02-02 21:04:15 +00:00
/**
* Set the base path.
*
* @param string $basePath The base path of the router.
*/
2016-01-30 00:18:23 +00:00
public static function setBasePath($basePath)
{
self::$basePath = $basePath;
}
2016-02-02 21:04:15 +00:00
/**
* Parse a URL.
*
* @param string $url The URL that is to be parsed.
*
* @return string THe parsed URL.
*/
2016-01-30 00:18:23 +00:00
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
}
2016-02-02 21:04:15 +00:00
/**
* Handle requests.
*
* @param string $method The HTTP method used to make the request.
* @param string $url The URL the request is made to.
*
* @return mixed The response.
*/
2016-01-30 00:18:23 +00:00
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);
}
}