summaryrefslogtreecommitdiffstats
path: root/lib/private/app/dependencyanalyzer.php
blob: fb4b376165632878d6afa967471ab0bba749b73c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
 /**
 * @author Thomas Müller
 * @copyright 2014 Thomas Müller deepdiver@owncloud.com
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OC\App;

class DependencyAnalyzer {

	/** @var Platform */
	private $system;

	/** @var \OCP\IL10N */
	private $l;

	/** @var array  */
	private $missing;

	/** @var array  */
	private $dependencies;

	/**
	 * @param array $app
	 * @param Platform $platform
	 * @param \OCP\IL10N $l
	 */
	function __construct(array $app, $platform, $l) {
		$this->system = $platform;
		$this->l = $l;
		$this->missing = array();
		$this->dependencies = array();
		if (array_key_exists('dependencies', $app)) {
			$this->dependencies = $app['dependencies'];
		}
	}

	/**
	 * @param array $app
	 * @returns array of missing dependencies
	 */
	public function analyze() {
		$this->analysePhpVersion();
		$this->analyseSupportedDatabases();
		return $this->missing;
	}

	private function analysePhpVersion() {
		if (isset($this->dependencies['php']['@attributes']['min-version'])) {
			$minVersion = $this->dependencies['php']['@attributes']['min-version'];
			if (version_compare($this->system->getPhpVersion(), $minVersion, '<')) {
				$this->missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion);
			}
		}
		if (isset($this->dependencies['php']['@attributes']['max-version'])) {
			$maxVersion = $this->dependencies['php']['@attributes']['max-version'];
			if (version_compare($this->system->getPhpVersion(), $maxVersion, '>')) {
				$this->missing[] = (string)$this->l->t('PHP with a version less then %s is required.', $maxVersion);
			}
		}
	}

	private function analyseSupportedDatabases() {
		if (!isset($this->dependencies['database'])) {
			return;
		}

		$supportedDatabases = $this->dependencies['database'];
		if (empty($supportedDatabases)) {
			return;
		}
		$supportedDatabases = array_map(function($db) {
			if (isset($db['@value'])) {
				return $db['@value'];
			}
			return $db;
		}, $supportedDatabases);
		$currentDatabase = $this->system->getDatabase();
		if (!in_array($currentDatabase, $supportedDatabases)) {
			$this->missing[] = (string)$this->l->t('Following databases are supported: %s', join(', ', $supportedDatabases));
		}
	}
}