Lines
78.57%
33 / 42
Methods
33.33%
2 / 6
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| getParam | 80.00% | 8 / 10 | 0.00% | 0 / 1 | 6.29 | ||
| getParams | 0.00% | 0 / 5 | 0.00% | 0 / 1 | 12 | ||
| getCookieParam | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | ||
| getBasePath | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| getBaseUrl | 87.50% | 7 / 8 | 0.00% | 0 / 1 | 5.05 | ||
| resolveBasePath | 92.30% | 12 / 13 | 0.00% | 0 / 1 | 7.02 | ||
| 14 | class Request extends \Slim\Psr7\Request | |
| 15 | { | |
| 16 | /** | |
| 17 | * Fetch request parameter value from body or query string (in that order). | |
| 18 | * | |
| 19 | * Note: This method is not part of the PSR-7 standard. | |
| 20 | * | |
| 21 | * @param string $key The parameter key. | |
| 22 | * @param mixed $default The default value. | |
| 23 | * | |
| 24 | * @return mixed | |
| 25 | * @psalm-api | |
| 26 | */ | |
| 27 | public function getParam(string $key, $default = null) | |
| 28 | { | |
| 29 | $postParams = $this->getParsedBody(); | |
| 30 | $getParams = $this->getQueryParams(); | |
| 31 | $result = $default; | |
| 32 | if (is_array($postParams) && isset($postParams[$key])) { | |
| 33 | $result = $postParams[$key]; | |
| 34 | } elseif (is_object($postParams) && property_exists($postParams, $key)) { | |
| 35 | $result = $postParams->$key; | |
| 36 | } elseif (isset($getParams[$key])) { | |
| 37 | $result = $getParams[$key]; | |
| 38 | } | |
| 39 | ||
| 40 | return $result; | |
| 41 | } | |
| 42 | ||
| 43 | /** | |
| 44 | * @deprecated | |
| 45 | * please use explicitly the query params or parsed request body params | |
| 46 | * | |
| 47 | * @return mixed[] | |
| 48 | * @psalm-api | |
| 49 | */ | |
| 50 | public function getParams(): array | |
| 51 | { | |
| 52 | $params = $this->getQueryParams(); | |
| 53 | $postParams = $this->getParsedBody(); | |
| 54 | if (is_array($postParams) || is_object($postParams)) { | |
| 55 | $params = array_replace($params, (array)$postParams); | |
| 56 | } | |
| 57 | ||
| 58 | return $params; | |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Fetch cookie value from cookies sent by the client to the server. | |
| 63 | * | |
| 64 | * Note: This method is not part of the PSR-7 standard. | |
| 65 | * | |
| 66 | * @param string $key The attribute name. | |
| 67 | * @param mixed $default Default value to return if the attribute does not exist. | |
| 68 | * | |
| 69 | * @return mixed | |
| 70 | * @psalm-api | |
| 71 | */ | |
| 72 | public function getCookieParam(string $key, $default = null) | |
| 73 | { | |
| 74 | $cookies = $this->getCookieParams(); | |
| 75 | $result = $default; | |
| 76 | if (isset($cookies[$key])) { | |
| 77 | $result = $cookies[$key]; | |
| 78 | } | |
| 79 | ||
| 80 | return $result; | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * @deprecated (use SlimApp::getBasePath() or resolve a named route instead) | |
| 85 | * @return string | |
| 86 | * @psalm-api | |
| 87 | */ | |
| 88 | public function getBasePath(): string | |
| 89 | { | |
| 90 | return $this->resolveBasePath(); | |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Return the fully qualified base URL. | |
| 95 | * | |
| 96 | * Note that this method never includes a trailing / | |
| 97 | * | |
| 98 | * This method is not part of PSR-7. | |
| 99 | * | |
| 100 | * @deprecated | |
| 101 | * @return string | |
| 102 | * @psalm-api | |
| 103 | */ | |
| 104 | public function getBaseUrl(): string | |
| 105 | { | |
| 106 | $scheme = $this->getUri()->getScheme(); | |
| 107 | $authority = $this->getUri()->getAuthority(); | |
| 108 | $basePath = $this->resolveBasePath(); | |
| 109 | ||
| 110 | if ($authority !== '' && substr($basePath, 0, 1) !== '/') { | |
| 111 | $basePath = $basePath . '/' . $basePath; | |
| 112 | } | |
| 113 | ||
| 114 | return ($scheme !== '' ? $scheme . ':' : '') | |
| 115 | . ($authority !== '' ? '//' . $authority : '') | |
| 116 | . rtrim($basePath, '/'); | |
| 117 | } | |
| 118 | ||
| 119 | private function resolveBasePath(): string | |
| 120 | { | |
| 121 | $envBasePath = getenv('ZMS_MODULE_BASEPATH'); | |
| 122 | $basePath = $envBasePath !== false ? $envBasePath : ''; | |
| 123 | if ($basePath === '') { | |
| 124 | $serverParams = $this->getServerParams(); | |
| 125 | $requestUri = $serverParams['REQUEST_URI'] ?? null; | |
| 126 | $scriptName = $serverParams['SCRIPT_NAME'] ?? null; | |
| 127 | if (!is_string($requestUri) || !is_string($scriptName)) { | |
| 128 | return $basePath; | |
| 129 | } | |
| 130 | ||
| 131 | while ( | |
| 132 | min(strlen($requestUri), strlen($scriptName)) > strlen($basePath) | |
| 133 | && strncmp($requestUri, $scriptName, strlen($basePath) + 1) === 0 | |
| 134 | ) { | |
| 135 | $nextPath = substr($requestUri, 0, strlen($basePath) + 1); | |
| 136 | $basePath = $nextPath; | |
| 137 | } | |
| 138 | } | |
| 139 | ||
| 140 | return rtrim($basePath, '/'); | |
| 141 | } | |
| 142 | } |