Files
simple-php-router/src/Pecee/SimpleRouter/SimpleRouter.php
T
Simon Sessingø 73ee4521bc Bugfixes
- Array arguments are now longer automaticially merged.
- Added domain-route parameter unit-test.
2016-11-17 03:50:33 +01:00

158 lines
4.3 KiB
PHP

<?php
/**
* ---------------------------
* Router helper class
* ---------------------------
* This class is added so calls can be made statically like Router::get() making the code look more pretty.
*/
namespace Pecee\SimpleRouter;
use Pecee\Http\Middleware\BaseCsrfVerifier;
class SimpleRouter {
/**
* Start/route request
* @throws \Pecee\Exception\RouterException
*/
public static function start() {
RouterBase::getInstance()->routeRequest();
}
/**
* Set default namespace for all routes
* @param string $defaultNamespace
*/
public static function setDefaultNamespace($defaultNamespace) {
RouterBase::getInstance()->setDefaultNamespace($defaultNamespace);
}
/**
* Set base csrf verifier
* @param BaseCsrfVerifier $baseCsrfVerifier
*/
public static function csrfVerifier(BaseCsrfVerifier $baseCsrfVerifier) {
RouterBase::getInstance()->setCsrfVerifier($baseCsrfVerifier);
}
public static function addBootManager(RouterBootManager $bootManager) {
RouterBase::getInstance()->addBootManager($bootManager);
}
public static function get($url, $callback, array $settings = null) {
return static::match(['get'], $url, $callback, $settings);
}
public static function post($url, $callback, array $settings = null) {
return static::match(['post'], $url, $callback, $settings);
}
public static function put($url, $callback, array $settings = null) {
return static::match(['put'], $url, $callback, $settings);
}
public static function patch($url, $callback, array $settings = null) {
return static::match(['patch'], $url, $callback, $settings);
}
public static function options($url, $callback, array $settings = null) {
return static::match(['options'], $url, $callback, $settings);
}
public static function delete($url, $callback, array $settings = null) {
return static::match(['delete'], $url, $callback, $settings);
}
public static function group($settings = array(), $callback) {
$group = new RouterGroup();
$group->setCallback($callback);
if($settings !== null && is_array($settings)) {
$group->setData($settings);
}
RouterBase::getInstance()->addRoute($group);
return $group;
}
/**
* Adds get + post route
*
* @param string $url
* @param callable $callback
* @param array|null $settings
* @return RouterRoute
*/
public static function basic($url, $callback, array $settings = null) {
return static::match(['get', 'post'], $url, $callback, $settings);
}
public static function match(array $requestMethods, $url, $callback, array $settings = null) {
$route = new RouterRoute($url, $callback);
$route->setRequestMethods($requestMethods);
if($settings !== null) {
$route->setData($settings);
}
RouterBase::getInstance()->addRoute($route);
return $route;
}
public static function all($url, $callback, array $settings = null) {
$route = new RouterRoute($url, $callback);
if($settings !== null) {
$route->setData($settings);
}
RouterBase::getInstance()->addRoute($route);
return $route;
}
public static function controller($url, $controller, array $settings = null) {
$route = new RouterController($url, $controller);
if($settings !== null) {
$route->setData($settings);
}
RouterBase::getInstance()->addRoute($route);
return $route;
}
public static function resource($url, $controller, array $settings = null) {
$route = new RouterResource($url, $controller);
if($settings !== null) {
$route->setData($settings);
}
static::router()->addRoute($route);
return $route;
}
public static function getRoute($controller = null, $parameters = null, $getParams = null) {
return static::router()->getRoute($controller, $parameters, $getParams);
}
public static function request() {
return static::router()->getRequest();
}
public static function response() {
return static::router()->getResponse();
}
protected static function router() {
return RouterBase::getInstance();
}
}