Completed
Push — master ( 2c4c56...fc98aa )
by Thomas
19:13
created

App::buildAppNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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