htmlburger /
wpemerge
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @package WPEmerge |
||
| 4 | * @author Atanas Angelov <[email protected]> |
||
| 5 | * @copyright 2017-2019 Atanas Angelov |
||
| 6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
||
| 7 | * @link https://wpemerge.com/ |
||
| 8 | */ |
||
| 9 | |||
| 10 | namespace WPEmerge\Routing; |
||
| 11 | |||
| 12 | use Closure; |
||
| 13 | use WPEmerge\Helpers\HasAttributesTrait; |
||
| 14 | use WPEmerge\Routing\Conditions\ConditionInterface; |
||
| 15 | use WPEmerge\View\ViewService; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Provide a fluent interface for registering routes with the router. |
||
| 19 | */ |
||
| 20 | class RouteBlueprint { |
||
| 21 | use HasAttributesTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Router. |
||
| 25 | * |
||
| 26 | * @var Router |
||
| 27 | */ |
||
| 28 | protected $router = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * View service. |
||
| 32 | * |
||
| 33 | * @var ViewService |
||
| 34 | */ |
||
| 35 | protected $view_service = null; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Allowed WordPress conditional tags |
||
| 39 | * |
||
| 40 | * @var string[] |
||
| 41 | */ |
||
| 42 | protected array $allowedConditionals = [ |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 43 | 'is_404', |
||
| 44 | 'is_archive', |
||
| 45 | 'is_attachment', |
||
| 46 | 'is_author', |
||
| 47 | 'is_category', |
||
| 48 | 'is_date', |
||
| 49 | 'is_day', |
||
| 50 | 'is_front_page', |
||
| 51 | 'is_home', |
||
| 52 | 'is_month', |
||
| 53 | 'is_page', |
||
| 54 | 'is_page_template', |
||
| 55 | 1 | 'is_paged', |
|
| 56 | 1 | 'is_post_type_archive', |
|
| 57 | 1 | 'is_privacy_policy', |
|
| 58 | 1 | 'is_search', |
|
| 59 | 'is_single', |
||
| 60 | 'is_singular', |
||
| 61 | 1 | 'is_sticky', |
|
| 62 | 'is_tag', |
||
| 63 | 'is_tax', |
||
| 64 | 'is_time', |
||
| 65 | 'is_year', |
||
| 66 | ]; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Constructor. |
||
| 70 | * |
||
| 71 | 1 | * @codeCoverageIgnore |
|
| 72 | 1 | * @param Router $router |
|
| 73 | * @param ViewService $view_service |
||
| 74 | */ |
||
| 75 | public function __construct( Router $router, ViewService $view_service ) { |
||
| 76 | $this->router = $router; |
||
| 77 | $this->view_service = $view_service; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Call WordPress conditional tag |
||
| 82 | 3 | * |
|
| 83 | 3 | * @param string $name |
|
| 84 | 3 | * @param array $arguments |
|
| 85 | * @return static $this |
||
| 86 | */ |
||
| 87 | 3 | public function __call( $name, $arguments ) { |
|
| 88 | 3 | if ( in_array( $name, $this->getAllowedConditionals(), true ) ) { |
|
| 89 | return $this->get()->where( $name, ...$arguments ); |
||
|
0 ignored issues
–
show
|
|||
| 90 | } |
||
| 91 | |||
| 92 | 3 | throw new \InvalidArgumentException( sprintf( 'Method `%s()` is not allowed WordPress conditional tag', $name ) ); |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get list of allowed conditional tags |
||
| 97 | * |
||
| 98 | * @return string[] |
||
| 99 | */ |
||
| 100 | public function getAllowedConditionals() { |
||
| 101 | 1 | return $this->allowedConditionals; |
|
| 102 | 1 | } |
|
| 103 | 1 | ||
| 104 | 1 | /** |
|
| 105 | * Handle ajax requests |
||
| 106 | * |
||
| 107 | 1 | * @param string $action |
|
| 108 | * @param boolean $private |
||
| 109 | * @param boolean $public |
||
| 110 | * @return static $this |
||
| 111 | */ |
||
| 112 | public function ajax( $action, $private = true, $public = false ) |
||
| 113 | { |
||
|
0 ignored issues
–
show
|
|||
| 114 | return $this->where( 'ajax', $action, $private, $public ); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | 1 | * Match requests using one of the specified methods. |
|
| 119 | 1 | * |
|
| 120 | 1 | * @param string[] $methods |
|
| 121 | * @return static $this |
||
| 122 | */ |
||
| 123 | public function methods( $methods ) { |
||
| 124 | 1 | $methods = $this->router->mergeMethodsAttribute( |
|
| 125 | (array) $this->getAttribute( 'methods', [] ), |
||
| 126 | (array) $methods |
||
| 127 | ); |
||
| 128 | |||
| 129 | return $this->attribute( 'methods', $methods ); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | 1 | * Set the condition attribute to a URL. |
|
| 134 | 1 | * |
|
| 135 | 1 | * @param string $url |
|
| 136 | * @param array<string, string> $where |
||
| 137 | * @return static $this |
||
| 138 | */ |
||
| 139 | 1 | public function url( $url, $where = [] ) { |
|
| 140 | return $this->where( 'url', $url, $where ); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set the condition attribute. |
||
| 145 | * |
||
| 146 | * @param string|array|ConditionInterface $condition |
||
| 147 | * @param mixed ,...$arguments |
||
| 148 | 1 | * @return static $this |
|
| 149 | 1 | */ |
|
| 150 | public function where( $condition ) { |
||
| 151 | if ( ! $condition instanceof ConditionInterface ) { |
||
| 152 | $condition = func_get_args(); |
||
|
0 ignored issues
–
show
|
|||
| 153 | } |
||
| 154 | |||
| 155 | $condition = $this->router->mergeConditionAttribute( |
||
| 156 | $this->getAttribute( 'condition', null ), |
||
| 157 | $condition |
||
| 158 | 1 | ); |
|
| 159 | 1 | ||
| 160 | 1 | return $this->attribute( 'condition', $condition ); |
|
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set the middleware attribute. |
||
| 165 | * |
||
| 166 | * @param string|string[] $middleware |
||
| 167 | * @return static $this |
||
| 168 | 4 | */ |
|
| 169 | 4 | public function middleware( $middleware ) { |
|
| 170 | 1 | $middleware = $this->router->mergeMiddlewareAttribute( |
|
| 171 | (array) $this->getAttribute( 'middleware', [] ), |
||
| 172 | (array) $middleware |
||
| 173 | 4 | ); |
|
| 174 | |||
| 175 | 4 | return $this->attribute( 'middleware', $middleware ); |
|
| 176 | } |
||
| 177 | 4 | ||
| 178 | 4 | /** |
|
| 179 | * Set the namespace attribute. |
||
| 180 | * This should be renamed to namespace for consistency once minimum PHP |
||
| 181 | 4 | * version is increased to 7+. |
|
| 182 | 4 | * |
|
| 183 | * @param string $namespace |
||
| 184 | * @return static $this |
||
| 185 | */ |
||
| 186 | public function setNamespace( $namespace ) { |
||
| 187 | $namespace = $this->router->mergeNamespaceAttribute( |
||
| 188 | $this->getAttribute( 'namespace', '' ), |
||
| 189 | $namespace |
||
| 190 | 1 | ); |
|
| 191 | |||
| 192 | 1 | return $this->attribute( 'namespace', $namespace ); |
|
| 193 | 1 | } |
|
| 194 | 1 | ||
| 195 | /** |
||
| 196 | * Set the query attribute. |
||
| 197 | * |
||
| 198 | * @param callable $query |
||
| 199 | * @return static $this |
||
| 200 | */ |
||
| 201 | public function query( $query ) { |
||
| 202 | 1 | $query = $this->router->mergeQueryAttribute( |
|
| 203 | 1 | $this->getAttribute( 'query', null ), |
|
| 204 | 1 | $query |
|
| 205 | ); |
||
| 206 | |||
| 207 | return $this->attribute( 'query', $query ); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | 1 | * Set the name attribute. |
|
| 212 | 1 | * |
|
| 213 | * @param string $name |
||
| 214 | * @return static $this |
||
| 215 | */ |
||
| 216 | public function name( $name ) { |
||
| 217 | return $this->attribute( 'name', $name ); |
||
| 218 | } |
||
| 219 | |||
| 220 | 1 | /** |
|
| 221 | 1 | * Create a route group. |
|
| 222 | * |
||
| 223 | * @param Closure|string $routes Closure or path to file. |
||
| 224 | * @return void |
||
| 225 | */ |
||
| 226 | public function group( $routes ) { |
||
| 227 | $this->router->group( $this->getAttributes(), $routes ); |
||
| 228 | } |
||
| 229 | 1 | ||
| 230 | 1 | /** |
|
| 231 | * Create a route. |
||
| 232 | * |
||
| 233 | * @param string|Closure $handler |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | public function handle( $handler = '' ) { |
||
| 237 | if ( ! empty( $handler ) ) { |
||
| 238 | 1 | $this->attribute( 'handler', $handler ); |
|
| 239 | 1 | } |
|
| 240 | |||
| 241 | $route = $this->router->route( $this->getAttributes() ); |
||
| 242 | |||
| 243 | $trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 1 ); |
||
| 244 | |||
| 245 | if ( ! empty( $trace ) && ! empty( $trace[0]['file'] ) ) { |
||
| 246 | $route->attribute( '__definition', $trace[0]['file'] . ':' . $trace[0]['line'] ); |
||
| 247 | 1 | } |
|
| 248 | 1 | ||
| 249 | $this->router->addRoute( $route ); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Handle a request by directly rendering a view. |
||
| 254 | * |
||
| 255 | * @param string|string[] $views |
||
| 256 | 1 | * @return void |
|
| 257 | 1 | */ |
|
| 258 | public function view( $views ) { |
||
| 259 | $this->handle( function () use ( $views ) { |
||
| 260 | return $this->view_service->make( $views ); |
||
| 261 | } ); |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | 1 | * Match ALL requests. |
|
| 266 | 1 | * |
|
| 267 | * @param string|Closure $handler |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | public function all( $handler = '' ) { |
||
| 271 | $this->any()->url( '*' )->handle( $handler ); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Match requests with a method of GET or HEAD. |
||
| 276 | * |
||
| 277 | * @return static $this |
||
| 278 | */ |
||
| 279 | public function get() { |
||
| 280 | return $this->methods( ['GET', 'HEAD'] ); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Match requests with a method of POST. |
||
| 285 | * |
||
| 286 | * @return static $this |
||
| 287 | */ |
||
| 288 | public function post() { |
||
| 289 | return $this->methods( ['POST'] ); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Match requests with a method of PUT. |
||
| 294 | * |
||
| 295 | * @return static $this |
||
| 296 | */ |
||
| 297 | public function put() { |
||
| 298 | return $this->methods( ['PUT'] ); |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Match requests with a method of PATCH. |
||
| 303 | * |
||
| 304 | * @return static $this |
||
| 305 | */ |
||
| 306 | public function patch() { |
||
| 307 | return $this->methods( ['PATCH'] ); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Match requests with a method of DELETE. |
||
| 312 | * |
||
| 313 | * @return static $this |
||
| 314 | */ |
||
| 315 | public function delete() { |
||
| 316 | return $this->methods( ['DELETE'] ); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Match requests with a method of OPTIONS. |
||
| 321 | * |
||
| 322 | * @return static $this |
||
| 323 | */ |
||
| 324 | public function options() { |
||
| 325 | return $this->methods( ['OPTIONS'] ); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Match requests with any method. |
||
| 330 | * |
||
| 331 | * @return static $this |
||
| 332 | */ |
||
| 333 | public function any() { |
||
| 334 | return $this->methods( ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'] ); |
||
| 335 | } |
||
| 336 | } |
||
| 337 |