bytic /
http
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Nip\Http; |
||
| 4 | |||
| 5 | use ByTIC\RequestDetective\RequestDetective; |
||
| 6 | use Nip\Http\Request\Http; |
||
| 7 | use Psr\Http\Message\ServerRequestInterface; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class Request |
||
| 11 | * @package Nip |
||
| 12 | */ |
||
| 13 | class Request extends \Symfony\Component\HttpFoundation\Request implements \ArrayAccess, ServerRequestInterface |
||
| 14 | { |
||
| 15 | use Traits\InteractsWithContentTypes; |
||
| 16 | use Traits\InteractsWithInput; |
||
| 17 | |||
| 18 | use Request\Traits\ArrayAccessTrait; |
||
| 19 | use Request\Traits\HasInterface; |
||
| 20 | use Request\Traits\HasJsonParameterBag; |
||
| 21 | use Request\Traits\PsrBridgeTrait; |
||
| 22 | use Request\Traits\InteractsWithUri; |
||
| 23 | use Request\Traits\InteractsWithMca; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var Http |
||
| 27 | */ |
||
| 28 | protected $http; |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Singleton |
||
| 33 | * |
||
| 34 | * @param null $newInstance |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 35 | * @return static |
||
| 36 | */ |
||
| 37 | public static function instance($newInstance = null) |
||
| 38 | { |
||
| 39 | static $instance; |
||
| 40 | |||
| 41 | if ($newInstance instanceof self) { |
||
| 42 | $instance = $newInstance; |
||
| 43 | } |
||
| 44 | |||
| 45 | if (!($instance instanceof self)) { |
||
| 46 | $instance = new self(); |
||
| 47 | } |
||
| 48 | |||
| 49 | return $instance; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Set an action parameter |
||
| 54 | * |
||
| 55 | * A $value of null will unset the $key if it exists |
||
| 56 | * |
||
| 57 | * @param string $key |
||
| 58 | * @param mixed $value |
||
| 59 | * @return self |
||
| 60 | */ |
||
| 61 | 1 | public function setAttribute($key, $value) |
|
| 62 | { |
||
| 63 | 1 | $this->attributes->set($key, $value); |
|
| 64 | |||
| 65 | 1 | return $this; |
|
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param array $params |
||
| 70 | * @return $this |
||
| 71 | */ |
||
| 72 | public function setParams(array $params) |
||
| 73 | { |
||
| 74 | foreach ($params as $param => $value) { |
||
| 75 | $this->{$param} = $value; |
||
| 76 | } |
||
| 77 | |||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns Http object |
||
| 83 | * @return Http |
||
| 84 | */ |
||
| 85 | 8 | public function getHttp() |
|
| 86 | { |
||
| 87 | 8 | if (!$this->http) { |
|
| 88 | 8 | $this->http = new Http(); |
|
| 89 | 8 | $this->http->setRequest($this); |
|
| 90 | } |
||
| 91 | |||
| 92 | 8 | return $this->http; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Checks if requested URI matches $url parameter, |
||
| 97 | * and redirects to $url if not |
||
| 98 | * |
||
| 99 | * @param string $url |
||
| 100 | * @param int $code |
||
| 101 | */ |
||
| 102 | public function checkURL($url, $code = 302) |
||
| 103 | { |
||
| 104 | $components = parse_url($url); |
||
| 105 | $request = parse_url($_SERVER['REQUEST_URI']); |
||
| 106 | |||
| 107 | if ($components['path'] != $request['path']) { |
||
| 108 | $redirect = $url . ($request['query'] ? '?' . $request['query'] : ''); |
||
| 109 | |||
| 110 | header("Location: " . $redirect, true, $code); |
||
| 111 | exit(); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param $url |
||
| 117 | */ |
||
| 118 | public function redirect($url) |
||
| 119 | { |
||
| 120 | header("Location: " . $url); |
||
| 121 | exit(); |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param bool $action |
||
| 126 | * @param bool $controller |
||
| 127 | * @param bool $module |
||
| 128 | * @param array $params |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | 1 | public function duplicateWithParams($action = false, $controller = false, $module = false, $params = []) |
|
| 132 | { |
||
| 133 | /** @var self $newRequest */ |
||
| 134 | 1 | $newRequest = $this->duplicate(); |
|
| 135 | 1 | $newRequest->setActionName($action); |
|
| 136 | 1 | $newRequest->setControllerName($controller); |
|
| 137 | 1 | $newRequest->setModuleName($module); |
|
| 138 | 1 | $newRequest->attributes->add($params); |
|
| 139 | |||
| 140 | 1 | return $newRequest; |
|
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | 1 | public function isMalicious() |
|
| 147 | { |
||
| 148 | 1 | return RequestDetective::isMalicious($this); |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the current encoded path info for the request. |
||
| 153 | * |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | public function decodedPath() |
||
| 157 | { |
||
| 158 | return rawurldecode($this->path()); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the current path info for the request. |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function path() |
||
| 167 | { |
||
| 168 | $pattern = trim($this->getPathInfo(), '/'); |
||
| 169 | return $pattern == '' ? '/' : '/' . $pattern; |
||
| 170 | } |
||
| 171 | } |
||
| 172 |