Completed
Push — master ( c33a99...33f36c )
by
unknown
11:35
created

Controller::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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