1
0

Controller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Donquixote <marjunebatac@gmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Thomas Tanghus <thomas@tanghus.net>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCP\AppFramework;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\Http\JSONResponse;
  32. use OCP\AppFramework\Http\Response;
  33. use OCP\IRequest;
  34. /**
  35. * Base class to inherit your controllers from
  36. * @since 6.0.0
  37. */
  38. abstract class Controller {
  39. /**
  40. * app name
  41. * @var string
  42. * @since 7.0.0
  43. */
  44. protected $appName;
  45. /**
  46. * current request
  47. * @var \OCP\IRequest
  48. * @since 6.0.0
  49. */
  50. protected $request;
  51. /**
  52. * @var array
  53. * @since 7.0.0
  54. */
  55. private $responders;
  56. /**
  57. * constructor of the controller
  58. * @param string $appName the name of the app
  59. * @param IRequest $request an instance of the request
  60. * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0
  61. */
  62. public function __construct($appName,
  63. IRequest $request) {
  64. $this->appName = $appName;
  65. $this->request = $request;
  66. // default responders
  67. $this->responders = [
  68. 'json' => function ($data) {
  69. if ($data instanceof DataResponse) {
  70. $response = new JSONResponse(
  71. $data->getData(),
  72. $data->getStatus()
  73. );
  74. $dataHeaders = $data->getHeaders();
  75. $headers = $response->getHeaders();
  76. // do not overwrite Content-Type if it already exists
  77. if (isset($dataHeaders['Content-Type'])) {
  78. unset($headers['Content-Type']);
  79. }
  80. $response->setHeaders(array_merge($dataHeaders, $headers));
  81. if ($data->getETag() !== null) {
  82. $response->setETag($data->getETag());
  83. }
  84. if ($data->getLastModified() !== null) {
  85. $response->setLastModified($data->getLastModified());
  86. }
  87. return $response;
  88. }
  89. return new JSONResponse($data);
  90. }
  91. ];
  92. }
  93. /**
  94. * Parses an HTTP accept header and returns the supported responder type
  95. * @param string $acceptHeader
  96. * @param string $default
  97. * @return string the responder type
  98. * @since 7.0.0
  99. * @since 9.1.0 Added default parameter
  100. */
  101. public function getResponderByHTTPHeader($acceptHeader, $default = 'json') {
  102. $headers = explode(',', $acceptHeader);
  103. // return the first matching responder
  104. foreach ($headers as $header) {
  105. $header = strtolower(trim($header));
  106. $responder = str_replace('application/', '', $header);
  107. if (array_key_exists($responder, $this->responders)) {
  108. return $responder;
  109. }
  110. }
  111. // no matching header return default
  112. return $default;
  113. }
  114. /**
  115. * Registers a formatter for a type
  116. * @param string $format
  117. * @param \Closure $responder
  118. * @since 7.0.0
  119. */
  120. protected function registerResponder($format, \Closure $responder) {
  121. $this->responders[$format] = $responder;
  122. }
  123. /**
  124. * Serializes and formats a response
  125. * @param mixed $response the value that was returned from a controller and
  126. * is not a Response instance
  127. * @param string $format the format for which a formatter has been registered
  128. * @throws \DomainException if format does not match a registered formatter
  129. * @return Response
  130. * @since 7.0.0
  131. */
  132. public function buildResponse($response, $format = 'json') {
  133. if (array_key_exists($format, $this->responders)) {
  134. $responder = $this->responders[$format];
  135. return $responder($response);
  136. }
  137. throw new \DomainException('No responder registered for format '.
  138. $format . '!');
  139. }
  140. }