@@ -38,125 +38,125 @@ |
||
| 38 | 38 | * @since 6.0.0 |
| 39 | 39 | */ |
| 40 | 40 | abstract class Controller { |
| 41 | - /** |
|
| 42 | - * app name |
|
| 43 | - * @var string |
|
| 44 | - * @since 7.0.0 |
|
| 45 | - */ |
|
| 46 | - protected $appName; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * current request |
|
| 50 | - * @var \OCP\IRequest |
|
| 51 | - * @since 6.0.0 |
|
| 52 | - */ |
|
| 53 | - protected $request; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var array |
|
| 57 | - * @since 7.0.0 |
|
| 58 | - */ |
|
| 59 | - private $responders; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * constructor of the controller |
|
| 63 | - * @param string $appName the name of the app |
|
| 64 | - * @param IRequest $request an instance of the request |
|
| 65 | - * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
| 66 | - */ |
|
| 67 | - public function __construct($appName, |
|
| 68 | - IRequest $request) { |
|
| 69 | - $this->appName = $appName; |
|
| 70 | - $this->request = $request; |
|
| 71 | - |
|
| 72 | - // default responders |
|
| 73 | - $this->responders = [ |
|
| 74 | - 'json' => function ($data) { |
|
| 75 | - if ($data instanceof DataResponse) { |
|
| 76 | - $response = new JSONResponse( |
|
| 77 | - $data->getData(), |
|
| 78 | - $data->getStatus() |
|
| 79 | - ); |
|
| 80 | - $dataHeaders = $data->getHeaders(); |
|
| 81 | - $headers = $response->getHeaders(); |
|
| 82 | - // do not overwrite Content-Type if it already exists |
|
| 83 | - if (isset($dataHeaders['Content-Type'])) { |
|
| 84 | - unset($headers['Content-Type']); |
|
| 85 | - } |
|
| 86 | - $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
| 87 | - |
|
| 88 | - if ($data->getETag() !== null) { |
|
| 89 | - $response->setETag($data->getETag()); |
|
| 90 | - } |
|
| 91 | - if ($data->getLastModified() !== null) { |
|
| 92 | - $response->setLastModified($data->getLastModified()); |
|
| 93 | - } |
|
| 94 | - if ($data->isThrottled()) { |
|
| 95 | - $response->throttle($data->getThrottleMetadata()); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - return $response; |
|
| 99 | - } |
|
| 100 | - return new JSONResponse($data); |
|
| 101 | - } |
|
| 102 | - ]; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Parses an HTTP accept header and returns the supported responder type |
|
| 108 | - * @param string $acceptHeader |
|
| 109 | - * @param string $default |
|
| 110 | - * @return string the responder type |
|
| 111 | - * @since 7.0.0 |
|
| 112 | - * @since 9.1.0 Added default parameter |
|
| 113 | - */ |
|
| 114 | - public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
| 115 | - $headers = explode(',', $acceptHeader); |
|
| 116 | - |
|
| 117 | - // return the first matching responder |
|
| 118 | - foreach ($headers as $header) { |
|
| 119 | - $header = strtolower(trim($header)); |
|
| 120 | - |
|
| 121 | - $responder = str_replace('application/', '', $header); |
|
| 122 | - |
|
| 123 | - if (array_key_exists($responder, $this->responders)) { |
|
| 124 | - return $responder; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // no matching header return default |
|
| 129 | - return $default; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Registers a formatter for a type |
|
| 135 | - * @param string $format |
|
| 136 | - * @param \Closure $responder |
|
| 137 | - * @since 7.0.0 |
|
| 138 | - */ |
|
| 139 | - protected function registerResponder($format, \Closure $responder) { |
|
| 140 | - $this->responders[$format] = $responder; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Serializes and formats a response |
|
| 146 | - * @param mixed $response the value that was returned from a controller and |
|
| 147 | - * is not a Response instance |
|
| 148 | - * @param string $format the format for which a formatter has been registered |
|
| 149 | - * @throws \DomainException if format does not match a registered formatter |
|
| 150 | - * @return Response |
|
| 151 | - * @since 7.0.0 |
|
| 152 | - */ |
|
| 153 | - public function buildResponse($response, $format = 'json') { |
|
| 154 | - if (array_key_exists($format, $this->responders)) { |
|
| 155 | - $responder = $this->responders[$format]; |
|
| 156 | - |
|
| 157 | - return $responder($response); |
|
| 158 | - } |
|
| 159 | - throw new \DomainException('No responder registered for format '. |
|
| 160 | - $format . '!'); |
|
| 161 | - } |
|
| 41 | + /** |
|
| 42 | + * app name |
|
| 43 | + * @var string |
|
| 44 | + * @since 7.0.0 |
|
| 45 | + */ |
|
| 46 | + protected $appName; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * current request |
|
| 50 | + * @var \OCP\IRequest |
|
| 51 | + * @since 6.0.0 |
|
| 52 | + */ |
|
| 53 | + protected $request; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var array |
|
| 57 | + * @since 7.0.0 |
|
| 58 | + */ |
|
| 59 | + private $responders; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * constructor of the controller |
|
| 63 | + * @param string $appName the name of the app |
|
| 64 | + * @param IRequest $request an instance of the request |
|
| 65 | + * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0 |
|
| 66 | + */ |
|
| 67 | + public function __construct($appName, |
|
| 68 | + IRequest $request) { |
|
| 69 | + $this->appName = $appName; |
|
| 70 | + $this->request = $request; |
|
| 71 | + |
|
| 72 | + // default responders |
|
| 73 | + $this->responders = [ |
|
| 74 | + 'json' => function ($data) { |
|
| 75 | + if ($data instanceof DataResponse) { |
|
| 76 | + $response = new JSONResponse( |
|
| 77 | + $data->getData(), |
|
| 78 | + $data->getStatus() |
|
| 79 | + ); |
|
| 80 | + $dataHeaders = $data->getHeaders(); |
|
| 81 | + $headers = $response->getHeaders(); |
|
| 82 | + // do not overwrite Content-Type if it already exists |
|
| 83 | + if (isset($dataHeaders['Content-Type'])) { |
|
| 84 | + unset($headers['Content-Type']); |
|
| 85 | + } |
|
| 86 | + $response->setHeaders(array_merge($dataHeaders, $headers)); |
|
| 87 | + |
|
| 88 | + if ($data->getETag() !== null) { |
|
| 89 | + $response->setETag($data->getETag()); |
|
| 90 | + } |
|
| 91 | + if ($data->getLastModified() !== null) { |
|
| 92 | + $response->setLastModified($data->getLastModified()); |
|
| 93 | + } |
|
| 94 | + if ($data->isThrottled()) { |
|
| 95 | + $response->throttle($data->getThrottleMetadata()); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + return $response; |
|
| 99 | + } |
|
| 100 | + return new JSONResponse($data); |
|
| 101 | + } |
|
| 102 | + ]; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Parses an HTTP accept header and returns the supported responder type |
|
| 108 | + * @param string $acceptHeader |
|
| 109 | + * @param string $default |
|
| 110 | + * @return string the responder type |
|
| 111 | + * @since 7.0.0 |
|
| 112 | + * @since 9.1.0 Added default parameter |
|
| 113 | + */ |
|
| 114 | + public function getResponderByHTTPHeader($acceptHeader, $default = 'json') { |
|
| 115 | + $headers = explode(',', $acceptHeader); |
|
| 116 | + |
|
| 117 | + // return the first matching responder |
|
| 118 | + foreach ($headers as $header) { |
|
| 119 | + $header = strtolower(trim($header)); |
|
| 120 | + |
|
| 121 | + $responder = str_replace('application/', '', $header); |
|
| 122 | + |
|
| 123 | + if (array_key_exists($responder, $this->responders)) { |
|
| 124 | + return $responder; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // no matching header return default |
|
| 129 | + return $default; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Registers a formatter for a type |
|
| 135 | + * @param string $format |
|
| 136 | + * @param \Closure $responder |
|
| 137 | + * @since 7.0.0 |
|
| 138 | + */ |
|
| 139 | + protected function registerResponder($format, \Closure $responder) { |
|
| 140 | + $this->responders[$format] = $responder; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Serializes and formats a response |
|
| 146 | + * @param mixed $response the value that was returned from a controller and |
|
| 147 | + * is not a Response instance |
|
| 148 | + * @param string $format the format for which a formatter has been registered |
|
| 149 | + * @throws \DomainException if format does not match a registered formatter |
|
| 150 | + * @return Response |
|
| 151 | + * @since 7.0.0 |
|
| 152 | + */ |
|
| 153 | + public function buildResponse($response, $format = 'json') { |
|
| 154 | + if (array_key_exists($format, $this->responders)) { |
|
| 155 | + $responder = $this->responders[$format]; |
|
| 156 | + |
|
| 157 | + return $responder($response); |
|
| 158 | + } |
|
| 159 | + throw new \DomainException('No responder registered for format '. |
|
| 160 | + $format . '!'); |
|
| 161 | + } |
|
| 162 | 162 | } |