Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
32 | * @copyright CiviCRM LLC (c) 2004-2015 | |
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 | ||
41 | public $key = NULL; | |
42 | public $type = NULL; | |
43 | public $name = NULL; | |
44 | public $label = NULL; | |
45 | public $file = NULL; | |
46 | ||
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 | ||
e0ef6999 EM |
86 | /** |
87 | * @param null $key | |
88 | * @param null $type | |
89 | * @param null $name | |
90 | * @param null $label | |
91 | * @param null $file | |
92 | */ | |
00be9182 | 93 | public function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) { |
353ffa53 TO |
94 | $this->key = $key; |
95 | $this->type = $type; | |
96 | $this->name = $name; | |
97 | $this->label = $label; | |
98 | $this->file = $file; | |
6a488035 TO |
99 | } |
100 | ||
101 | /** | |
102 | * Copy attributes from an XML document to $this | |
103 | * | |
104 | * @param SimpleXMLElement $info | |
6a488035 TO |
105 | */ |
106 | public function parse($info) { | |
353ffa53 TO |
107 | $this->key = (string) $info->attributes()->key; |
108 | $this->type = (string) $info->attributes()->type; | |
109 | $this->file = (string) $info->file; | |
6a488035 TO |
110 | $this->label = (string) $info->name; |
111 | ||
112 | // Convert first level variables to CRM_Core_Extension properties | |
113 | // and deeper into arrays. An exception for URLS section, since | |
114 | // we want them in special format. | |
115 | foreach ($info as $attr => $val) { | |
116 | if (count($val->children()) == 0) { | |
117 | $this->$attr = (string) $val; | |
118 | } | |
119 | elseif ($attr === 'urls') { | |
120 | $this->urls = array(); | |
121 | foreach ($val->url as $url) { | |
122 | $urlAttr = (string) $url->attributes()->desc; | |
123 | $this->urls[$urlAttr] = (string) $url; | |
124 | } | |
125 | ksort($this->urls); | |
126 | } | |
127 | else { | |
128 | $this->$attr = CRM_Utils_XML::xmlObjToArray($val); | |
129 | } | |
130 | } | |
131 | } | |
132 | ||
133 | } |