Passed
Push — master ( d1b03f...c1183f )
by Christoph
11:01 queued 11s
created

Coordinator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A bootApp() 0 27 4
A runRegistration() 0 41 6
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2020 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2020 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
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
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\AppFramework\Bootstrap;
27
28
use OC_App;
29
use OCP\AppFramework\App;
30
use OCP\AppFramework\Bootstrap\IBootstrap;
31
use OCP\AppFramework\QueryException;
32
use OCP\EventDispatcher\IEventDispatcher;
33
use OCP\ILogger;
34
use OCP\IServerContainer;
35
use function class_exists;
36
use function class_implements;
37
use function in_array;
38
39
class Coordinator {
40
41
	/** @var IServerContainer */
42
	private $serverContainer;
43
44
	/** @var IEventDispatcher */
45
	private $eventDispatcher;
46
47
	/** @var ILogger */
48
	private $logger;
49
50
	public function __construct(IServerContainer $container,
51
								IEventDispatcher $eventListener,
52
								ILogger $logger) {
53
		$this->serverContainer = $container;
54
		$this->eventDispatcher = $eventListener;
55
		$this->logger = $logger;
56
	}
57
58
	public function runRegistration(): void {
59
		$context = new RegistrationContext($this->logger);
60
		$apps = [];
61
		foreach (OC_App::getEnabledApps() as $appId) {
62
			/*
63
			 * First, we have to enable the app's autoloader
64
			 *
65
			 * @todo use $this->appManager->getAppPath($appId) here
66
			 */
67
			$path = OC_App::getAppPath($appId);
0 ignored issues
show
Deprecated Code introduced by
The function OC_App::getAppPath() has been deprecated: 11.0.0 use \OC::$server->getAppManager()->getAppPath() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
			$path = /** @scrutinizer ignore-deprecated */ OC_App::getAppPath($appId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
68
			if ($path === false) {
69
				// Ignore
70
				continue;
71
			}
72
			OC_App::registerAutoloading($appId, $path);
73
74
			/*
75
			 * Next we check if there is an application class and it implements
76
			 * the \OCP\AppFramework\Bootstrap\IBootstrap interface
77
			 */
78
			$appNameSpace = App::buildAppNamespace($appId);
79
			$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
80
			if (class_exists($applicationClassName) && in_array(IBootstrap::class, class_implements($applicationClassName), true)) {
81
				try {
82
					/** @var IBootstrap|App $application */
83
					$apps[$appId] = $application = $this->serverContainer->query($applicationClassName);
84
					$application->register($context->for($appId));
0 ignored issues
show
Bug introduced by
The method register() does not exist on OCP\AppFramework\App. Did you maybe mean registerRoutes()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
					$application->/** @scrutinizer ignore-call */ 
85
                   register($context->for($appId));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
				} catch (QueryException $e) {
86
					// Weird, but ok
87
				}
88
			}
89
		}
90
91
		/**
92
		 * Now that all register methods have been called, we can delegate the registrations
93
		 * to the actual services
94
		 */
95
		$context->delegateCapabilityRegistrations($apps);
96
		$context->delegateEventListenerRegistrations($this->eventDispatcher);
97
		$context->delegateContainerRegistrations($apps);
98
		$context->delegateMiddlewareRegistrations($apps);
99
	}
100
101
	public function bootApp(string $appId): void {
102
		$appNameSpace = App::buildAppNamespace($appId);
103
		$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
104
		if (!class_exists($applicationClassName)) {
105
			// Nothing to boot
106
			return;
107
		}
108
109
		/*
110
		 * Now it is time to fetch an instance of the App class. For classes
111
		 * that implement \OCP\AppFramework\Bootstrap\IBootstrap this means
112
		 * the instance was already created for register, but any other
113
		 * (legacy) code will now do their magic via the constructor.
114
		 */
115
		try {
116
			/** @var App $application */
117
			$application = $this->serverContainer->query($applicationClassName);
118
			if ($application instanceof IBootstrap) {
119
				/** @var BootContext $context */
120
				$context = new BootContext($application->getContainer());
121
				$application->boot($context);
122
			}
123
		} catch (QueryException $e) {
124
			$this->logger->logException($e, [
125
				'message' => "Could not boot $appId" . $e->getMessage(),
126
			]);
127
			return;
128
		}
129
	}
130
}
131