dependencyanalyzer.php 2.24 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 {

15
16
17
18
19
20
21
22
23
24
25
26
	/** @var Platform */
	private $system;

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

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

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

27
28
	/**
	 * @param array $app
29
	 * @param Platform $platform
30
31
	 * @param \OCP\IL10N $l
	 */
32
33
	function __construct(array $app, $platform, $l) {
		$this->system = $platform;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		$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();
Thomas Müller's avatar
Thomas Müller committed
48
		$this->analyseSupportedDatabases();
49
50
51
52
		return $this->missing;
	}

	private function analysePhpVersion() {
53
54
		if (isset($this->dependencies['php']['@attributes']['min-version'])) {
			$minVersion = $this->dependencies['php']['@attributes']['min-version'];
55
56
57
58
			if (version_compare($this->system->getPhpVersion(), $minVersion, '<')) {
				$this->missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion);
			}
		}
59
60
		if (isset($this->dependencies['php']['@attributes']['max-version'])) {
			$maxVersion = $this->dependencies['php']['@attributes']['max-version'];
61
62
63
64
65
66
			if (version_compare($this->system->getPhpVersion(), $maxVersion, '>')) {
				$this->missing[] = (string)$this->l->t('PHP with a version less then %s is required.', $maxVersion);
			}
		}
	}

Thomas Müller's avatar
Thomas Müller committed
67
	private function analyseSupportedDatabases() {
68
		if (!isset($this->dependencies['database'])) {
Thomas Müller's avatar
Thomas Müller committed
69
70
71
			return;
		}

72
		$supportedDatabases = $this->dependencies['database'];
Thomas Müller's avatar
Thomas Müller committed
73
74
75
		if (empty($supportedDatabases)) {
			return;
		}
76
77
78
79
80
81
		$supportedDatabases = array_map(function($db) {
			if (isset($db['@value'])) {
				return $db['@value'];
			}
			return $db;
		}, $supportedDatabases);
Thomas Müller's avatar
Thomas Müller committed
82
83
84
85
86
		$currentDatabase = $this->system->getDatabase();
		if (!in_array($currentDatabase, $supportedDatabases)) {
			$this->missing[] = (string)$this->l->t('Following databases are supported: %s', join(', ', $supportedDatabases));
		}
	}
87
}