Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * Metadata for an extension (e.g. the extension's "info.xml" file) | |
4faa436b SB |
14 | * |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
17 | */ |
18 | class CRM_Extension_Info { | |
19 | ||
20 | /** | |
fe482240 | 21 | * Extension info file name. |
6a488035 TO |
22 | */ |
23 | const FILENAME = 'info.xml'; | |
24 | ||
7b966967 SL |
25 | /** |
26 | * @var string | |
27 | */ | |
6a488035 TO |
28 | public $key = NULL; |
29 | public $type = NULL; | |
30 | public $name = NULL; | |
31 | public $label = NULL; | |
32 | public $file = NULL; | |
33 | ||
4025c773 TO |
34 | /** |
35 | * @var array | |
36 | * Each item is a specification like: | |
37 | * array('type'=>'psr4', 'namespace'=>'Foo\Bar', 'path'=>'/foo/bar'). | |
38 | */ | |
be2fb01f | 39 | public $classloader = []; |
4025c773 | 40 | |
f21a105f TO |
41 | /** |
42 | * @var array | |
43 | * Each item is they key-name of an extension required by this extension. | |
44 | */ | |
be2fb01f | 45 | public $requires = []; |
f21a105f | 46 | |
6a488035 | 47 | /** |
fe482240 | 48 | * Load extension info an XML file. |
6a488035 | 49 | * |
6c8f6e67 EM |
50 | * @param $file |
51 | * | |
52 | * @throws CRM_Extension_Exception_ParseException | |
6a488035 | 53 | * @return CRM_Extension_Info |
6a488035 TO |
54 | */ |
55 | public static function loadFromFile($file) { | |
56 | list ($xml, $error) = CRM_Utils_XML::parseFile($file); | |
57 | if ($xml === FALSE) { | |
58 | throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $error"); | |
59 | } | |
60 | ||
61 | $instance = new CRM_Extension_Info(); | |
62 | $instance->parse($xml); | |
63 | return $instance; | |
64 | } | |
65 | ||
66 | /** | |
fe482240 | 67 | * Load extension info a string. |
6a488035 | 68 | * |
f41911fd TO |
69 | * @param string $string |
70 | * XML content. | |
77b97be7 EM |
71 | * |
72 | * @throws CRM_Extension_Exception_ParseException | |
6a488035 TO |
73 | * @return CRM_Extension_Info |
74 | */ | |
75 | public static function loadFromString($string) { | |
76 | list ($xml, $error) = CRM_Utils_XML::parseString($string); | |
77 | if ($xml === FALSE) { | |
78 | throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $string"); | |
79 | } | |
80 | ||
81 | $instance = new CRM_Extension_Info(); | |
82 | $instance->parse($xml); | |
83 | return $instance; | |
84 | } | |
85 | ||
f21a105f TO |
86 | /** |
87 | * Build a reverse-dependency map. | |
88 | * | |
89 | * @param array $infos | |
90 | * The universe of available extensions. | |
91 | * Ex: $infos['org.civicrm.foobar'] = new CRM_Extension_Info(). | |
92 | * @return array | |
93 | * If "org.civicrm.api" is required by "org.civicrm.foo", then return | |
94 | * array('org.civicrm.api' => array(CRM_Extension_Info[org.civicrm.foo])). | |
95 | * Array(string $key => array $requiredBys). | |
96 | */ | |
97 | public static function buildReverseMap($infos) { | |
be2fb01f | 98 | $revMap = []; |
f21a105f TO |
99 | foreach ($infos as $info) { |
100 | foreach ($info->requires as $key) { | |
101 | $revMap[$key][] = $info; | |
102 | } | |
103 | } | |
104 | return $revMap; | |
105 | } | |
106 | ||
e0ef6999 EM |
107 | /** |
108 | * @param null $key | |
109 | * @param null $type | |
110 | * @param null $name | |
111 | * @param null $label | |
112 | * @param null $file | |
113 | */ | |
00be9182 | 114 | public function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) { |
353ffa53 TO |
115 | $this->key = $key; |
116 | $this->type = $type; | |
117 | $this->name = $name; | |
118 | $this->label = $label; | |
119 | $this->file = $file; | |
6a488035 TO |
120 | } |
121 | ||
122 | /** | |
123 | * Copy attributes from an XML document to $this | |
124 | * | |
125 | * @param SimpleXMLElement $info | |
6a488035 TO |
126 | */ |
127 | public function parse($info) { | |
353ffa53 TO |
128 | $this->key = (string) $info->attributes()->key; |
129 | $this->type = (string) $info->attributes()->type; | |
130 | $this->file = (string) $info->file; | |
6a488035 TO |
131 | $this->label = (string) $info->name; |
132 | ||
133 | // Convert first level variables to CRM_Core_Extension properties | |
134 | // and deeper into arrays. An exception for URLS section, since | |
135 | // we want them in special format. | |
136 | foreach ($info as $attr => $val) { | |
137 | if (count($val->children()) == 0) { | |
138 | $this->$attr = (string) $val; | |
139 | } | |
140 | elseif ($attr === 'urls') { | |
be2fb01f | 141 | $this->urls = []; |
6a488035 TO |
142 | foreach ($val->url as $url) { |
143 | $urlAttr = (string) $url->attributes()->desc; | |
144 | $this->urls[$urlAttr] = (string) $url; | |
145 | } | |
146 | ksort($this->urls); | |
147 | } | |
4025c773 | 148 | elseif ($attr === 'classloader') { |
be2fb01f | 149 | $this->classloader = []; |
4025c773 | 150 | foreach ($val->psr4 as $psr4) { |
be2fb01f | 151 | $this->classloader[] = [ |
4025c773 TO |
152 | 'type' => 'psr4', |
153 | 'prefix' => (string) $psr4->attributes()->prefix, | |
154 | 'path' => (string) $psr4->attributes()->path, | |
be2fb01f | 155 | ]; |
4025c773 TO |
156 | } |
157 | } | |
f21a105f | 158 | elseif ($attr === 'requires') { |
e4c4f267 | 159 | $this->requires = $this->filterRequirements($val); |
f21a105f | 160 | } |
6a488035 TO |
161 | else { |
162 | $this->$attr = CRM_Utils_XML::xmlObjToArray($val); | |
163 | } | |
164 | } | |
165 | } | |
166 | ||
e4c4f267 CW |
167 | /** |
168 | * Filter out invalid requirements, e.g. extensions that have been moved to core. | |
169 | * | |
170 | * @param SimpleXMLElement $requirements | |
171 | * @return array | |
172 | */ | |
173 | public function filterRequirements($requirements) { | |
174 | $filtered = []; | |
175 | $compatInfo = CRM_Extension_System::getCompatibilityInfo(); | |
176 | foreach ($requirements->ext as $ext) { | |
177 | $ext = (string) $ext; | |
178 | if (empty($compatInfo[$ext]['obsolete'])) { | |
179 | $filtered[] = $ext; | |
180 | } | |
181 | } | |
182 | return $filtered; | |
183 | } | |
184 | ||
6a488035 | 185 | } |