Merge branch 'master' into master-civimail-abtest
[civicrm-core.git] / CRM / Extension / Info.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 * Metadata for an extension (e.g. the extension's "info.xml" file)
30 */
31class CRM_Extension_Info {
32
33 /**
34 * Extension info file name
35 */
36 const FILENAME = 'info.xml';
37
38 public $key = NULL;
39 public $type = NULL;
40 public $name = NULL;
41 public $label = NULL;
42 public $file = NULL;
43
44 /**
45 * Load extension info an XML file
46 *
6c8f6e67
EM
47 * @param $file
48 *
49 * @throws CRM_Extension_Exception_ParseException
6a488035 50 * @return CRM_Extension_Info
6a488035
TO
51 */
52 public static function loadFromFile($file) {
53 list ($xml, $error) = CRM_Utils_XML::parseFile($file);
54 if ($xml === FALSE) {
55 throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $error");
56 }
57
58 $instance = new CRM_Extension_Info();
59 $instance->parse($xml);
60 return $instance;
61 }
62
63 /**
64 * Load extension info a string
65 *
66 * @param string $string XML content
77b97be7
EM
67 *
68 * @throws CRM_Extension_Exception_ParseException
6a488035
TO
69 * @return CRM_Extension_Info
70 */
71 public static function loadFromString($string) {
72 list ($xml, $error) = CRM_Utils_XML::parseString($string);
73 if ($xml === FALSE) {
74 throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $string");
75 }
76
77 $instance = new CRM_Extension_Info();
78 $instance->parse($xml);
79 return $instance;
80 }
81
e0ef6999
EM
82 /**
83 * @param null $key
84 * @param null $type
85 * @param null $name
86 * @param null $label
87 * @param null $file
88 */
6a488035
TO
89 function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) {
90 $this->key = $key;
91 $this->type = $type;
92 $this->name = $name;
93 $this->label = $label;
94 $this->file = $file;
95 }
96
97 /**
98 * Copy attributes from an XML document to $this
99 *
100 * @param SimpleXMLElement $info
101 * @return void
102 */
103 public function parse($info) {
104 $this->key = (string) $info->attributes()->key;
105 $this->type = (string) $info->attributes()->type;
106 $this->file = (string) $info->file;
107 $this->label = (string) $info->name;
108
109 // Convert first level variables to CRM_Core_Extension properties
110 // and deeper into arrays. An exception for URLS section, since
111 // we want them in special format.
112 foreach ($info as $attr => $val) {
113 if (count($val->children()) == 0) {
114 $this->$attr = (string) $val;
115 }
116 elseif ($attr === 'urls') {
117 $this->urls = array();
118 foreach ($val->url as $url) {
119 $urlAttr = (string) $url->attributes()->desc;
120 $this->urls[$urlAttr] = (string) $url;
121 }
122 ksort($this->urls);
123 }
124 else {
125 $this->$attr = CRM_Utils_XML::xmlObjToArray($val);
126 }
127 }
128 }
129
130}