Merge pull request #14014 from MegaphoneJon/reporting-14
[civicrm-core.git] / CRM / Extension / Info.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Metadata for an extension (e.g. the extension's "info.xml" file)
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2019
33 */
34 class CRM_Extension_Info {
35
36 /**
37 * Extension info file name.
38 */
39 const FILENAME = 'info.xml';
40
41 /**
42 * @var string
43 */
44 public $key = NULL;
45 public $type = NULL;
46 public $name = NULL;
47 public $label = NULL;
48 public $file = NULL;
49
50 /**
51 * @var array
52 * Each item is a specification like:
53 * array('type'=>'psr4', 'namespace'=>'Foo\Bar', 'path'=>'/foo/bar').
54 */
55 public $classloader = [];
56
57 /**
58 * @var array
59 * Each item is they key-name of an extension required by this extension.
60 */
61 public $requires = [];
62
63 /**
64 * Load extension info an XML file.
65 *
66 * @param $file
67 *
68 * @throws CRM_Extension_Exception_ParseException
69 * @return CRM_Extension_Info
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 /**
83 * Load extension info a string.
84 *
85 * @param string $string
86 * XML content.
87 *
88 * @throws CRM_Extension_Exception_ParseException
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
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) {
114 $revMap = [];
115 foreach ($infos as $info) {
116 foreach ($info->requires as $key) {
117 $revMap[$key][] = $info;
118 }
119 }
120 return $revMap;
121 }
122
123 /**
124 * @param null $key
125 * @param null $type
126 * @param null $name
127 * @param null $label
128 * @param null $file
129 */
130 public function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) {
131 $this->key = $key;
132 $this->type = $type;
133 $this->name = $name;
134 $this->label = $label;
135 $this->file = $file;
136 }
137
138 /**
139 * Copy attributes from an XML document to $this
140 *
141 * @param SimpleXMLElement $info
142 */
143 public function parse($info) {
144 $this->key = (string) $info->attributes()->key;
145 $this->type = (string) $info->attributes()->type;
146 $this->file = (string) $info->file;
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') {
157 $this->urls = [];
158 foreach ($val->url as $url) {
159 $urlAttr = (string) $url->attributes()->desc;
160 $this->urls[$urlAttr] = (string) $url;
161 }
162 ksort($this->urls);
163 }
164 elseif ($attr === 'classloader') {
165 $this->classloader = [];
166 foreach ($val->psr4 as $psr4) {
167 $this->classloader[] = [
168 'type' => 'psr4',
169 'prefix' => (string) $psr4->attributes()->prefix,
170 'path' => (string) $psr4->attributes()->path,
171 ];
172 }
173 }
174 elseif ($attr === 'requires') {
175 $this->requires = $this->filterRequirements($val);
176 }
177 else {
178 $this->$attr = CRM_Utils_XML::xmlObjToArray($val);
179 }
180 }
181 }
182
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
201 }