Completed
Pull Request — master (#29767)
by Individual IT
09:24
created

App   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildAppNamespace() 0 10 2
D main() 0 69 12
A part() 0 11 1
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
 *
8
 * @copyright Copyright (c) 2017, ownCloud GmbH
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
26
namespace OC\AppFramework;
27
28
use OC\AppFramework\Http\Dispatcher;
29
use OC_App;
30
use OC\AppFramework\DependencyInjection\DIContainer;
31
use OCP\AppFramework\QueryException;
32
use OCP\AppFramework\Http\ICallbackResponse;
33
34
/**
35
 * Entry point for every request in your app. You can consider this as your
36
 * public static void main() method
37
 *
38
 * Handles all the dependency injection, controllers and output flow
39
 */
40
class App {
41
42
43
	/**
44
	 * Turns an app id into a namespace by either reading the appinfo.xml's
45
	 * namespace tag or uppercasing the appid's first letter
46
	 * @param string $appId the app id
47
	 * @param string $topNamespace the namespace which should be prepended to
48
	 * the transformed app id, defaults to OCA\
49
	 * @return string the starting namespace for the app
50
	 */
51
	public static function buildAppNamespace($appId, $topNamespace='OCA\\') {
52
		// first try to parse the app's appinfo/info.xml <namespace> tag
53
		$appInfo = \OC_App::getAppInfo($appId);
54
		if (isset($appInfo['namespace'])) {
55
			return $topNamespace . trim($appInfo['namespace']);
56
		}
57
58
		// if the tag is not found, fall back to uppercasing the first letter
59
		return $topNamespace . ucfirst($appId);
60
	}
61
62
63
	/**
64
	 * Shortcut for calling a controller method and printing the result
65
	 * @param string $controllerName the name of the controller under which it is
66
	 *                               stored in the DI container
67
	 * @param string $methodName the method that you want to call
68
	 * @param DIContainer $container an instance of a pimple container.
69
	 * @param array $urlParams list of URL parameters (optional)
70
	 */
71
	public static function main($controllerName, $methodName, DIContainer $container, array $urlParams = null) {
72
		if (!is_null($urlParams)) {
73
			$container['OCP\\IRequest']->setUrlParameters($urlParams);
74
		} else if (isset($container['urlParams']) && !is_null($container['urlParams'])) {
75
			$container['OCP\\IRequest']->setUrlParameters($container['urlParams']);
76
		}
77
		$appName = $container['AppName'];
78
79
		// first try $controllerName then go for \OCA\AppName\Controller\$controllerName
80
		try {
81
			$controller = $container->query($controllerName);
82
		} catch(QueryException $e) {
83
			$appNameSpace = self::buildAppNamespace($appName);
84
			$controllerName = $appNameSpace . '\\Controller\\' . $controllerName;
85
			try {
86
				$controller = $container->query($controllerName);
87
			} catch (QueryException $e2) {
88
				// the reason we got here could also be because of the first exception above,
89
				// so combine the message from both
90
				throw new QueryException($e2->getMessage() . ' or error resolving constructor arguments: ' . $e->getMessage());
91
			}
92
		}
93
94
		// initialize the dispatcher and run all the middleware before the controller
95
		/** @var Dispatcher $dispatcher */
96
		$dispatcher = $container['Dispatcher'];
97
98
		list(
99
			$httpHeaders,
100
			$responseHeaders,
101
			$responseCookies,
102
			$output,
103
			$response
104
		) = $dispatcher->dispatch($controller, $methodName);
105
106
		$io = $container['OCP\\AppFramework\\Http\\IOutput'];
107
108
		if(!is_null($httpHeaders)) {
109
			$io->setHeader($httpHeaders);
110
		}
111
112
		foreach($responseHeaders as $name => $value) {
113
			$io->setHeader($name . ': ' . $value);
114
		}
115
116
		foreach($responseCookies as $name => $value) {
117
			$expireDate = null;
118
			if($value['expireDate'] instanceof \DateTime) {
119
				$expireDate = $value['expireDate']->getTimestamp();
120
			}
121
			$io->setCookie(
122
				$name,
123
				$value['value'],
124
				$expireDate,
125
				$container->getServer()->getWebRoot(),
126
				null,
127
				$container->getServer()->getRequest()->getServerProtocol() === 'https',
128
				true
129
			);
130
		}
131
132
		if ($response instanceof ICallbackResponse) {
133
			$response->callback($io);
134
		} else if(!is_null($output)) {
135
			$io->setHeader('Content-Length: ' . strlen($output));
136
			$io->setOutput($output);
137
		}
138
139
	}
140
141
	/**
142
	 * Shortcut for calling a controller method and printing the result.
143
	 * Similar to App:main except that no headers will be sent.
144
	 * This should be used for example when registering sections via
145
	 * \OC\AppFramework\Core\API::registerAdmin()
146
	 *
147
	 * @param string $controllerName the name of the controller under which it is
148
	 *                               stored in the DI container
149
	 * @param string $methodName the method that you want to call
150
	 * @param array $urlParams an array with variables extracted from the routes
151
	 * @param DIContainer $container an instance of a pimple container.
152
	 */
153
	public static function part($controllerName, $methodName, array $urlParams,
154
								DIContainer $container){
155
156
		$container['urlParams'] = $urlParams;
157
		$controller = $container[$controllerName];
158
159
		$dispatcher = $container['Dispatcher'];
160
161
		list(, , $output) =  $dispatcher->dispatch($controller, $methodName);
162
		return $output;
163
	}
164
165
}
166