-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSRouter.php
More file actions
87 lines (78 loc) · 2.85 KB
/
Copy pathSRouter.php
File metadata and controls
87 lines (78 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php declare(strict_types=1);
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017/7/14
* Time: 下午8:03
*/
namespace Inhere\Route;
use Closure;
use Inhere\Route\Dispatcher\DispatcherInterface;
use InvalidArgumentException;
use LogicException;
use function method_exists;
/**
* Class SRoute - this is static class version
* @package Inhere\Route
* @method static get(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static post(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static put(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static delete(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static options(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static head(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static search(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static connect(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static trace(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static any(string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static add(string $method, string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static map(array $methods, string $path, mixed $handler, array $binds = [], array $opts = [])
* @method static group(string $prefix, Closure $callback, array $middleware = [], array $opts = [])
* @method static config(array $config)
* @method static match(string $path, string $method = 'GET')
* @method static dispatch(DispatcherInterface | array $dispatcher, $path = null, $method = null)
*/
final class SRouter
{
/** @var Router|RouterInterface */
private static Router|RouterInterface $router;
/**
* SRouter constructor. disable new class.
*/
private function __construct()
{
}
/**
* Defines a route callback and method
*
* @param string $method
* @param array $args
*
* @return Router|mixed
* @throws InvalidArgumentException
* @throws LogicException
*/
public static function __callStatic($method, array $args)
{
if (method_exists(self::getRouter(), $method)) {
return self::getRouter()->$method(...$args);
}
throw new InvalidArgumentException("call invalid method: $method");
}
/**
* @return Router|RouterInterface
*/
public static function getRouter(): RouterInterface
{
if (!self::$router) {
self::$router = new Router();
}
return self::$router;
}
/**
* @param RouterInterface $router
*/
public static function setRouter(RouterInterface $router): void
{
self::$router = $router;
}
}