|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Simples\Http; |
| 4 | + |
| 5 | +use Simples\Kernel\App; |
| 6 | +use Simples\Route\Match; |
| 7 | + |
| 8 | +/** |
| 9 | + * @method Response answerContinue($data = null, $meta = []) // 100 |
| 10 | + * @method Response answerSwitching Protocols($data = null, $meta = []) // 101 |
| 11 | + * @method Response answerProcessing($data = null, $meta = []) // 102 |
| 12 | + * |
| 13 | + * @method Response answerOK($data = null, $meta = []) // 200 |
| 14 | + * @method Response answerCreated($data = null, $meta = []) // 201 |
| 15 | + * @method Response answerAccepted($data = null, $meta = []) // 202 |
| 16 | + * @method Response answerNonAuthoritativeInformation($data = null, $meta = []) // 203 |
| 17 | + * @method Response answerNoContent($data = null, $meta = []) // 204 |
| 18 | + * @method Response answerResetContent($data = null, $meta = []) // 205 |
| 19 | + * @method Response answerPartialContent($data = null, $meta = []) // 206 |
| 20 | + * @method Response answerMultiStatus($data = null, $meta = []) // 207 |
| 21 | + * @method Response answerAlreadyReported($data = null, $meta = []) // 208 |
| 22 | + * |
| 23 | + * @method Response answerMultiple Choices($data = null, $meta = []) // 300 |
| 24 | + * @method Response answerMoved Permanently($data = null, $meta = []) // 301 |
| 25 | + * @method Response answerFound($data = null, $meta = []) // 302 |
| 26 | + * @method Response answerSeeOther($data = null, $meta = []) // 303 |
| 27 | + * @method Response answerNotModified($data = null, $meta = []) // 304 |
| 28 | + * @method Response answerUseProxy($data = null, $meta = []) // 305 |
| 29 | + * @method Response answerTemporaryRedirect($data = null, $meta = []) // 307 |
| 30 | + * |
| 31 | + * @method Response answerBadRequest($data = null, $meta = []) // 400 |
| 32 | + * @method Response answerUnauthorized($data = null, $meta = []) // 401 |
| 33 | + * @method Response answerPaymentRequired($data = null, $meta = []) // 402 |
| 34 | + * @method Response answerForbidden($data = null, $meta = []) // 403 |
| 35 | + * @method Response answerNotFound($data = null, $meta = []) // 404 |
| 36 | + * @method Response answerMethodNotAllowed($data = null, $meta = []) // 405 |
| 37 | + * @method Response answerNotAcceptable($data = null, $meta = []) // 406 |
| 38 | + * @method Response answerProxyAuthenticationRequired($data = null, $meta = []) // 407 |
| 39 | + * @method Response answerRequestTimeout($data = null, $meta = []) // 408 |
| 40 | + * @method Response answerConflict($data = null, $meta = []) // 409 |
| 41 | + * @method Response answerGone($data = null, $meta = []) // 410 |
| 42 | + * @method Response answerLengthRequired($data = null, $meta = []) // 411 |
| 43 | + * @method Response answerPreconditionFailed($data = null, $meta = []) // 412 |
| 44 | + * @method Response answerRequestEntityTooLarge($data = null, $meta = []) // 413 |
| 45 | + * @method Response answerRequestURITooLarge($data = null, $meta = []) // 414 |
| 46 | + * @method Response answerUnsupportedMediaType($data = null, $meta = []) // 415 |
| 47 | + * @method Response answerRequestedRangeNotSatisfiable($data = null, $meta = []) // 416 |
| 48 | + * @method Response answerExpectationFailed($data = null, $meta = []) // 417 |
| 49 | + * @method Response answerIAmATeapot($data = null, $meta = []) // 418 |
| 50 | + * @method Response answerNotProcessableEntity($data = null, $meta = []) // 422 |
| 51 | + * @method Response answerLocked($data = null, $meta = []) // 423 |
| 52 | + * @method Response answerFailedDependency($data = null, $meta = []) // 424 |
| 53 | + * @method Response answerUnorderedCollection($data = null, $meta = []) // 425 |
| 54 | + * @method Response answerUpgradeRequired($data = null, $meta = []) // 426 |
| 55 | + * @method Response answerPreconditionRequired($data = null, $meta = []) // 428 |
| 56 | + * @method Response answerTooManyRequests($data = null, $meta = []) // 429 |
| 57 | + * @method Response answerRequestHeaderFieldsTooLarge($data = null, $meta = []) // 431 |
| 58 | + * |
| 59 | + * @method Response answerInternalServerErro($data = null, $meta = []) // 500 |
| 60 | + * @method Response answerNotImplemented($data = null, $meta = []) // 501 |
| 61 | + * @method Response answerBadGateway($data = null, $meta = []) // 502 |
| 62 | + * @method Response answerServiceUnavailable($data = null, $meta = []) // 503 |
| 63 | + * @method Response answerGatewayTimeout($data = null, $meta = []) // 504 |
| 64 | + * @method Response answerHTTPVersionNotSupported($data = null, $meta = []) // 505 |
| 65 | + * @method Response answerVariantAlsoNegotiates($data = null, $meta = []) // 506 |
| 66 | + * @method Response answerInsufficientStorage($data = null, $meta = []) // 507 |
| 67 | + * @method Response answerLoopDetected($data = null, $meta = []) // 508 |
| 68 | + * @method Response answerNetworkAuthenticationRequired($data = null, $meta = []) // 511 |
| 69 | + * |
| 70 | + * Class Controller |
| 71 | + * @package Simples\Http |
| 72 | + */ |
| 73 | +abstract class Controller |
| 74 | +{ |
| 75 | + /** |
| 76 | + * @var Request |
| 77 | + */ |
| 78 | + private $request; |
| 79 | + |
| 80 | + /** |
| 81 | + * @var Response |
| 82 | + */ |
| 83 | + private $response; |
| 84 | + |
| 85 | + /** |
| 86 | + * @var Match |
| 87 | + */ |
| 88 | + private $match; |
| 89 | + |
| 90 | + /** |
| 91 | + * @param Request $request |
| 92 | + * @param Response $response |
| 93 | + * @param Match $match |
| 94 | + * @return $this |
| 95 | + */ |
| 96 | + public final function boot(Request $request, Response $response, Match $match) |
| 97 | + { |
| 98 | + $this->request = $request; |
| 99 | + $this->response = $response; |
| 100 | + $this->match = $match; |
| 101 | + |
| 102 | + return $this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param null $content |
| 107 | + * @param array $meta |
| 108 | + * @param int $code |
| 109 | + * @return Response |
| 110 | + */ |
| 111 | + abstract protected function answer($content = null, $meta = [], $code = 200) : Response; |
| 112 | + |
| 113 | + /** |
| 114 | + * @return Request |
| 115 | + */ |
| 116 | + final protected function request() |
| 117 | + { |
| 118 | + return $this->request; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return Response |
| 123 | + */ |
| 124 | + final protected function response() |
| 125 | + { |
| 126 | + return $this->response; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return Match |
| 131 | + */ |
| 132 | + public function match() |
| 133 | + { |
| 134 | + return $this->match; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @param $name |
| 139 | + * @param $type |
| 140 | + * @return mixed |
| 141 | + */ |
| 142 | + public function input($name, $type = null) |
| 143 | + { |
| 144 | + $input = $this->request->getInput($name); |
| 145 | + if (!$input or !$type) { |
| 146 | + return $input; |
| 147 | + } |
| 148 | + return $input->filter($type); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * @param $logging |
| 153 | + */ |
| 154 | + public function setLog($logging) |
| 155 | + { |
| 156 | + App::log($logging && env('TEST_MODE')); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param $name |
| 161 | + * @param $arguments |
| 162 | + * @return Response |
| 163 | + */ |
| 164 | + public function __call($name, $arguments) |
| 165 | + { |
| 166 | + $httpStatusCodes = ResponseStream::HTTP_STATUS_CODE; |
| 167 | + |
| 168 | + $content = off($arguments, 0, ''); |
| 169 | + $meta = off($arguments, 1, []); |
| 170 | + $code = 501; |
| 171 | + |
| 172 | + if (substr($name, 0, 6) === 'answer') { |
| 173 | + $reasonPhrase = substr($name, 6); |
| 174 | + foreach ($httpStatusCodes as $statusCode => $statusReasonPhrase) { |
| 175 | + if ($reasonPhrase === str_replace([' ', '-'], '', $statusReasonPhrase)) { |
| 176 | + $code = $statusCode; |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + return $this->answer($content, $meta, $code); |
| 183 | + } |
| 184 | +} |
0 commit comments