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