| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\Framework\Http; |
| 4 |
|
| 5 |
use FluentSupport\Framework\Support\Arr; |
| 6 |
|
| 7 |
class Router |
| 8 |
{ |
| 9 |
protected $app = null; |
| 10 |
|
| 11 |
protected $name = []; |
| 12 |
|
| 13 |
protected $prefix = []; |
| 14 |
|
| 15 |
protected $routes = []; |
| 16 |
|
| 17 |
protected $routeGroups = []; |
| 18 |
|
| 19 |
protected $groupStack = []; |
| 20 |
|
| 21 |
protected $policyHandler = null; |
| 22 |
|
| 23 |
public function __construct($app) |
| 24 |
{ |
| 25 |
$this->app = $app; |
| 26 |
} |
| 27 |
|
| 28 |
public function prefix($prefix) |
| 29 |
{ |
| 30 |
$this->prefix[] = $prefix; |
| 31 |
|
| 32 |
return $this; |
| 33 |
} |
| 34 |
|
| 35 |
public function name($name) |
| 36 |
{ |
| 37 |
$this->name[] = $name; |
| 38 |
|
| 39 |
return $this; |
| 40 |
} |
| 41 |
|
| 42 |
public function group($attributes = [], \Closure $callback = null) |
| 43 |
{ |
| 44 |
if ($attributes instanceof \Closure) { |
| 45 |
$callback = $attributes; |
| 46 |
$attributes = []; |
| 47 |
} |
| 48 |
|
| 49 |
if (isset($attributes['name'])) { |
| 50 |
$this->name($attributes['name']); |
| 51 |
} |
| 52 |
|
| 53 |
if (isset($attributes['prefix'])) { |
| 54 |
$this->prefix($attributes['prefix']); |
| 55 |
} |
| 56 |
|
| 57 |
call_user_func($callback, $this); |
| 58 |
array_pop($this->prefix); |
| 59 |
array_pop($this->name); |
| 60 |
} |
| 61 |
|
| 62 |
public function withPolicy($handler) |
| 63 |
{ |
| 64 |
$this->policyHandler = $handler; |
| 65 |
|
| 66 |
return $this; |
| 67 |
} |
| 68 |
|
| 69 |
public function get($uri, $handler) |
| 70 |
{ |
| 71 |
$this->routes[] = $route = $this->newRoute( |
| 72 |
$uri, $handler, \WP_REST_Server::READABLE |
| 73 |
); |
| 74 |
|
| 75 |
return $route; |
| 76 |
} |
| 77 |
|
| 78 |
public function post($uri, $handler) |
| 79 |
{ |
| 80 |
$this->routes[] = $route = $this->newRoute( |
| 81 |
$uri, $handler, \WP_REST_Server::CREATABLE |
| 82 |
); |
| 83 |
|
| 84 |
return $route; |
| 85 |
} |
| 86 |
|
| 87 |
public function put($uri, $handler) |
| 88 |
{ |
| 89 |
$this->routes[] = $route = $this->newRoute( |
| 90 |
$uri, $handler, \WP_REST_Server::EDITABLE |
| 91 |
); |
| 92 |
|
| 93 |
return $route; |
| 94 |
} |
| 95 |
|
| 96 |
public function patch($uri, $handler) |
| 97 |
{ |
| 98 |
$this->routes[] = $route = $this->newRoute( |
| 99 |
$uri, $handler, \WP_REST_Server::EDITABLE |
| 100 |
); |
| 101 |
|
| 102 |
return $route; |
| 103 |
} |
| 104 |
|
| 105 |
public function delete($uri, $handler) |
| 106 |
{ |
| 107 |
$this->routes[] = $route = $this->newRoute( |
| 108 |
$uri, $handler, \WP_REST_Server::DELETABLE |
| 109 |
); |
| 110 |
|
| 111 |
return $route; |
| 112 |
} |
| 113 |
|
| 114 |
public function any($uri, $handler) |
| 115 |
{ |
| 116 |
$this->routes[] = $route = $this->newRoute( |
| 117 |
$uri, $handler, \WP_REST_Server::ALLMETHODS |
| 118 |
); |
| 119 |
|
| 120 |
return $route; |
| 121 |
} |
| 122 |
|
| 123 |
protected function newRoute($uri, $handler, $method) |
| 124 |
{ |
| 125 |
$route = Route::create( |
| 126 |
$this->app, |
| 127 |
$this->getRestNamespace(), |
| 128 |
$this->buildUriWithPrefix($uri), |
| 129 |
$handler, |
| 130 |
$method, |
| 131 |
implode('', $this->name) |
| 132 |
); |
| 133 |
|
| 134 |
if ($this->policyHandler) { |
| 135 |
$route->withPolicy($this->policyHandler); |
| 136 |
} |
| 137 |
|
| 138 |
return $route; |
| 139 |
} |
| 140 |
|
| 141 |
protected function getRestNamespace() |
| 142 |
{ |
| 143 |
$version = $this->app->config->get('app.rest_version'); |
| 144 |
|
| 145 |
$namespace = trim($this->app->config->get('app.rest_namespace'), '/'); |
| 146 |
|
| 147 |
return "{$namespace}/{$version}"; |
| 148 |
} |
| 149 |
|
| 150 |
protected function buildUriWithPrefix($uri) |
| 151 |
{ |
| 152 |
$uri = trim($uri, '/'); |
| 153 |
|
| 154 |
$prefix = array_map(function($prefix) { |
| 155 |
return trim($prefix, '/'); |
| 156 |
}, $this->prefix); |
| 157 |
|
| 158 |
$prefix = implode('/', $prefix); |
| 159 |
|
| 160 |
return trim($prefix, '/') . '/' . trim($uri, '/'); |
| 161 |
} |
| 162 |
|
| 163 |
public function registerRoutes() |
| 164 |
{ |
| 165 |
foreach ($this->routes as $route) $route->register(); |
| 166 |
} |
| 167 |
|
| 168 |
public function getRoutes() |
| 169 |
{ |
| 170 |
return $this->routes; |
| 171 |
} |
| 172 |
} |
| 173 |
|