@@ -31,265 +31,265 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class Http |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * Return's the protocol that the request was made with. |
|
| 36 | - * |
|
| 37 | - * @return string |
|
| 38 | - */ |
|
| 39 | - public function protocol() |
|
| 40 | - { |
|
| 41 | - if ($this->server('HTTPS') == 'on' || |
|
| 42 | - $this->server('HTTPS') == 1 || |
|
| 43 | - $this->server('SERVER_PORT') == 443 || |
|
| 44 | - (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PROTO') == 'https') || |
|
| 45 | - (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PORT') == 443)) |
|
| 46 | - { |
|
| 47 | - return 'https'; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - return 'http'; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Fetch an item from the COOKIE array. |
|
| 55 | - * |
|
| 56 | - * @param string $index The index key |
|
| 57 | - * @param mixed $default The default value |
|
| 58 | - * |
|
| 59 | - * @return string|array |
|
| 60 | - */ |
|
| 61 | - public function cookie($index = null, $default = null) |
|
| 62 | - { |
|
| 63 | - return (func_num_args() === 0) ? $_COOKIE : Arr::get($_COOKIE, strtoupper($index), $default); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Fetch an item from the FILE array. |
|
| 68 | - * |
|
| 69 | - * @param string $index The index key |
|
| 70 | - * @param mixed $default The default value |
|
| 71 | - * |
|
| 72 | - * @return string|array |
|
| 73 | - */ |
|
| 74 | - public function file($index = null, $default = null) |
|
| 75 | - { |
|
| 76 | - return (func_num_args() === 0) ? $_FILES : Arr::get($_FILES, strtoupper($index), $default); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Fetch an item from the SERVER array. |
|
| 81 | - * |
|
| 82 | - * @param string $index The index key |
|
| 83 | - * @param mixed $default The default value |
|
| 84 | - * |
|
| 85 | - * @return string|array |
|
| 86 | - */ |
|
| 87 | - public function server($index = null, $default = null) |
|
| 88 | - { |
|
| 89 | - return (func_num_args() === 0) ? $_SERVER : Arr::get($_SERVER, strtoupper($index), $default); |
|
| 90 | - } |
|
| 34 | + /** |
|
| 35 | + * Return's the protocol that the request was made with. |
|
| 36 | + * |
|
| 37 | + * @return string |
|
| 38 | + */ |
|
| 39 | + public function protocol() |
|
| 40 | + { |
|
| 41 | + if ($this->server('HTTPS') == 'on' || |
|
| 42 | + $this->server('HTTPS') == 1 || |
|
| 43 | + $this->server('SERVER_PORT') == 443 || |
|
| 44 | + (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PROTO') == 'https') || |
|
| 45 | + (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PORT') == 443)) |
|
| 46 | + { |
|
| 47 | + return 'https'; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + return 'http'; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Fetch an item from the COOKIE array. |
|
| 55 | + * |
|
| 56 | + * @param string $index The index key |
|
| 57 | + * @param mixed $default The default value |
|
| 58 | + * |
|
| 59 | + * @return string|array |
|
| 60 | + */ |
|
| 61 | + public function cookie($index = null, $default = null) |
|
| 62 | + { |
|
| 63 | + return (func_num_args() === 0) ? $_COOKIE : Arr::get($_COOKIE, strtoupper($index), $default); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Fetch an item from the FILE array. |
|
| 68 | + * |
|
| 69 | + * @param string $index The index key |
|
| 70 | + * @param mixed $default The default value |
|
| 71 | + * |
|
| 72 | + * @return string|array |
|
| 73 | + */ |
|
| 74 | + public function file($index = null, $default = null) |
|
| 75 | + { |
|
| 76 | + return (func_num_args() === 0) ? $_FILES : Arr::get($_FILES, strtoupper($index), $default); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Fetch an item from the SERVER array. |
|
| 81 | + * |
|
| 82 | + * @param string $index The index key |
|
| 83 | + * @param mixed $default The default value |
|
| 84 | + * |
|
| 85 | + * @return string|array |
|
| 86 | + */ |
|
| 87 | + public function server($index = null, $default = null) |
|
| 88 | + { |
|
| 89 | + return (func_num_args() === 0) ? $_SERVER : Arr::get($_SERVER, strtoupper($index), $default); |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Gets the URI Protocol based setting, will attempt to detect the path |
|
| 94 | - * portion of the current URI. |
|
| 95 | - * |
|
| 96 | - * @param string $protocol |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function detectPath(string $protocol = '') |
|
| 101 | - { |
|
| 102 | - if (empty($protocol)) { |
|
| 103 | - $protocol = 'REQUEST_URI'; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - switch($protocol) { |
|
| 107 | - case 'REQUEST_URI': |
|
| 108 | - $path = $this->parseRequestUri(); |
|
| 109 | - break; |
|
| 110 | - case 'QUERY_STRING': |
|
| 111 | - $path = $this->parseQueryString(); |
|
| 112 | - break; |
|
| 113 | - case 'PATH_INFO': |
|
| 114 | - default: |
|
| 115 | - $path = $this->server($protocol) ?? $this->parseRequestUri(); |
|
| 116 | - break; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $path; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Filters a value from the start of a string in this case the passed URI string. |
|
| 124 | - * |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - protected function parseRequestUri() |
|
| 128 | - { |
|
| 129 | - if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) { |
|
| 130 | - return ''; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $requestURI = $this->server('REQUEST_URI') ?? '/'; |
|
| 134 | - $components = parse_url($requestURI); |
|
| 135 | - $query = $components['query'] ?? ''; |
|
| 136 | - $uri = $components['path'] ?? ''; |
|
| 137 | - |
|
| 138 | - // If the search value is at the start |
|
| 139 | - if (isset($this->server('SCRIPT_NAME')[0])) { |
|
| 140 | - if (0 === strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
| 141 | - $uri = (string) substr($uri, strlen($this->server('SCRIPT_NAME'))); |
|
| 142 | - } elseif (0 < strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
| 143 | - $uri = (string) substr($uri, strpos($uri, $this->server('SCRIPT_NAME')) + strlen($this->server('SCRIPT_NAME'))); |
|
| 144 | - } elseif (0 === strpos($uri, dirname($this->server('SCRIPT_NAME')))) { |
|
| 145 | - $uri = (string) substr($uri, strlen(dirname($this->server('SCRIPT_NAME')))); |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - // This section ensures that even on servers that require the URI to contain |
|
| 150 | - // the query string (Nginx) is the correctly |
|
| 151 | - if ('' === trim($uri, '/') && 0 === strncmp($query, '/', 1)) { |
|
| 152 | - $query = explode('?', $query, 2); |
|
| 153 | - $uri = $query[0]; |
|
| 154 | - $_SERVER['QUERY_STRING'] = $query[1] ?? ''; |
|
| 155 | - } else { |
|
| 156 | - $_SERVER['QUERY_STRING'] = $query; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Parses the string into variables |
|
| 160 | - parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
| 161 | - |
|
| 162 | - if ('/' === $uri || '' === $uri) { |
|
| 163 | - return ''; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - return $this->filterDecode($uri); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Will parse QUERY_STRING and automatically detect the URI from it. |
|
| 171 | - * |
|
| 172 | - * @return string |
|
| 173 | - */ |
|
| 174 | - protected function parseQueryString() |
|
| 175 | - { |
|
| 176 | - $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
| 177 | - |
|
| 178 | - if (trim($uri, '/') === '') { |
|
| 179 | - return ''; |
|
| 180 | - } elseif (0 === strncmp($uri, '/', 1)) { |
|
| 181 | - $uri = explode('?', $uri, 2); |
|
| 182 | - $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
| 183 | - $uri = $uri[0] ?? ''; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
| 187 | - |
|
| 188 | - return $this->filterDecode($uri); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
| 193 | - * |
|
| 194 | - * @param string $uri |
|
| 195 | - * |
|
| 196 | - * @return string |
|
| 197 | - */ |
|
| 198 | - protected function filterDecode($uri) |
|
| 199 | - { |
|
| 200 | - // Remove all characters except letters, |
|
| 201 | - // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
| 202 | - $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
| 92 | + /** |
|
| 93 | + * Gets the URI Protocol based setting, will attempt to detect the path |
|
| 94 | + * portion of the current URI. |
|
| 95 | + * |
|
| 96 | + * @param string $protocol |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function detectPath(string $protocol = '') |
|
| 101 | + { |
|
| 102 | + if (empty($protocol)) { |
|
| 103 | + $protocol = 'REQUEST_URI'; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + switch($protocol) { |
|
| 107 | + case 'REQUEST_URI': |
|
| 108 | + $path = $this->parseRequestUri(); |
|
| 109 | + break; |
|
| 110 | + case 'QUERY_STRING': |
|
| 111 | + $path = $this->parseQueryString(); |
|
| 112 | + break; |
|
| 113 | + case 'PATH_INFO': |
|
| 114 | + default: |
|
| 115 | + $path = $this->server($protocol) ?? $this->parseRequestUri(); |
|
| 116 | + break; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $path; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Filters a value from the start of a string in this case the passed URI string. |
|
| 124 | + * |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + protected function parseRequestUri() |
|
| 128 | + { |
|
| 129 | + if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) { |
|
| 130 | + return ''; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $requestURI = $this->server('REQUEST_URI') ?? '/'; |
|
| 134 | + $components = parse_url($requestURI); |
|
| 135 | + $query = $components['query'] ?? ''; |
|
| 136 | + $uri = $components['path'] ?? ''; |
|
| 137 | + |
|
| 138 | + // If the search value is at the start |
|
| 139 | + if (isset($this->server('SCRIPT_NAME')[0])) { |
|
| 140 | + if (0 === strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
| 141 | + $uri = (string) substr($uri, strlen($this->server('SCRIPT_NAME'))); |
|
| 142 | + } elseif (0 < strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
| 143 | + $uri = (string) substr($uri, strpos($uri, $this->server('SCRIPT_NAME')) + strlen($this->server('SCRIPT_NAME'))); |
|
| 144 | + } elseif (0 === strpos($uri, dirname($this->server('SCRIPT_NAME')))) { |
|
| 145 | + $uri = (string) substr($uri, strlen(dirname($this->server('SCRIPT_NAME')))); |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + // This section ensures that even on servers that require the URI to contain |
|
| 150 | + // the query string (Nginx) is the correctly |
|
| 151 | + if ('' === trim($uri, '/') && 0 === strncmp($query, '/', 1)) { |
|
| 152 | + $query = explode('?', $query, 2); |
|
| 153 | + $uri = $query[0]; |
|
| 154 | + $_SERVER['QUERY_STRING'] = $query[1] ?? ''; |
|
| 155 | + } else { |
|
| 156 | + $_SERVER['QUERY_STRING'] = $query; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Parses the string into variables |
|
| 160 | + parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
| 161 | + |
|
| 162 | + if ('/' === $uri || '' === $uri) { |
|
| 163 | + return ''; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + return $this->filterDecode($uri); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Will parse QUERY_STRING and automatically detect the URI from it. |
|
| 171 | + * |
|
| 172 | + * @return string |
|
| 173 | + */ |
|
| 174 | + protected function parseQueryString() |
|
| 175 | + { |
|
| 176 | + $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
| 177 | + |
|
| 178 | + if (trim($uri, '/') === '') { |
|
| 179 | + return ''; |
|
| 180 | + } elseif (0 === strncmp($uri, '/', 1)) { |
|
| 181 | + $uri = explode('?', $uri, 2); |
|
| 182 | + $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
| 183 | + $uri = $uri[0] ?? ''; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
| 187 | + |
|
| 188 | + return $this->filterDecode($uri); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
| 193 | + * |
|
| 194 | + * @param string $uri |
|
| 195 | + * |
|
| 196 | + * @return string |
|
| 197 | + */ |
|
| 198 | + protected function filterDecode($uri) |
|
| 199 | + { |
|
| 200 | + // Remove all characters except letters, |
|
| 201 | + // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
| 202 | + $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
| 203 | 203 | |
| 204 | - // Return argument if not empty or return a single slash |
|
| 205 | - return trim($uri, '/') ?: '/'; |
|
| 206 | - } |
|
| 204 | + // Return argument if not empty or return a single slash |
|
| 205 | + return trim($uri, '/') ?: '/'; |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Parse the base URL. |
|
| 210 | - * |
|
| 211 | - * @return string |
|
| 212 | - */ |
|
| 213 | - public function parseBaseUrl() |
|
| 214 | - { |
|
| 215 | - $filename = basename($this->server('SCRIPT_FILENAME')); |
|
| 208 | + /** |
|
| 209 | + * Parse the base URL. |
|
| 210 | + * |
|
| 211 | + * @return string |
|
| 212 | + */ |
|
| 213 | + public function parseBaseUrl() |
|
| 214 | + { |
|
| 215 | + $filename = basename($this->server('SCRIPT_FILENAME')); |
|
| 216 | 216 | |
| 217 | - if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
| 218 | - $baseUrl = $this->server('SCRIPT_NAME'); |
|
| 219 | - } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
| 220 | - $baseUrl = $this->server('PHP_SELF'); |
|
| 221 | - } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
| 222 | - $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
| 223 | - } else { |
|
| 224 | - $path = $this->server('PHP_SELF', ''); |
|
| 225 | - $file = $this->server('SCRIPT_FILENAME', ''); |
|
| 226 | - $segs = explode('/', trim($file, '/')); |
|
| 227 | - $segs = array_reverse($segs); |
|
| 228 | - $index = 0; |
|
| 229 | - $last = count($segs); |
|
| 230 | - $baseUrl = ''; |
|
| 217 | + if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
| 218 | + $baseUrl = $this->server('SCRIPT_NAME'); |
|
| 219 | + } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
| 220 | + $baseUrl = $this->server('PHP_SELF'); |
|
| 221 | + } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
| 222 | + $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
| 223 | + } else { |
|
| 224 | + $path = $this->server('PHP_SELF', ''); |
|
| 225 | + $file = $this->server('SCRIPT_FILENAME', ''); |
|
| 226 | + $segs = explode('/', trim($file, '/')); |
|
| 227 | + $segs = array_reverse($segs); |
|
| 228 | + $index = 0; |
|
| 229 | + $last = count($segs); |
|
| 230 | + $baseUrl = ''; |
|
| 231 | 231 | |
| 232 | - do { |
|
| 233 | - $seg = $segs[$index]; |
|
| 234 | - $baseUrl = '/'.$seg.$baseUrl; |
|
| 235 | - ++$index; |
|
| 236 | - } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // Does the baseUrl have anything in common with the request_uri? |
|
| 240 | - $requestUri = $this->parseRequestUri(); |
|
| 241 | - |
|
| 242 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
| 243 | - $requestUri = '/'.$requestUri; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - $baseUrl = dirname($baseUrl); |
|
| 247 | - |
|
| 248 | - if (empty($baseUrl) || false !== strpos(rawurldecode($requestUri), $baseUrl)) { |
|
| 249 | - // no match whatsoever; set it blank |
|
| 250 | - return ''; |
|
| 251 | - } |
|
| 232 | + do { |
|
| 233 | + $seg = $segs[$index]; |
|
| 234 | + $baseUrl = '/'.$seg.$baseUrl; |
|
| 235 | + ++$index; |
|
| 236 | + } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // Does the baseUrl have anything in common with the request_uri? |
|
| 240 | + $requestUri = $this->parseRequestUri(); |
|
| 241 | + |
|
| 242 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
| 243 | + $requestUri = '/'.$requestUri; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + $baseUrl = dirname($baseUrl); |
|
| 247 | + |
|
| 248 | + if (empty($baseUrl) || false !== strpos(rawurldecode($requestUri), $baseUrl)) { |
|
| 249 | + // no match whatsoever; set it blank |
|
| 250 | + return ''; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
| 254 | - // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
| 255 | - // from PATH_INFO or QUERY_STRING |
|
| 256 | - if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
| 257 | - $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
| 258 | - } |
|
| 253 | + // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
| 254 | + // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
| 255 | + // from PATH_INFO or QUERY_STRING |
|
| 256 | + if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
| 257 | + $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
| 258 | + } |
|
| 259 | 259 | |
| 260 | - return rtrim($baseUrl, '/'.DIRECTORY_SEPARATOR); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Parse the path info. |
|
| 265 | - * |
|
| 266 | - * @return string |
|
| 267 | - */ |
|
| 268 | - public function parsePathInfo() |
|
| 269 | - { |
|
| 270 | - if (null === ($requestUri = $this->parseRequestUri())) { |
|
| 271 | - return '/'; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - // Remove the query string from REQUEST_URI |
|
| 275 | - if (false !== $pos = strpos($requestUri, '?')) { |
|
| 276 | - $requestUri = substr($requestUri, 0, $pos); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
| 280 | - $requestUri = '/'.$requestUri; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - if (null === ($baseUrl = $this->parseBaseUrl())) { |
|
| 284 | - return $requestUri; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - $pathInfo = substr($requestUri, strlen($baseUrl)); |
|
| 288 | - |
|
| 289 | - if (false === $pathInfo && '' === $pathInfo) { |
|
| 290 | - return '/'; |
|
| 291 | - } |
|
| 260 | + return rtrim($baseUrl, '/'.DIRECTORY_SEPARATOR); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Parse the path info. |
|
| 265 | + * |
|
| 266 | + * @return string |
|
| 267 | + */ |
|
| 268 | + public function parsePathInfo() |
|
| 269 | + { |
|
| 270 | + if (null === ($requestUri = $this->parseRequestUri())) { |
|
| 271 | + return '/'; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + // Remove the query string from REQUEST_URI |
|
| 275 | + if (false !== $pos = strpos($requestUri, '?')) { |
|
| 276 | + $requestUri = substr($requestUri, 0, $pos); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
| 280 | + $requestUri = '/'.$requestUri; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + if (null === ($baseUrl = $this->parseBaseUrl())) { |
|
| 284 | + return $requestUri; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + $pathInfo = substr($requestUri, strlen($baseUrl)); |
|
| 288 | + |
|
| 289 | + if (false === $pathInfo && '' === $pathInfo) { |
|
| 290 | + return '/'; |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - return (string) $pathInfo; |
|
| 294 | - } |
|
| 293 | + return (string) $pathInfo; |
|
| 294 | + } |
|
| 295 | 295 | } |
| 296 | 296 | \ No newline at end of file |
@@ -33,721 +33,721 @@ |
||
| 33 | 33 | */ |
| 34 | 34 | class Cli |
| 35 | 35 | { |
| 36 | - /** |
|
| 37 | - * Background color identifier. |
|
| 38 | - * |
|
| 39 | - * @var array $backgroundColors |
|
| 40 | - */ |
|
| 41 | - protected static $backgroundColors = [ |
|
| 42 | - 'black' => '40', |
|
| 43 | - 'red' => '41', |
|
| 44 | - 'green' => '42', |
|
| 45 | - 'yellow' => '43', |
|
| 46 | - 'blue' => '44', |
|
| 47 | - 'magenta' => '45', |
|
| 48 | - 'cyan' => '46', |
|
| 49 | - 'light_gray' => '47' |
|
| 50 | - ]; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Foreground color identifier. |
|
| 54 | - * |
|
| 55 | - * @var array $foregroundColors |
|
| 56 | - */ |
|
| 57 | - protected static $foregroundColors = [ |
|
| 58 | - 'black' => '0;30', |
|
| 59 | - 'dark_gray' => '1;30', |
|
| 60 | - 'blue' => '0;34', |
|
| 61 | - 'dark_blue' => '1;34', |
|
| 62 | - 'light_blue' => '1;34', |
|
| 63 | - 'green' => '0;32', |
|
| 64 | - 'light_green' => '1;32', |
|
| 65 | - 'cyan' => '0;36', |
|
| 66 | - 'light_cyan' => '1;36', |
|
| 67 | - 'red' => '0;31', |
|
| 68 | - 'light_red' => '1;31', |
|
| 69 | - 'purple' => '0;35', |
|
| 70 | - 'light_purple' => '1;35', |
|
| 71 | - 'light_yellow' => '0;33', |
|
| 72 | - 'yellow' => '1;33', |
|
| 73 | - 'light_gray' => '0;37', |
|
| 74 | - 'white' => '1;37' |
|
| 75 | - ]; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Indicates that you do not use any color for foreground or background. |
|
| 79 | - * |
|
| 80 | - * @var bool $noColor |
|
| 81 | - */ |
|
| 82 | - public static $noColor = false; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * String of arguments to be used in console. |
|
| 86 | - * |
|
| 87 | - * @var array $options |
|
| 88 | - */ |
|
| 89 | - protected static $options = []; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Readline Support for command line. |
|
| 93 | - * |
|
| 94 | - * @var bool $readlineSupport |
|
| 95 | - */ |
|
| 96 | - public static $readlineSupport = false; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * List of array segments. |
|
| 100 | - * |
|
| 101 | - * @var array $segments |
|
| 102 | - */ |
|
| 103 | - protected static $segments = []; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * The standar STDERR is where the application writes its error messages. |
|
| 107 | - * |
|
| 108 | - * @var string $stderr |
|
| 109 | - */ |
|
| 110 | - protected static $stderr; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * The estandar STDOUT is where the application records its output messages. |
|
| 114 | - * |
|
| 115 | - * @var resource $stdout |
|
| 116 | - */ |
|
| 117 | - protected static $stdout; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Message that tells the user that he is waiting to receive an order. |
|
| 121 | - * |
|
| 122 | - * @var string $waitMsg |
|
| 123 | - */ |
|
| 124 | - public static $waitMsg = 'Press any key to continue...'; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Static constructor. Parses all the CLI params. |
|
| 128 | - * |
|
| 129 | - * @return \Syscodes\Contracts\Core\Lenevor $core |
|
| 130 | - * |
|
| 131 | - * @throws \Exception |
|
| 132 | - */ |
|
| 133 | - public static function initialize() |
|
| 134 | - { |
|
| 135 | - // Readline is an extension for PHP that makes interactive the command console |
|
| 136 | - static::$readlineSupport = extension_loaded('readline'); |
|
| 137 | - |
|
| 138 | - // clear segments & options to keep testing clean |
|
| 139 | - static::$options = []; |
|
| 140 | - static::$segments = []; |
|
| 141 | - |
|
| 142 | - static::parseCommandLine(); |
|
| 143 | - |
|
| 144 | - // Writes its error messages |
|
| 145 | - static::$stderr = STDERR; |
|
| 146 | - |
|
| 147 | - // Records its output messages |
|
| 148 | - static::$stdout = STDOUT; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Beeps a certain number of times. |
|
| 153 | - * |
|
| 154 | - * @param int $num The number of times to beep |
|
| 155 | - * |
|
| 156 | - * @return int |
|
| 157 | - */ |
|
| 158 | - public static function bell(int $num = 1) |
|
| 159 | - { |
|
| 160 | - echo str_repeat("\x07", $num); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Clears the screen of output. |
|
| 165 | - * |
|
| 166 | - * @return void |
|
| 167 | - */ |
|
| 168 | - public static function clearScreen() |
|
| 169 | - { |
|
| 170 | - static::isWindows() |
|
| 171 | - // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 172 | - ? static::newLine(40) |
|
| 173 | - // Anything with a flair of Unix will handle these magic characters |
|
| 174 | - : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Returns the given text with the correct color codes for a foreground and |
|
| 179 | - * optionally a background color. |
|
| 180 | - * |
|
| 181 | - * @param string $text The text to color |
|
| 182 | - * @param string $foreground The foreground color |
|
| 183 | - * @param string $background The background color |
|
| 184 | - * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 185 | - * |
|
| 186 | - * @return string The color coded string |
|
| 187 | - * |
|
| 188 | - * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 189 | - */ |
|
| 190 | - public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 191 | - { |
|
| 192 | - if (static::$noColor) |
|
| 193 | - { |
|
| 194 | - return $text; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
| 198 | - throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | - throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 206 | - |
|
| 207 | - if ($background !== null) { |
|
| 208 | - $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if ($format === 'underline') { |
|
| 212 | - $string .= "\033[4m"; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - $string .= $text."\033[0m"; |
|
| 216 | - |
|
| 217 | - return $string; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Get the number of characters in a string. |
|
| 222 | - * |
|
| 223 | - * @param string $string |
|
| 224 | - * |
|
| 225 | - * @return int |
|
| 226 | - */ |
|
| 227 | - public static function strlen(?string $string) |
|
| 228 | - { |
|
| 229 | - if (is_null($string)) { |
|
| 230 | - return 0; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - foreach (static::$foregroundColors as $color) { |
|
| 234 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - foreach (static::$backgroundColors as $color) { |
|
| 238 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 242 | - |
|
| 243 | - return mb_strlen($string); |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 248 | - * |
|
| 249 | - * @param string|array $text The text to output, or array of errors |
|
| 250 | - * @param string $foreground The foreground color |
|
| 251 | - * @param string|null $background the background color |
|
| 252 | - * |
|
| 253 | - * @return string |
|
| 254 | - */ |
|
| 255 | - public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 256 | - { |
|
| 257 | - if (is_array($text)) { |
|
| 258 | - $text = implode(PHP_EOL, $text); |
|
| 259 | - } |
|
| 36 | + /** |
|
| 37 | + * Background color identifier. |
|
| 38 | + * |
|
| 39 | + * @var array $backgroundColors |
|
| 40 | + */ |
|
| 41 | + protected static $backgroundColors = [ |
|
| 42 | + 'black' => '40', |
|
| 43 | + 'red' => '41', |
|
| 44 | + 'green' => '42', |
|
| 45 | + 'yellow' => '43', |
|
| 46 | + 'blue' => '44', |
|
| 47 | + 'magenta' => '45', |
|
| 48 | + 'cyan' => '46', |
|
| 49 | + 'light_gray' => '47' |
|
| 50 | + ]; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Foreground color identifier. |
|
| 54 | + * |
|
| 55 | + * @var array $foregroundColors |
|
| 56 | + */ |
|
| 57 | + protected static $foregroundColors = [ |
|
| 58 | + 'black' => '0;30', |
|
| 59 | + 'dark_gray' => '1;30', |
|
| 60 | + 'blue' => '0;34', |
|
| 61 | + 'dark_blue' => '1;34', |
|
| 62 | + 'light_blue' => '1;34', |
|
| 63 | + 'green' => '0;32', |
|
| 64 | + 'light_green' => '1;32', |
|
| 65 | + 'cyan' => '0;36', |
|
| 66 | + 'light_cyan' => '1;36', |
|
| 67 | + 'red' => '0;31', |
|
| 68 | + 'light_red' => '1;31', |
|
| 69 | + 'purple' => '0;35', |
|
| 70 | + 'light_purple' => '1;35', |
|
| 71 | + 'light_yellow' => '0;33', |
|
| 72 | + 'yellow' => '1;33', |
|
| 73 | + 'light_gray' => '0;37', |
|
| 74 | + 'white' => '1;37' |
|
| 75 | + ]; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Indicates that you do not use any color for foreground or background. |
|
| 79 | + * |
|
| 80 | + * @var bool $noColor |
|
| 81 | + */ |
|
| 82 | + public static $noColor = false; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * String of arguments to be used in console. |
|
| 86 | + * |
|
| 87 | + * @var array $options |
|
| 88 | + */ |
|
| 89 | + protected static $options = []; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Readline Support for command line. |
|
| 93 | + * |
|
| 94 | + * @var bool $readlineSupport |
|
| 95 | + */ |
|
| 96 | + public static $readlineSupport = false; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * List of array segments. |
|
| 100 | + * |
|
| 101 | + * @var array $segments |
|
| 102 | + */ |
|
| 103 | + protected static $segments = []; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * The standar STDERR is where the application writes its error messages. |
|
| 107 | + * |
|
| 108 | + * @var string $stderr |
|
| 109 | + */ |
|
| 110 | + protected static $stderr; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * The estandar STDOUT is where the application records its output messages. |
|
| 114 | + * |
|
| 115 | + * @var resource $stdout |
|
| 116 | + */ |
|
| 117 | + protected static $stdout; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Message that tells the user that he is waiting to receive an order. |
|
| 121 | + * |
|
| 122 | + * @var string $waitMsg |
|
| 123 | + */ |
|
| 124 | + public static $waitMsg = 'Press any key to continue...'; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Static constructor. Parses all the CLI params. |
|
| 128 | + * |
|
| 129 | + * @return \Syscodes\Contracts\Core\Lenevor $core |
|
| 130 | + * |
|
| 131 | + * @throws \Exception |
|
| 132 | + */ |
|
| 133 | + public static function initialize() |
|
| 134 | + { |
|
| 135 | + // Readline is an extension for PHP that makes interactive the command console |
|
| 136 | + static::$readlineSupport = extension_loaded('readline'); |
|
| 137 | + |
|
| 138 | + // clear segments & options to keep testing clean |
|
| 139 | + static::$options = []; |
|
| 140 | + static::$segments = []; |
|
| 141 | + |
|
| 142 | + static::parseCommandLine(); |
|
| 143 | + |
|
| 144 | + // Writes its error messages |
|
| 145 | + static::$stderr = STDERR; |
|
| 146 | + |
|
| 147 | + // Records its output messages |
|
| 148 | + static::$stdout = STDOUT; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Beeps a certain number of times. |
|
| 153 | + * |
|
| 154 | + * @param int $num The number of times to beep |
|
| 155 | + * |
|
| 156 | + * @return int |
|
| 157 | + */ |
|
| 158 | + public static function bell(int $num = 1) |
|
| 159 | + { |
|
| 160 | + echo str_repeat("\x07", $num); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Clears the screen of output. |
|
| 165 | + * |
|
| 166 | + * @return void |
|
| 167 | + */ |
|
| 168 | + public static function clearScreen() |
|
| 169 | + { |
|
| 170 | + static::isWindows() |
|
| 171 | + // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
| 172 | + ? static::newLine(40) |
|
| 173 | + // Anything with a flair of Unix will handle these magic characters |
|
| 174 | + : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Returns the given text with the correct color codes for a foreground and |
|
| 179 | + * optionally a background color. |
|
| 180 | + * |
|
| 181 | + * @param string $text The text to color |
|
| 182 | + * @param string $foreground The foreground color |
|
| 183 | + * @param string $background The background color |
|
| 184 | + * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
| 185 | + * |
|
| 186 | + * @return string The color coded string |
|
| 187 | + * |
|
| 188 | + * @throws \Syscodes\Core\Exceptions\LenevorException |
|
| 189 | + */ |
|
| 190 | + public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
| 191 | + { |
|
| 192 | + if (static::$noColor) |
|
| 193 | + { |
|
| 194 | + return $text; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
| 198 | + throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | + throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
| 206 | + |
|
| 207 | + if ($background !== null) { |
|
| 208 | + $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if ($format === 'underline') { |
|
| 212 | + $string .= "\033[4m"; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + $string .= $text."\033[0m"; |
|
| 216 | + |
|
| 217 | + return $string; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Get the number of characters in a string. |
|
| 222 | + * |
|
| 223 | + * @param string $string |
|
| 224 | + * |
|
| 225 | + * @return int |
|
| 226 | + */ |
|
| 227 | + public static function strlen(?string $string) |
|
| 228 | + { |
|
| 229 | + if (is_null($string)) { |
|
| 230 | + return 0; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + foreach (static::$foregroundColors as $color) { |
|
| 234 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + foreach (static::$backgroundColors as $color) { |
|
| 238 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
| 242 | + |
|
| 243 | + return mb_strlen($string); |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
| 248 | + * |
|
| 249 | + * @param string|array $text The text to output, or array of errors |
|
| 250 | + * @param string $foreground The foreground color |
|
| 251 | + * @param string|null $background the background color |
|
| 252 | + * |
|
| 253 | + * @return string |
|
| 254 | + */ |
|
| 255 | + public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
| 256 | + { |
|
| 257 | + if (is_array($text)) { |
|
| 258 | + $text = implode(PHP_EOL, $text); |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - if ($foreground || $background) { |
|
| 262 | - $text = static::color($text, $foreground, $background); |
|
| 263 | - } |
|
| 261 | + if ($foreground || $background) { |
|
| 262 | + $text = static::color($text, $foreground, $background); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | - static::fwrite(static::$stderr, $text.PHP_EOL); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Attempts to determine the width of the viewable CLI window. |
|
| 270 | - * |
|
| 271 | - * @param int $default |
|
| 272 | - * |
|
| 273 | - * @return int |
|
| 274 | - */ |
|
| 275 | - public static function getWidth(int $default = 80) |
|
| 276 | - { |
|
| 277 | - if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
| 278 | - return $default; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - return (int) shell_exec('tput cols'); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Attempts to determine the height of the viewable CLI window. |
|
| 286 | - * |
|
| 287 | - * @param int $default |
|
| 288 | - * |
|
| 289 | - * @return int |
|
| 290 | - */ |
|
| 291 | - public static function getHeight(int $default = 32) |
|
| 292 | - { |
|
| 293 | - if (static::isWindows()) { |
|
| 294 | - return $default; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return (int) shell_exec('tput lines'); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 302 | - * If no maximum width is specified, will wrap to the window's max. |
|
| 303 | - * |
|
| 304 | - * @param string $string |
|
| 305 | - * @param int $max |
|
| 306 | - * @param int $padLeft |
|
| 307 | - * |
|
| 308 | - * @return string |
|
| 309 | - */ |
|
| 310 | - public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 311 | - { |
|
| 312 | - if (empty($string)) { |
|
| 313 | - return ''; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - if ($max === 0) { |
|
| 317 | - $max = static::getWidth(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - if (static::getWidth() < $max) { |
|
| 321 | - $max = static::getWidth(); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - $max = $max - $padLeft; |
|
| 325 | - |
|
| 326 | - $lines = wordwrap($string, $max); |
|
| 327 | - |
|
| 328 | - if ($pad_left > 0) { |
|
| 329 | - $lines = explode(PHP_EOL, $lines); |
|
| 330 | - |
|
| 331 | - $first = true; |
|
| 332 | - |
|
| 333 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 334 | - if ( ! $first) { |
|
| 335 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 336 | - } else { |
|
| 337 | - $first = false; |
|
| 338 | - } |
|
| 339 | - }); |
|
| 340 | - |
|
| 341 | - $lines = implode(PHP_EOL, $lines); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - return $lines; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Get input from the shell, using readline or the standard STDIN. |
|
| 349 | - * |
|
| 350 | - * @param string|int $prefix The name of the option (int if unnamed) |
|
| 351 | - * |
|
| 352 | - * @return string |
|
| 353 | - */ |
|
| 354 | - public static function input($prefix = '') |
|
| 355 | - { |
|
| 356 | - if (static::$readlineSupport) { |
|
| 357 | - return readline($prefix); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - echo $prefix; |
|
| 361 | - |
|
| 362 | - return fgets(STDIN); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * If operating system === windows. |
|
| 367 | - * |
|
| 368 | - * @return string |
|
| 369 | - */ |
|
| 370 | - public static function isWindows() |
|
| 371 | - { |
|
| 372 | - return stripos(PHP_OS, 'WIN') === 0; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Enter a number of empty lines. |
|
| 377 | - * |
|
| 378 | - * @param int $num Number of lines to output |
|
| 379 | - * |
|
| 380 | - * @return void |
|
| 381 | - */ |
|
| 382 | - public static function newLine(int $num = 1) |
|
| 383 | - { |
|
| 384 | - for ($i = 0; $i < $num; $i++) { |
|
| 385 | - static::write(); |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Returns the option with the given name. You can also give the option number. |
|
| 391 | - * |
|
| 392 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 393 | - * @param mixed $default The value to return if the option is not defined |
|
| 394 | - * |
|
| 395 | - * @return mixed |
|
| 396 | - * |
|
| 397 | - * @uses \Syscodes\Contract\Core\Lenevor |
|
| 398 | - */ |
|
| 399 | - public static function option($name, $default = null) |
|
| 400 | - { |
|
| 401 | - if ( ! isset(static::$options[$name])) { |
|
| 402 | - return Lenevor::value($default); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - return static::$options[$name]; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Parses the command line it was called from and collects all |
|
| 410 | - * options and valid segments. |
|
| 411 | - * |
|
| 412 | - * @return bool |
|
| 413 | - */ |
|
| 414 | - protected static function parseCommandLine() |
|
| 415 | - { |
|
| 416 | - $options = false; |
|
| 417 | - |
|
| 418 | - for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
| 419 | - if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
| 420 | - static::$segments[] = $_SERVER['argv'][$i]; |
|
| 421 | - |
|
| 422 | - continue; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - $options = true; |
|
| 426 | - |
|
| 427 | - $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 428 | - $value = null; |
|
| 429 | - |
|
| 430 | - if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
| 431 | - $value = $_SERVER['argv'][$i + 1]; |
|
| 432 | - $i++; |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - static::$options[$args] = $value; |
|
| 436 | - |
|
| 437 | - $options = false; |
|
| 438 | - } |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Returns the command line string portions of the arguments, minus |
|
| 443 | - * any options, as a string. |
|
| 444 | - * |
|
| 445 | - * @return string |
|
| 446 | - */ |
|
| 447 | - public static function getURI() |
|
| 448 | - { |
|
| 449 | - return implode('/', static::$segments); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Returns an individual segment. |
|
| 454 | - * |
|
| 455 | - * @param int $index |
|
| 456 | - * |
|
| 457 | - * @return mixed|null |
|
| 458 | - */ |
|
| 459 | - public static function getSegment(int $index) |
|
| 460 | - { |
|
| 461 | - if ( ! isset(static::$segments[$index - 1])) { |
|
| 462 | - return null; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - return static::$segments[$index - 1]; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * Returns the raw array of segments found. |
|
| 470 | - * |
|
| 471 | - * @return array |
|
| 472 | - */ |
|
| 473 | - public static function getSegments() |
|
| 474 | - { |
|
| 475 | - return static::$segments; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 480 | - * |
|
| 481 | - * Usage: |
|
| 482 | - * |
|
| 483 | - * // Waits for any key press |
|
| 484 | - * Cli::prompt(); |
|
| 485 | - * |
|
| 486 | - * // Takes any input |
|
| 487 | - * $color = Cli::prompt('What is your favorite color?'); |
|
| 488 | - * |
|
| 489 | - * // Takes any input, but offers default |
|
| 490 | - * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 491 | - * |
|
| 492 | - * // Will only accept the options in the array |
|
| 493 | - * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 494 | - * |
|
| 495 | - * @return string The user input |
|
| 496 | - */ |
|
| 497 | - public static function prompt() |
|
| 498 | - { |
|
| 499 | - $args = func_get_args(); |
|
| 500 | - |
|
| 501 | - $options = []; |
|
| 502 | - $output = ''; |
|
| 503 | - $default = null; |
|
| 504 | - |
|
| 505 | - // How many we got |
|
| 506 | - $arg_count = count($args); |
|
| 507 | - |
|
| 508 | - // Is the last argument a boolean? True means required |
|
| 509 | - $required = end($args) === true; |
|
| 510 | - |
|
| 511 | - // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 512 | - $required === true and --$arg_count; |
|
| 513 | - |
|
| 514 | - // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 515 | - switch ($arg_count) { |
|
| 516 | - case 2: |
|
| 517 | - |
|
| 518 | - // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 519 | - if (is_array($args[1])) { |
|
| 520 | - list($output, $options) = $args; |
|
| 521 | - } |
|
| 522 | - // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 523 | - elseif (is_string($args[1])) { |
|
| 524 | - list($output, $default) = $args; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - break; |
|
| 528 | - |
|
| 529 | - case 1: |
|
| 530 | - |
|
| 531 | - // No question (probably been asked already) so just show options |
|
| 532 | - // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 533 | - if (is_array($args[0])) { |
|
| 534 | - $options = $args[0]; |
|
| 535 | - } |
|
| 536 | - // Question without options |
|
| 537 | - // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 538 | - elseif (is_string($args[0])) { |
|
| 539 | - $output = $args[0]; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - break; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - // If a question has been asked with the read |
|
| 546 | - if ($output !== '') { |
|
| 547 | - $extra_output = ''; |
|
| 548 | - |
|
| 549 | - if ($default !== null) { |
|
| 550 | - $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 551 | - } elseif ($options !== []) { |
|
| 552 | - $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - static::fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - // Read the input from keyboard. |
|
| 559 | - $input = trim(static::input()) ?: $default; |
|
| 560 | - |
|
| 561 | - // No input provided and we require one (default will stop this being called) |
|
| 562 | - if (empty($input) and $required === true) { |
|
| 563 | - static::write('This is required.'); |
|
| 564 | - static::newLine(); |
|
| 565 | - |
|
| 566 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - // If options are provided and the choice is not in the array, tell them to try again |
|
| 570 | - if ( ! empty($options) and ! in_array($input, $options)) { |
|
| 571 | - static::write('This is not a valid option. Please try again.'); |
|
| 572 | - static::newLine(); |
|
| 573 | - |
|
| 574 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - return $input; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Allows you to set a commandline option from code. |
|
| 582 | - * |
|
| 583 | - * @param string|int $name The name of the option (int if unnamed) |
|
| 584 | - * @param mixed|null $value The value to set, or null to delete the option |
|
| 585 | - * |
|
| 586 | - * @return mixed |
|
| 587 | - */ |
|
| 588 | - public static function setOption($name, $value = null) |
|
| 589 | - { |
|
| 590 | - if ($value == null) { |
|
| 591 | - if (isset(static::$options[$name])) { |
|
| 592 | - unset(static::$options[$name]); |
|
| 593 | - } |
|
| 594 | - } else { |
|
| 595 | - static::$options[$name] = $value; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Waits a certain number of seconds, optionally showing a wait message and |
|
| 601 | - * waiting for a key press. |
|
| 602 | - * |
|
| 603 | - * @param int $seconds Number of seconds |
|
| 604 | - * @param bool $countdown Show a countdown or not |
|
| 605 | - * |
|
| 606 | - * @return string |
|
| 607 | - */ |
|
| 608 | - public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 609 | - { |
|
| 610 | - if ($countdown === true) { |
|
| 611 | - $time = $seconds; |
|
| 612 | - |
|
| 613 | - while ($time > 0) { |
|
| 614 | - fwrite(static::$stdout, $time.'... '); |
|
| 615 | - sleep(1); |
|
| 616 | - $time--; |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - static::write(); |
|
| 620 | - } else { |
|
| 621 | - if ($seconds = 0) { |
|
| 622 | - sleep($seconds); |
|
| 623 | - } else { |
|
| 624 | - static::write(static::$waitMsg); |
|
| 625 | - static::input(); |
|
| 626 | - } |
|
| 627 | - } |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Outputs a string to the cli. If you send an array it will implode them |
|
| 632 | - * with a line break. |
|
| 633 | - * |
|
| 634 | - * @param string|array $text The text to output, or array of lines |
|
| 635 | - * @param string|null $foreground The foreground color |
|
| 636 | - * @param string|null $background The background color |
|
| 637 | - * |
|
| 638 | - * @return string |
|
| 639 | - */ |
|
| 640 | - public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 641 | - { |
|
| 642 | - if (is_array($text)) { |
|
| 643 | - $text = implode(PHP_EOL, $text); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - if ($foreground OR $background) { |
|
| 647 | - $text = static::color($text, $foreground, $background); |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - static::fwrite(static::$stdout, $text.PHP_EOL); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Returns a well formatted table. |
|
| 655 | - * |
|
| 656 | - * @param array $tbody List of rows |
|
| 657 | - * @param array $thead List of columns |
|
| 658 | - * |
|
| 659 | - * @return void |
|
| 660 | - */ |
|
| 661 | - public static function table(array $tbody, array $thead = []) |
|
| 662 | - { |
|
| 663 | - $rows = []; |
|
| 664 | - |
|
| 665 | - if ( ! empty($thead)) { |
|
| 666 | - $rows[] = array_values($thead); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - foreach ($tbody as $tr) { |
|
| 670 | - $rows[] = count($rows); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - $totalRows = count($rows); |
|
| 674 | - |
|
| 675 | - // Store all columns lengths |
|
| 676 | - $allColsLengths = []; |
|
| 677 | - |
|
| 678 | - // Store maximum lengths by column |
|
| 679 | - $maxColsLengths = []; |
|
| 680 | - |
|
| 681 | - for ($row = 0; $row < $totalRows; $row++) { |
|
| 682 | - $column = 0; |
|
| 683 | - |
|
| 684 | - foreach ($rows[$row] as $col) { |
|
| 685 | - $allColsLengths[$row][$column] = static::strlen($col); |
|
| 686 | - |
|
| 687 | - if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
| 688 | - $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - $column++; |
|
| 692 | - } |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - for ($row = 0; $row < $totalRows; $row++) { |
|
| 696 | - $column = 0; |
|
| 697 | - |
|
| 698 | - foreach ($rows[$row] as $col) |
|
| 699 | - { |
|
| 700 | - $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 265 | + static::fwrite(static::$stderr, $text.PHP_EOL); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Attempts to determine the width of the viewable CLI window. |
|
| 270 | + * |
|
| 271 | + * @param int $default |
|
| 272 | + * |
|
| 273 | + * @return int |
|
| 274 | + */ |
|
| 275 | + public static function getWidth(int $default = 80) |
|
| 276 | + { |
|
| 277 | + if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
| 278 | + return $default; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + return (int) shell_exec('tput cols'); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Attempts to determine the height of the viewable CLI window. |
|
| 286 | + * |
|
| 287 | + * @param int $default |
|
| 288 | + * |
|
| 289 | + * @return int |
|
| 290 | + */ |
|
| 291 | + public static function getHeight(int $default = 32) |
|
| 292 | + { |
|
| 293 | + if (static::isWindows()) { |
|
| 294 | + return $default; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return (int) shell_exec('tput lines'); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
| 302 | + * If no maximum width is specified, will wrap to the window's max. |
|
| 303 | + * |
|
| 304 | + * @param string $string |
|
| 305 | + * @param int $max |
|
| 306 | + * @param int $padLeft |
|
| 307 | + * |
|
| 308 | + * @return string |
|
| 309 | + */ |
|
| 310 | + public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
| 311 | + { |
|
| 312 | + if (empty($string)) { |
|
| 313 | + return ''; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + if ($max === 0) { |
|
| 317 | + $max = static::getWidth(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + if (static::getWidth() < $max) { |
|
| 321 | + $max = static::getWidth(); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + $max = $max - $padLeft; |
|
| 325 | + |
|
| 326 | + $lines = wordwrap($string, $max); |
|
| 327 | + |
|
| 328 | + if ($pad_left > 0) { |
|
| 329 | + $lines = explode(PHP_EOL, $lines); |
|
| 330 | + |
|
| 331 | + $first = true; |
|
| 332 | + |
|
| 333 | + array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 334 | + if ( ! $first) { |
|
| 335 | + $line = str_repeat(' ', $pad_left) . $line; |
|
| 336 | + } else { |
|
| 337 | + $first = false; |
|
| 338 | + } |
|
| 339 | + }); |
|
| 340 | + |
|
| 341 | + $lines = implode(PHP_EOL, $lines); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + return $lines; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Get input from the shell, using readline or the standard STDIN. |
|
| 349 | + * |
|
| 350 | + * @param string|int $prefix The name of the option (int if unnamed) |
|
| 351 | + * |
|
| 352 | + * @return string |
|
| 353 | + */ |
|
| 354 | + public static function input($prefix = '') |
|
| 355 | + { |
|
| 356 | + if (static::$readlineSupport) { |
|
| 357 | + return readline($prefix); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + echo $prefix; |
|
| 361 | + |
|
| 362 | + return fgets(STDIN); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * If operating system === windows. |
|
| 367 | + * |
|
| 368 | + * @return string |
|
| 369 | + */ |
|
| 370 | + public static function isWindows() |
|
| 371 | + { |
|
| 372 | + return stripos(PHP_OS, 'WIN') === 0; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Enter a number of empty lines. |
|
| 377 | + * |
|
| 378 | + * @param int $num Number of lines to output |
|
| 379 | + * |
|
| 380 | + * @return void |
|
| 381 | + */ |
|
| 382 | + public static function newLine(int $num = 1) |
|
| 383 | + { |
|
| 384 | + for ($i = 0; $i < $num; $i++) { |
|
| 385 | + static::write(); |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Returns the option with the given name. You can also give the option number. |
|
| 391 | + * |
|
| 392 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 393 | + * @param mixed $default The value to return if the option is not defined |
|
| 394 | + * |
|
| 395 | + * @return mixed |
|
| 396 | + * |
|
| 397 | + * @uses \Syscodes\Contract\Core\Lenevor |
|
| 398 | + */ |
|
| 399 | + public static function option($name, $default = null) |
|
| 400 | + { |
|
| 401 | + if ( ! isset(static::$options[$name])) { |
|
| 402 | + return Lenevor::value($default); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + return static::$options[$name]; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Parses the command line it was called from and collects all |
|
| 410 | + * options and valid segments. |
|
| 411 | + * |
|
| 412 | + * @return bool |
|
| 413 | + */ |
|
| 414 | + protected static function parseCommandLine() |
|
| 415 | + { |
|
| 416 | + $options = false; |
|
| 417 | + |
|
| 418 | + for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
| 419 | + if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
| 420 | + static::$segments[] = $_SERVER['argv'][$i]; |
|
| 421 | + |
|
| 422 | + continue; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + $options = true; |
|
| 426 | + |
|
| 427 | + $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
| 428 | + $value = null; |
|
| 429 | + |
|
| 430 | + if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
| 431 | + $value = $_SERVER['argv'][$i + 1]; |
|
| 432 | + $i++; |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + static::$options[$args] = $value; |
|
| 436 | + |
|
| 437 | + $options = false; |
|
| 438 | + } |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Returns the command line string portions of the arguments, minus |
|
| 443 | + * any options, as a string. |
|
| 444 | + * |
|
| 445 | + * @return string |
|
| 446 | + */ |
|
| 447 | + public static function getURI() |
|
| 448 | + { |
|
| 449 | + return implode('/', static::$segments); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Returns an individual segment. |
|
| 454 | + * |
|
| 455 | + * @param int $index |
|
| 456 | + * |
|
| 457 | + * @return mixed|null |
|
| 458 | + */ |
|
| 459 | + public static function getSegment(int $index) |
|
| 460 | + { |
|
| 461 | + if ( ! isset(static::$segments[$index - 1])) { |
|
| 462 | + return null; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + return static::$segments[$index - 1]; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * Returns the raw array of segments found. |
|
| 470 | + * |
|
| 471 | + * @return array |
|
| 472 | + */ |
|
| 473 | + public static function getSegments() |
|
| 474 | + { |
|
| 475 | + return static::$segments; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Asks the user for input. This can have either 1 or 2 arguments. |
|
| 480 | + * |
|
| 481 | + * Usage: |
|
| 482 | + * |
|
| 483 | + * // Waits for any key press |
|
| 484 | + * Cli::prompt(); |
|
| 485 | + * |
|
| 486 | + * // Takes any input |
|
| 487 | + * $color = Cli::prompt('What is your favorite color?'); |
|
| 488 | + * |
|
| 489 | + * // Takes any input, but offers default |
|
| 490 | + * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 491 | + * |
|
| 492 | + * // Will only accept the options in the array |
|
| 493 | + * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
| 494 | + * |
|
| 495 | + * @return string The user input |
|
| 496 | + */ |
|
| 497 | + public static function prompt() |
|
| 498 | + { |
|
| 499 | + $args = func_get_args(); |
|
| 500 | + |
|
| 501 | + $options = []; |
|
| 502 | + $output = ''; |
|
| 503 | + $default = null; |
|
| 504 | + |
|
| 505 | + // How many we got |
|
| 506 | + $arg_count = count($args); |
|
| 507 | + |
|
| 508 | + // Is the last argument a boolean? True means required |
|
| 509 | + $required = end($args) === true; |
|
| 510 | + |
|
| 511 | + // Reduce the argument count if required was passed, we don't care about that anymore |
|
| 512 | + $required === true and --$arg_count; |
|
| 513 | + |
|
| 514 | + // This method can take a few crazy combinations of arguments, so lets work it out |
|
| 515 | + switch ($arg_count) { |
|
| 516 | + case 2: |
|
| 517 | + |
|
| 518 | + // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
| 519 | + if (is_array($args[1])) { |
|
| 520 | + list($output, $options) = $args; |
|
| 521 | + } |
|
| 522 | + // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
| 523 | + elseif (is_string($args[1])) { |
|
| 524 | + list($output, $default) = $args; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + break; |
|
| 528 | + |
|
| 529 | + case 1: |
|
| 530 | + |
|
| 531 | + // No question (probably been asked already) so just show options |
|
| 532 | + // E.g: $ready = Cli::prompt(array('y','n')); |
|
| 533 | + if (is_array($args[0])) { |
|
| 534 | + $options = $args[0]; |
|
| 535 | + } |
|
| 536 | + // Question without options |
|
| 537 | + // E.g: $ready = Cli::prompt('What did you do today?'); |
|
| 538 | + elseif (is_string($args[0])) { |
|
| 539 | + $output = $args[0]; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + break; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + // If a question has been asked with the read |
|
| 546 | + if ($output !== '') { |
|
| 547 | + $extra_output = ''; |
|
| 548 | + |
|
| 549 | + if ($default !== null) { |
|
| 550 | + $extra_output = ' [ Default: "'.$default.'" ]'; |
|
| 551 | + } elseif ($options !== []) { |
|
| 552 | + $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + static::fwrite(static::$stdout, $output.$extra_output.': '); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + // Read the input from keyboard. |
|
| 559 | + $input = trim(static::input()) ?: $default; |
|
| 560 | + |
|
| 561 | + // No input provided and we require one (default will stop this being called) |
|
| 562 | + if (empty($input) and $required === true) { |
|
| 563 | + static::write('This is required.'); |
|
| 564 | + static::newLine(); |
|
| 565 | + |
|
| 566 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + // If options are provided and the choice is not in the array, tell them to try again |
|
| 570 | + if ( ! empty($options) and ! in_array($input, $options)) { |
|
| 571 | + static::write('This is not a valid option. Please try again.'); |
|
| 572 | + static::newLine(); |
|
| 573 | + |
|
| 574 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + return $input; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Allows you to set a commandline option from code. |
|
| 582 | + * |
|
| 583 | + * @param string|int $name The name of the option (int if unnamed) |
|
| 584 | + * @param mixed|null $value The value to set, or null to delete the option |
|
| 585 | + * |
|
| 586 | + * @return mixed |
|
| 587 | + */ |
|
| 588 | + public static function setOption($name, $value = null) |
|
| 589 | + { |
|
| 590 | + if ($value == null) { |
|
| 591 | + if (isset(static::$options[$name])) { |
|
| 592 | + unset(static::$options[$name]); |
|
| 593 | + } |
|
| 594 | + } else { |
|
| 595 | + static::$options[$name] = $value; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Waits a certain number of seconds, optionally showing a wait message and |
|
| 601 | + * waiting for a key press. |
|
| 602 | + * |
|
| 603 | + * @param int $seconds Number of seconds |
|
| 604 | + * @param bool $countdown Show a countdown or not |
|
| 605 | + * |
|
| 606 | + * @return string |
|
| 607 | + */ |
|
| 608 | + public static function wait(int $seconds = 0, bool $countdown = false) |
|
| 609 | + { |
|
| 610 | + if ($countdown === true) { |
|
| 611 | + $time = $seconds; |
|
| 612 | + |
|
| 613 | + while ($time > 0) { |
|
| 614 | + fwrite(static::$stdout, $time.'... '); |
|
| 615 | + sleep(1); |
|
| 616 | + $time--; |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + static::write(); |
|
| 620 | + } else { |
|
| 621 | + if ($seconds = 0) { |
|
| 622 | + sleep($seconds); |
|
| 623 | + } else { |
|
| 624 | + static::write(static::$waitMsg); |
|
| 625 | + static::input(); |
|
| 626 | + } |
|
| 627 | + } |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * Outputs a string to the cli. If you send an array it will implode them |
|
| 632 | + * with a line break. |
|
| 633 | + * |
|
| 634 | + * @param string|array $text The text to output, or array of lines |
|
| 635 | + * @param string|null $foreground The foreground color |
|
| 636 | + * @param string|null $background The background color |
|
| 637 | + * |
|
| 638 | + * @return string |
|
| 639 | + */ |
|
| 640 | + public static function write(string $text = '', string $foreground = null, string $background = null) |
|
| 641 | + { |
|
| 642 | + if (is_array($text)) { |
|
| 643 | + $text = implode(PHP_EOL, $text); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + if ($foreground OR $background) { |
|
| 647 | + $text = static::color($text, $foreground, $background); |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + static::fwrite(static::$stdout, $text.PHP_EOL); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Returns a well formatted table. |
|
| 655 | + * |
|
| 656 | + * @param array $tbody List of rows |
|
| 657 | + * @param array $thead List of columns |
|
| 658 | + * |
|
| 659 | + * @return void |
|
| 660 | + */ |
|
| 661 | + public static function table(array $tbody, array $thead = []) |
|
| 662 | + { |
|
| 663 | + $rows = []; |
|
| 664 | + |
|
| 665 | + if ( ! empty($thead)) { |
|
| 666 | + $rows[] = array_values($thead); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + foreach ($tbody as $tr) { |
|
| 670 | + $rows[] = count($rows); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + $totalRows = count($rows); |
|
| 674 | + |
|
| 675 | + // Store all columns lengths |
|
| 676 | + $allColsLengths = []; |
|
| 677 | + |
|
| 678 | + // Store maximum lengths by column |
|
| 679 | + $maxColsLengths = []; |
|
| 680 | + |
|
| 681 | + for ($row = 0; $row < $totalRows; $row++) { |
|
| 682 | + $column = 0; |
|
| 683 | + |
|
| 684 | + foreach ($rows[$row] as $col) { |
|
| 685 | + $allColsLengths[$row][$column] = static::strlen($col); |
|
| 686 | + |
|
| 687 | + if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
| 688 | + $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + $column++; |
|
| 692 | + } |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + for ($row = 0; $row < $totalRows; $row++) { |
|
| 696 | + $column = 0; |
|
| 697 | + |
|
| 698 | + foreach ($rows[$row] as $col) |
|
| 699 | + { |
|
| 700 | + $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
| 701 | 701 | |
| 702 | - if ($diverse) { |
|
| 703 | - $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - $column++; |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - $table = ''; |
|
| 711 | - $cols = ''; |
|
| 712 | - |
|
| 713 | - for ($row = 0; $row < $rows; $row++) { |
|
| 714 | - if (0 === $row) { |
|
| 715 | - $cols = '+'; |
|
| 716 | - |
|
| 717 | - foreach ($rows[$row] as $col) { |
|
| 718 | - $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - $table .= $cols.PHP_EOL; |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 725 | - |
|
| 726 | - if (0 === $row && ( ! empty($thead)) || ($row + 1) === $rows) { |
|
| 727 | - $table .= $cols.PHP_EOL; |
|
| 728 | - } |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - static::write($table); |
|
| 732 | - } |
|
| 733 | - |
|
| 734 | - /** |
|
| 735 | - * The library is intended for used on Cli commanda, |
|
| 736 | - * this commands can be called from controllers and |
|
| 737 | - * elsewhere of framework. |
|
| 738 | - * |
|
| 739 | - * @param resource $handle |
|
| 740 | - * @param string $text |
|
| 741 | - * |
|
| 742 | - * @return string |
|
| 743 | - */ |
|
| 744 | - protected static function fwrite($handle, string $text) |
|
| 745 | - { |
|
| 746 | - if (isCli()) { |
|
| 747 | - fwrite($handle, $text); |
|
| 748 | - return; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - echo $text; |
|
| 752 | - } |
|
| 702 | + if ($diverse) { |
|
| 703 | + $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + $column++; |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + $table = ''; |
|
| 711 | + $cols = ''; |
|
| 712 | + |
|
| 713 | + for ($row = 0; $row < $rows; $row++) { |
|
| 714 | + if (0 === $row) { |
|
| 715 | + $cols = '+'; |
|
| 716 | + |
|
| 717 | + foreach ($rows[$row] as $col) { |
|
| 718 | + $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + $table .= $cols.PHP_EOL; |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
| 725 | + |
|
| 726 | + if (0 === $row && ( ! empty($thead)) || ($row + 1) === $rows) { |
|
| 727 | + $table .= $cols.PHP_EOL; |
|
| 728 | + } |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + static::write($table); |
|
| 732 | + } |
|
| 733 | + |
|
| 734 | + /** |
|
| 735 | + * The library is intended for used on Cli commanda, |
|
| 736 | + * this commands can be called from controllers and |
|
| 737 | + * elsewhere of framework. |
|
| 738 | + * |
|
| 739 | + * @param resource $handle |
|
| 740 | + * @param string $text |
|
| 741 | + * |
|
| 742 | + * @return string |
|
| 743 | + */ |
|
| 744 | + protected static function fwrite($handle, string $text) |
|
| 745 | + { |
|
| 746 | + if (isCli()) { |
|
| 747 | + fwrite($handle, $text); |
|
| 748 | + return; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + echo $text; |
|
| 752 | + } |
|
| 753 | 753 | } |
| 754 | 754 | \ No newline at end of file |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 201 | + if ($background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
| 202 | 202 | throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
| 203 | 203 | } |
| 204 | 204 | |
@@ -330,9 +330,9 @@ discard block |
||
| 330 | 330 | |
| 331 | 331 | $first = true; |
| 332 | 332 | |
| 333 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
| 333 | + array_walk($lines, function(&$line, $index) use ($pad_left, &$first) { |
|
| 334 | 334 | if ( ! $first) { |
| 335 | - $line = str_repeat(' ', $pad_left) . $line; |
|
| 335 | + $line = str_repeat(' ', $pad_left).$line; |
|
| 336 | 336 | } else { |
| 337 | 337 | $first = false; |
| 338 | 338 | } |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | } |
| 709 | 709 | |
| 710 | 710 | $table = ''; |
| 711 | - $cols = ''; |
|
| 711 | + $cols = ''; |
|
| 712 | 712 | |
| 713 | 713 | for ($row = 0; $row < $rows; $row++) { |
| 714 | 714 | if (0 === $row) { |
@@ -34,58 +34,58 @@ |
||
| 34 | 34 | */ |
| 35 | 35 | class Application implements ApplicationContracts |
| 36 | 36 | { |
| 37 | - /** |
|
| 38 | - * Get the Lenevor class instance. |
|
| 39 | - * |
|
| 40 | - * @var \Syscodes\Contracts\Core\Lenevor $core |
|
| 41 | - */ |
|
| 42 | - protected $core; |
|
| 37 | + /** |
|
| 38 | + * Get the Lenevor class instance. |
|
| 39 | + * |
|
| 40 | + * @var \Syscodes\Contracts\Core\Lenevor $core |
|
| 41 | + */ |
|
| 42 | + protected $core; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Console constructor. Initialize the console of Lenevor. |
|
| 46 | - * |
|
| 47 | - * @param \Syscodes\Contracts\Core\Lenevor $core |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function __construct(Lenevor $core) |
|
| 52 | - { |
|
| 53 | - // Initialize the Cli |
|
| 54 | - if (isCli()) { |
|
| 55 | - Cli::initialize(); |
|
| 56 | - } |
|
| 44 | + /** |
|
| 45 | + * Console constructor. Initialize the console of Lenevor. |
|
| 46 | + * |
|
| 47 | + * @param \Syscodes\Contracts\Core\Lenevor $core |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function __construct(Lenevor $core) |
|
| 52 | + { |
|
| 53 | + // Initialize the Cli |
|
| 54 | + if (isCli()) { |
|
| 55 | + Cli::initialize(); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $this->core = $core; |
|
| 59 | - } |
|
| 58 | + $this->core = $core; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Runs the current command discovered on the CLI. |
|
| 63 | - * |
|
| 64 | - * @return void |
|
| 65 | - */ |
|
| 66 | - public function run() |
|
| 67 | - { |
|
| 68 | - Cli::getURI(); |
|
| 61 | + /** |
|
| 62 | + * Runs the current command discovered on the CLI. |
|
| 63 | + * |
|
| 64 | + * @return void |
|
| 65 | + */ |
|
| 66 | + public function run() |
|
| 67 | + { |
|
| 68 | + Cli::getURI(); |
|
| 69 | 69 | |
| 70 | 70 | |
| 71 | - } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Displays basic information about the Console. |
|
| 75 | - * |
|
| 76 | - * @return void |
|
| 77 | - * |
|
| 78 | - * @uses Version::PRODUCT |
|
| 79 | - * @uses Version::RELEASE |
|
| 80 | - */ |
|
| 81 | - public function showHeader() |
|
| 82 | - { |
|
| 83 | - Cli::write(Version::PRODUCT.' ' |
|
| 84 | - .Cli::color(Version::RELEASE, 'cyan').' | ' |
|
| 85 | - .'Server Time: '.Cli::color(date('Y/m/d H:i:sa'), 'light_yellow').' | ' |
|
| 86 | - .cli::color('['.PHP_OS.']', 'light_purple') |
|
| 87 | - ); |
|
| 73 | + /** |
|
| 74 | + * Displays basic information about the Console. |
|
| 75 | + * |
|
| 76 | + * @return void |
|
| 77 | + * |
|
| 78 | + * @uses Version::PRODUCT |
|
| 79 | + * @uses Version::RELEASE |
|
| 80 | + */ |
|
| 81 | + public function showHeader() |
|
| 82 | + { |
|
| 83 | + Cli::write(Version::PRODUCT.' ' |
|
| 84 | + .Cli::color(Version::RELEASE, 'cyan').' | ' |
|
| 85 | + .'Server Time: '.Cli::color(date('Y/m/d H:i:sa'), 'light_yellow').' | ' |
|
| 86 | + .cli::color('['.PHP_OS.']', 'light_purple') |
|
| 87 | + ); |
|
| 88 | 88 | |
| 89 | - Cli::newLine(1); |
|
| 90 | - } |
|
| 89 | + Cli::newLine(1); |
|
| 90 | + } |
|
| 91 | 91 | } |
| 92 | 92 | \ No newline at end of file |
@@ -38,163 +38,163 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class Lenevor implements LenevorContract |
| 40 | 40 | { |
| 41 | - /** |
|
| 42 | - * The application implementation. |
|
| 43 | - * |
|
| 44 | - * @var \Syscodes\Contracts\Core\Application $app |
|
| 45 | - */ |
|
| 46 | - protected $app; |
|
| 41 | + /** |
|
| 42 | + * The application implementation. |
|
| 43 | + * |
|
| 44 | + * @var \Syscodes\Contracts\Core\Application $app |
|
| 45 | + */ |
|
| 46 | + protected $app; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * The bootstrap classes for the application. |
|
| 50 | - * |
|
| 51 | - * @var array $bootstrappers |
|
| 52 | - */ |
|
| 53 | - protected $bootstrappers = [ |
|
| 54 | - \Syscodes\Core\Bootstrap\BootDetectEnvironment::class, |
|
| 55 | - \Syscodes\Core\Bootstrap\BootConfiguration::class, |
|
| 56 | - \Syscodes\Core\Bootstrap\BootHandleExceptions::class, |
|
| 57 | - \Syscodes\Core\Bootstrap\BootRegisterFacades::class, |
|
| 58 | - \Syscodes\Core\Bootstrap\BootRegisterProviders::class, |
|
| 59 | - \Syscodes\Core\Bootstrap\BootProviders::class, |
|
| 60 | - ]; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Get the application's middleware. |
|
| 64 | - * |
|
| 65 | - * @var array $middleware |
|
| 66 | - */ |
|
| 67 | - protected $middleware = []; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The router instance. |
|
| 71 | - * |
|
| 72 | - * @var \Syscodes\Routing\Router $router |
|
| 73 | - */ |
|
| 74 | - protected $router; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Total app execution time. |
|
| 78 | - * |
|
| 79 | - * @var float $totalTime |
|
| 80 | - */ |
|
| 81 | - protected $totalTime; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Constructor. Lenevor class instance. |
|
| 85 | - * |
|
| 86 | - * @param \Syscodes\Contracts\Core\Application $app |
|
| 87 | - * @param \Syscodes\Routing\Router $router |
|
| 88 | - * |
|
| 89 | - * @return void |
|
| 90 | - */ |
|
| 91 | - public function __construct(Application $app, Router $router) |
|
| 92 | - { |
|
| 93 | - $this->app = $app; |
|
| 94 | - $this->router = $router; |
|
| 95 | - } |
|
| 48 | + /** |
|
| 49 | + * The bootstrap classes for the application. |
|
| 50 | + * |
|
| 51 | + * @var array $bootstrappers |
|
| 52 | + */ |
|
| 53 | + protected $bootstrappers = [ |
|
| 54 | + \Syscodes\Core\Bootstrap\BootDetectEnvironment::class, |
|
| 55 | + \Syscodes\Core\Bootstrap\BootConfiguration::class, |
|
| 56 | + \Syscodes\Core\Bootstrap\BootHandleExceptions::class, |
|
| 57 | + \Syscodes\Core\Bootstrap\BootRegisterFacades::class, |
|
| 58 | + \Syscodes\Core\Bootstrap\BootRegisterProviders::class, |
|
| 59 | + \Syscodes\Core\Bootstrap\BootProviders::class, |
|
| 60 | + ]; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Get the application's middleware. |
|
| 64 | + * |
|
| 65 | + * @var array $middleware |
|
| 66 | + */ |
|
| 67 | + protected $middleware = []; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The router instance. |
|
| 71 | + * |
|
| 72 | + * @var \Syscodes\Routing\Router $router |
|
| 73 | + */ |
|
| 74 | + protected $router; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Total app execution time. |
|
| 78 | + * |
|
| 79 | + * @var float $totalTime |
|
| 80 | + */ |
|
| 81 | + protected $totalTime; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Constructor. Lenevor class instance. |
|
| 85 | + * |
|
| 86 | + * @param \Syscodes\Contracts\Core\Application $app |
|
| 87 | + * @param \Syscodes\Routing\Router $router |
|
| 88 | + * |
|
| 89 | + * @return void |
|
| 90 | + */ |
|
| 91 | + public function __construct(Application $app, Router $router) |
|
| 92 | + { |
|
| 93 | + $this->app = $app; |
|
| 94 | + $this->router = $router; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Initializes the framework, this can only be called once. |
|
| 99 | - * Launch the application. |
|
| 100 | - * |
|
| 101 | - * @param \Syscodes\http\Request $request |
|
| 102 | - * |
|
| 103 | - * @return \Syscodes\Http\Response |
|
| 104 | - */ |
|
| 105 | - public function handle($request) |
|
| 106 | - { |
|
| 107 | - try { |
|
| 108 | - $response = $this->sendRequestThroughRouter($request); |
|
| 109 | - } catch (Throwable $e) { |
|
| 110 | - $this->reportException($e); |
|
| 111 | - |
|
| 112 | - $response = $this->renderException($request, $e); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $response; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Send the given request through the router. |
|
| 120 | - * |
|
| 121 | - * @param \Syscodes\Http\Request $request |
|
| 122 | - * |
|
| 123 | - * @return \Syscodes\Http\Response |
|
| 124 | - */ |
|
| 125 | - protected function sendRequestThroughRouter($request) |
|
| 126 | - { |
|
| 127 | - $this->app->instance('request', $request); |
|
| 128 | - |
|
| 129 | - Facade::clearResolvedInstance('request'); |
|
| 130 | - |
|
| 131 | - // Load configuration system |
|
| 132 | - $this->bootstrap(); |
|
| 133 | - |
|
| 134 | - return (new Pipeline($this->app)) |
|
| 135 | - ->send($request) |
|
| 136 | - ->through($this->app->skipGoingMiddleware() ? [] : $this->middleware) |
|
| 137 | - ->then($this->dispatchToRouter()); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Bootstrap the application for HTTP requests. |
|
| 142 | - * |
|
| 143 | - * @return void |
|
| 144 | - */ |
|
| 145 | - protected function bootstrap() |
|
| 146 | - { |
|
| 147 | - if ( ! $this->app->hasBeenBootstrapped()) { |
|
| 148 | - $this->app->bootstrapWith($this->bootstrappers()); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Get the bootstrap classes for the application. |
|
| 154 | - * |
|
| 155 | - * @return array |
|
| 156 | - */ |
|
| 157 | - protected function bootstrappers() |
|
| 158 | - { |
|
| 159 | - return $this->bootstrappers; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the dispatcher of routes. |
|
| 164 | - * |
|
| 165 | - * @return \Closure |
|
| 166 | - */ |
|
| 167 | - protected function dispatchToRouter() |
|
| 168 | - { |
|
| 169 | - return function ($request) { |
|
| 170 | - $this->app->instance('request', $request); |
|
| 171 | - |
|
| 172 | - return $this->router->dispatch($request); |
|
| 173 | - }; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Report the exception to the exception handler. |
|
| 178 | - * |
|
| 179 | - * @param \Throwable $e |
|
| 180 | - * |
|
| 181 | - * @return void |
|
| 182 | - */ |
|
| 183 | - protected function reportException(Throwable $e) |
|
| 184 | - { |
|
| 185 | - return $this->app[ExceptionHandler::class]->report($e); |
|
| 186 | - } |
|
| 97 | + /** |
|
| 98 | + * Initializes the framework, this can only be called once. |
|
| 99 | + * Launch the application. |
|
| 100 | + * |
|
| 101 | + * @param \Syscodes\http\Request $request |
|
| 102 | + * |
|
| 103 | + * @return \Syscodes\Http\Response |
|
| 104 | + */ |
|
| 105 | + public function handle($request) |
|
| 106 | + { |
|
| 107 | + try { |
|
| 108 | + $response = $this->sendRequestThroughRouter($request); |
|
| 109 | + } catch (Throwable $e) { |
|
| 110 | + $this->reportException($e); |
|
| 111 | + |
|
| 112 | + $response = $this->renderException($request, $e); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return $response; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Send the given request through the router. |
|
| 120 | + * |
|
| 121 | + * @param \Syscodes\Http\Request $request |
|
| 122 | + * |
|
| 123 | + * @return \Syscodes\Http\Response |
|
| 124 | + */ |
|
| 125 | + protected function sendRequestThroughRouter($request) |
|
| 126 | + { |
|
| 127 | + $this->app->instance('request', $request); |
|
| 128 | + |
|
| 129 | + Facade::clearResolvedInstance('request'); |
|
| 130 | + |
|
| 131 | + // Load configuration system |
|
| 132 | + $this->bootstrap(); |
|
| 133 | + |
|
| 134 | + return (new Pipeline($this->app)) |
|
| 135 | + ->send($request) |
|
| 136 | + ->through($this->app->skipGoingMiddleware() ? [] : $this->middleware) |
|
| 137 | + ->then($this->dispatchToRouter()); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Bootstrap the application for HTTP requests. |
|
| 142 | + * |
|
| 143 | + * @return void |
|
| 144 | + */ |
|
| 145 | + protected function bootstrap() |
|
| 146 | + { |
|
| 147 | + if ( ! $this->app->hasBeenBootstrapped()) { |
|
| 148 | + $this->app->bootstrapWith($this->bootstrappers()); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Get the bootstrap classes for the application. |
|
| 154 | + * |
|
| 155 | + * @return array |
|
| 156 | + */ |
|
| 157 | + protected function bootstrappers() |
|
| 158 | + { |
|
| 159 | + return $this->bootstrappers; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the dispatcher of routes. |
|
| 164 | + * |
|
| 165 | + * @return \Closure |
|
| 166 | + */ |
|
| 167 | + protected function dispatchToRouter() |
|
| 168 | + { |
|
| 169 | + return function ($request) { |
|
| 170 | + $this->app->instance('request', $request); |
|
| 171 | + |
|
| 172 | + return $this->router->dispatch($request); |
|
| 173 | + }; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Report the exception to the exception handler. |
|
| 178 | + * |
|
| 179 | + * @param \Throwable $e |
|
| 180 | + * |
|
| 181 | + * @return void |
|
| 182 | + */ |
|
| 183 | + protected function reportException(Throwable $e) |
|
| 184 | + { |
|
| 185 | + return $this->app[ExceptionHandler::class]->report($e); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Render the exception to a response. |
|
| 190 | - * |
|
| 191 | - * @param \Syscodes\Http\Request $request |
|
| 192 | - * @param \Throwable $e |
|
| 193 | - * |
|
| 194 | - * @return \Syscodes\Http\Response |
|
| 195 | - */ |
|
| 196 | - protected function renderException($request, Throwable $e) |
|
| 197 | - { |
|
| 198 | - return $this->app[ExceptionHandler::class]->render($request, $e); |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | 188 | \ No newline at end of file |
| 189 | + /** |
|
| 190 | + * Render the exception to a response. |
|
| 191 | + * |
|
| 192 | + * @param \Syscodes\Http\Request $request |
|
| 193 | + * @param \Throwable $e |
|
| 194 | + * |
|
| 195 | + * @return \Syscodes\Http\Response |
|
| 196 | + */ |
|
| 197 | + protected function renderException($request, Throwable $e) |
|
| 198 | + { |
|
| 199 | + return $this->app[ExceptionHandler::class]->render($request, $e); |
|
| 200 | + } |
|
| 201 | + } |
|
| 202 | 202 | \ No newline at end of file |