Completed
Push — master ( f6f7b5...3a3e6c )
by Lukas
40:35 queued 25:18
created

PluginManager::populate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 0
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Vincent Petry <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud GmbH.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace OCA\DAV\AppInfo;
22
23
use OCP\App\IAppManager;
24
use OC\ServerContainer;
25
use OCP\AppFramework\QueryException;
26
27
/**
28
 * Manager for DAV plugins from apps, used to register them
29
 * to the Sabre server.
30
 */
31
class PluginManager {
32
33
	/**
34
	 * @var ServerContainer
35
	 */
36
	private $container;
37
38
	/**
39
	 * @var IAppManager
40
	 */
41
	private $appManager;
42
43
	/**
44
	 * App plugins
45
	 *
46
	 * @var array
47
	 */
48
	private $plugins = null;
49
50
	/**
51
	 * App collections
52
	 *
53
	 * @var array
54
	 */
55
	private $collections = null;
56
57
	/**
58
	 * Contstruct a PluginManager
59
	 *
60
	 * @param ServerContainer $container server container for resolving plugin classes
61
	 * @param IAppManager $appManager app manager to loading apps and their info
62
	 */
63
	public function __construct(ServerContainer $container, IAppManager $appManager) {
64
		$this->container = $container;
65
		$this->appManager = $appManager;
66
	}
67
68
	/**
69
	 * Returns an array of app-registered plugins
70
	 *
71
	 * @return array
72
	 */
73
	public function getAppPlugins() {
74
		if (null === $this->plugins) {
75
			$this->populate();
76
		}
77
		return $this->plugins;
78
	}
79
80
	/**
81
	 * Returns an array of app-registered collections
82
	 *
83
	 * @return array
84
	 */
85
	public function getAppCollections() {
86
		if (null === $this->collections) {
87
			$this->populate();
88
		}
89
		return $this->collections;
90
	}
91
92
	/**
93
	 * Retrieve plugin and collection list and populate attributes
94
	 */
95
	private function populate() {
96
		$this->plugins = [];
97
		$this->collections = [];
98
		foreach ($this->appManager->getInstalledApps() as $app) {
99
			// load plugins and collections from info.xml
100
			$info = $this->appManager->getAppInfo($app);
101
			if (!isset($info['types']) || !in_array('dav', $info['types'], true)) {
102
				continue;
103
			}
104
			// FIXME: switch to public API once available
105
			// load app to make sure its classes are available
106
			\OC_App::loadApp($app);
107
			$this->loadSabrePluginsFromInfoXml($this->extractPluginList($info));
108
			$this->loadSabreCollectionsFromInfoXml($this->extractCollectionList($info));
109
		}
110
	}
111
112 View Code Duplication
	private function extractPluginList(array $array) {
113
		if (isset($array['sabre']) && is_array($array['sabre'])) {
114
			if (isset($array['sabre']['plugins']) && is_array($array['sabre']['plugins'])) {
115
				if (isset($array['sabre']['plugins']['plugin'])) {
116
					$items = $array['sabre']['plugins']['plugin'];
117
					if (!is_array($items)) {
118
						$items = [$items];
119
					}
120
					return $items;
121
				}
122
			}
123
		}
124
		return [];
125
	}
126
127 View Code Duplication
	private function extractCollectionList(array $array) {
128
		if (isset($array['sabre']) && is_array($array['sabre'])) {
129
			if (isset($array['sabre']['collections']) && is_array($array['sabre']['collections'])) {
130
				if (isset($array['sabre']['collections']['collection'])) {
131
					$items = $array['sabre']['collections']['collection'];
132
					if (!is_array($items)) {
133
						$items = [$items];
134
					}
135
					return $items;
136
				}
137
			}
138
		}
139
		return [];
140
	}
141
142 View Code Duplication
	private function loadSabrePluginsFromInfoXml(array $plugins) {
143
		foreach ($plugins as $plugin) {
144
			try {
145
				$this->plugins[] = $this->container->query($plugin);
146
			} catch (QueryException $e) {
147
				if (class_exists($plugin)) {
148
					$this->plugins[] = new $plugin();
149
				} else {
150
					throw new \Exception("Sabre plugin class '$plugin' is unknown and could not be loaded");
151
				}
152
			}
153
		}
154
	}
155
156 View Code Duplication
	private function loadSabreCollectionsFromInfoXml(array $collections) {
157
		foreach ($collections as $collection) {
158
			try {
159
				$this->collections[] = $this->container->query($collection);
160
			} catch (QueryException $e) {
161
				if (class_exists($collection)) {
162
					$this->collections[] = new $collection();
163
				} else {
164
					throw new \Exception("Sabre collection class '$collection' is unknown and could not be loaded");
165
				}
166
			}
167
		}
168
	}
169
170
}
171