3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
13 * Metadata for an extension (e.g. the extension's "info.xml" file)
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 class CRM_Extension_Info
{
21 * Extension info file name.
23 const FILENAME
= 'info.xml';
36 * Each item is a specification like:
37 * array('type'=>'psr4', 'namespace'=>'Foo\Bar', 'path'=>'/foo/bar').
39 public $classloader = [];
43 * Each item is they key-name of an extension required by this extension.
45 public $requires = [];
49 * List of strings (tag-names).
54 * Load extension info an XML file.
58 * @throws CRM_Extension_Exception_ParseException
59 * @return CRM_Extension_Info
61 public static function loadFromFile($file) {
62 list ($xml, $error) = CRM_Utils_XML
::parseFile($file);
64 throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $error");
67 $instance = new CRM_Extension_Info();
68 $instance->parse($xml);
73 * Load extension info a string.
75 * @param string $string
78 * @throws CRM_Extension_Exception_ParseException
79 * @return CRM_Extension_Info
81 public static function loadFromString($string) {
82 list ($xml, $error) = CRM_Utils_XML
::parseString($string);
84 throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $string");
87 $instance = new CRM_Extension_Info();
88 $instance->parse($xml);
93 * Build a reverse-dependency map.
96 * The universe of available extensions.
97 * Ex: $infos['org.civicrm.foobar'] = new CRM_Extension_Info().
99 * If "org.civicrm.api" is required by "org.civicrm.foo", then return
100 * array('org.civicrm.api' => array(CRM_Extension_Info[org.civicrm.foo])).
101 * Array(string $key => array $requiredBys).
103 public static function buildReverseMap($infos) {
105 foreach ($infos as $info) {
106 foreach ($info->requires
as $key) {
107 $revMap[$key][] = $info;
120 public function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) {
124 $this->label
= $label;
129 * Copy attributes from an XML document to $this
131 * @param SimpleXMLElement $info
133 public function parse($info) {
134 $this->key
= (string) $info->attributes()->key
;
135 $this->type
= (string) $info->attributes()->type
;
136 $this->file
= (string) $info->file
;
137 $this->label
= (string) $info->name
;
139 // Convert first level variables to CRM_Core_Extension properties
140 // and deeper into arrays. An exception for URLS section, since
141 // we want them in special format.
142 foreach ($info as $attr => $val) {
143 if (count($val->children()) == 0) {
144 $this->$attr = (string) $val;
146 elseif ($attr === 'urls') {
148 foreach ($val->url
as $url) {
149 $urlAttr = (string) $url->attributes()->desc
;
150 $this->urls
[$urlAttr] = (string) $url;
154 elseif ($attr === 'classloader') {
155 $this->classloader
= [];
156 foreach ($val->psr4
as $psr4) {
157 $this->classloader
[] = [
159 'prefix' => (string) $psr4->attributes()->prefix
,
160 'path' => (string) $psr4->attributes()->path
,
164 elseif ($attr === 'tags') {
166 foreach ($val->tag
as $tag) {
167 $this->tags
[] = (string) $tag;
170 elseif ($attr === 'requires') {
171 $this->requires
= $this->filterRequirements($val);
174 $this->$attr = CRM_Utils_XML
::xmlObjToArray($val);
180 * Filter out invalid requirements, e.g. extensions that have been moved to core.
182 * @param SimpleXMLElement $requirements
185 public function filterRequirements($requirements) {
187 $compatInfo = CRM_Extension_System
::getCompatibilityInfo();
188 foreach ($requirements->ext
as $ext) {
189 $ext = (string) $ext;
190 if (empty($compatInfo[$ext]['obsolete'])) {