Router.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Routes; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | |
| 7 | use function is_callable; |
| 8 | use function str_contains; |
| 9 | |
| 10 | class Router |
| 11 | { |
| 12 | /** |
| 13 | * @since 3.0.0 |
| 14 | * @param string $uri |
| 15 | * @param string|callable $action |
| 16 | * @param string $method |
| 17 | * |
| 18 | * @return void |
| 19 | */ |
| 20 | public function get(string $uri, $action, $method = '__invoke') |
| 21 | { |
| 22 | $this->addRoute('GET', $method, $uri, $action); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @since 3.0.0 |
| 27 | * @param string $uri |
| 28 | * @param string|callable $action |
| 29 | * @param string $method |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function post(string $uri, $action, $method = '__invoke') |
| 34 | { |
| 35 | $this->addRoute('POST', $method, $uri, $action); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @since 3.0.0 |
| 40 | */ |
| 41 | protected function isRouteValid(string $route): bool |
| 42 | { |
| 43 | return isset($_GET['givewp-route']) && $_GET['givewp-route'] === $route; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @since 3.0.0 |
| 48 | */ |
| 49 | protected function getRequestDataByType(string $type): array |
| 50 | { |
| 51 | if ($type === 'POST'){ |
| 52 | return $this->getDataFromPostRequest(); |
| 53 | } |
| 54 | |
| 55 | return $this->getDataFromGetRequest(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @since 3.0.0 |
| 60 | */ |
| 61 | protected function getDataFromPostRequest(): array |
| 62 | { |
| 63 | $requestData = []; |
| 64 | |
| 65 | if (!isset($_SERVER['CONTENT_TYPE'])) { |
| 66 | return $requestData; |
| 67 | } |
| 68 | |
| 69 | if (str_contains($_SERVER['CONTENT_TYPE'], "application/json")) { |
| 70 | $requestData = file_get_contents('php://input'); |
| 71 | $requestData = json_decode($requestData, true); |
| 72 | $requestData = give_clean($requestData); |
| 73 | } else { |
| 74 | $requestData = array_merge( |
| 75 | give_clean($_REQUEST), |
| 76 | give_clean($_FILES) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | return $requestData; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @since 3.0.0 |
| 85 | */ |
| 86 | protected function getDataFromGetRequest(): array |
| 87 | { |
| 88 | return give_clean($_GET); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @since 3.0.0 |
| 93 | * |
| 94 | * @param string $type |
| 95 | * @param string $method |
| 96 | * @param string $uri |
| 97 | * @param $action |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | protected function addRoute(string $type, string $method, string $uri, $action) |
| 102 | { |
| 103 | add_action('template_redirect', function () use ($type, $method, $uri, $action) { |
| 104 | if (!$this->isRouteValid($uri)) { |
| 105 | // fail silently for use with template_redirect |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | $request = $this->getRequestDataByType($type); |
| 110 | |
| 111 | if (is_callable($action)) { |
| 112 | return $action($request); |
| 113 | } |
| 114 | |
| 115 | if (!method_exists($action, $method)) { |
| 116 | throw new InvalidArgumentException("The method $method does not exist on $action"); |
| 117 | } |
| 118 | |
| 119 | return give($action)->$method($request); |
| 120 | }); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @since 3.0.0 |
| 125 | */ |
| 126 | public function url(string $uri, array $args = []): string |
| 127 | { |
| 128 | return add_query_arg( |
| 129 | array_merge( |
| 130 | ['givewp-route' => $uri], |
| 131 | $args |
| 132 | ), |
| 133 | home_url() |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 |