Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
153 lines (137 sloc) 3.67 KB
<?php
/**
* @package WPEmerge
* @author Atanas Angelov <atanas.angelov.dev@gmail.com>
* @copyright 2018 Atanas Angelov
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
* @link https://wpemerge.com/
*/
namespace WPEmerge\Routing;
use Closure;
use WPEmerge\Controllers\WordPressController;
/**
* Allow objects to have routes
*/
trait HasRoutesTrait {
/**
* Array of registered routes
*
* @var RouteInterface[]
*/
protected $routes = [];
/**
* Get registered routes
*
* @return RouteInterface[]
*/
public function getRoutes() {
return $this->routes;
}
/**
* Add a route
*
* @param RouteInterface $route
* @return RouteInterface
*/
public function addRoute( $route ) {
$this->routes[] = $route;
return $route;
}
/**
* Create and add a new route
*
* @param string[] $methods
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
* @throws \WPEmerge\Exceptions\Exception
*/
public function route( $methods, $condition, $handler = null ) {
if ( $handler === null ) {
$handler = WordPressController::class . '@handle';
}
$route = new Route( $methods, $condition, $handler );
return $this->addRoute( $route );
}
/**
* Create and add a route group
*
* @param string|Closure|\WPEmerge\Routing\Conditions\ConditionInterface $condition
* @param Closure|null $routes
* @return RouteInterface
*/
public function group( $condition, $routes = null ) {
$routeGroup = new RouteGroup( $condition, $routes );
return $this->addRoute( $routeGroup );
}
/**
* Create and add a route for the GET and HEAD methods
*
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
*/
public function get( $condition, $handler = null ) {
return $this->route( ['GET', 'HEAD'], $condition, $handler );
}
/**
* Create and add a route for the POST method
*
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
*/
public function post( $condition, $handler = null ) {
return $this->route( ['POST'], $condition, $handler );
}
/**
* Create and add a route for the PUT method
*
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
*/
public function put( $condition, $handler = null ) {
return $this->route( ['PUT'], $condition, $handler );
}
/**
* Create and add a route for the PATCH method
*
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
*/
public function patch( $condition, $handler = null ) {
return $this->route( ['PATCH'], $condition, $handler );
}
/**
* Create and add a route for the DELETE method
*
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
*/
public function delete( $condition, $handler = null ) {
return $this->route( ['DELETE'], $condition, $handler );
}
/**
* Create and add a route for the OPTIONS method
*
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
*/
public function options( $condition, $handler = null ) {
return $this->route( ['OPTIONS'], $condition, $handler );
}
/**
* Create and add a route for all supported methods
*
* @param mixed $condition
* @param string|Closure|null $handler
* @return RouteInterface
*/
public function any( $condition, $handler = null ) {
return $this->route( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], $condition, $handler );
}
}