aboutsummaryrefslogtreecommitdiff
path: root/engine/classes/ElggPluginManifestParser18.php
blob: 1d4e9daed3baac04b3b77b72aac3e3fcaf7c787b (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
 * Plugin manifest.xml parser for Elgg 1.8 and above.
 *
 * @package    Elgg.Core
 * @subpackage Plugins
 */
class ElggPluginManifestParser18 extends ElggPluginManifestParser {
	/**
	 * The valid top level attributes and defaults for a 1.8 manifest array.
	 *
	 * @var array
	 */
	protected $validAttributes = array(
		'name' => null,
		'author' => null,
		'version' => null,
		'blurb' => null,
		'description' => null,
		'website' => null,
		'copyright' => null,
		'license' => 'GNU Public License version 2',
		'depends' => array(),
		'screenshots' => array(),
		'conflicts' => array(),
		'provides' => array(),
		'admin' => array(
			'on_enable' => null,
			'on_disable' => null,
			'interface_type' => 'advanced'
		)
	);

	/**
	 * Required attributes for a valid 1.8 manifest
	 *
	 * @var array
	 */
	protected $requiredAttributes = array(
		'name', 'author', 'version', 'description', 'depends'
	);

	/**
	 * Parse a manifest object from 1.8 and later
	 *
	 * @return void
	 */
	public function parse() {
		$parsed = array();
		foreach ($this->manifestObject->children as $element) {
			switch ($element->name) {
				// single elements
				// translatable
				case 'blurb':
				case 'description':
					$element->content = elgg_echo($element->content);

				case 'name':
				case 'author':
				case 'version':
				case 'website':
				case 'copyright':
				case 'license':
					$parsed[$element->name] = $element->content;
					break;

				// arrays
				case 'screenshot':
					if (isset($element->attributes['description'])) {
						$description = elgg_echo($element->attributes['description']);
					}
					$parsed['screenshots'][] = array(
						'description' => $description,
						'path' => $element->content
					);
					break;

				case 'admin':
					$parsed['admin'] = array();
					if (!isset($element->children)) {
						return false;
					}

					foreach ($element->children as $child_element) {
						$parsed['admin'][$child_element->name] = $child_element->content;
					}

					break;

				case 'provides':
				case 'conflicts':
				case 'depends':
					if (!isset($element->children)) {
						return false;
					}

					$info = array();
					foreach ($element->children as $child_element) {
						$info[$child_element->name] = $child_element->content;
					}

					$parsed[$element->name][] = $info;
					break;
			}
		}

		// check we have all the required fields
		foreach ($this->requiredAttributes as $attr) {
			if (!array_key_exists($attr, $parsed)) {
				throw new PluginException(elgg_echo('PluginException:ParserErrorMissingRequiredAttribute',
							array($attr, $this->caller->getPluginID())));
			}
		}

		$this->manifest = $parsed;

		return true;
	}
}