bulk comment fixes
[civicrm-core.git] / CRM / Extension / Info.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 class 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 *
47 * @param $file
48 *
49 * @throws CRM_Extension_Exception_ParseException
50 * @internal param string $string XML content
51 * @return CRM_Extension_Info
52 */
53 public static function loadFromFile($file) {
54 list ($xml, $error) = CRM_Utils_XML::parseFile($file);
55 if ($xml === FALSE) {
56 throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $error");
57 }
58
59 $instance = new CRM_Extension_Info();
60 $instance->parse($xml);
61 return $instance;
62 }
63
64 /**
65 * Load extension info a string
66 *
67 * @param string $string XML content
68 *
69 * @throws CRM_Extension_Exception_ParseException
70 * @return CRM_Extension_Info
71 */
72 public static function loadFromString($string) {
73 list ($xml, $error) = CRM_Utils_XML::parseString($string);
74 if ($xml === FALSE) {
75 throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $string");
76 }
77
78 $instance = new CRM_Extension_Info();
79 $instance->parse($xml);
80 return $instance;
81 }
82
83 function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) {
84 $this->key = $key;
85 $this->type = $type;
86 $this->name = $name;
87 $this->label = $label;
88 $this->file = $file;
89 }
90
91 /**
92 * Copy attributes from an XML document to $this
93 *
94 * @param SimpleXMLElement $info
95 * @return void
96 */
97 public function parse($info) {
98 $this->key = (string) $info->attributes()->key;
99 $this->type = (string) $info->attributes()->type;
100 $this->file = (string) $info->file;
101 $this->label = (string) $info->name;
102
103 // Convert first level variables to CRM_Core_Extension properties
104 // and deeper into arrays. An exception for URLS section, since
105 // we want them in special format.
106 foreach ($info as $attr => $val) {
107 if (count($val->children()) == 0) {
108 $this->$attr = (string) $val;
109 }
110 elseif ($attr === 'urls') {
111 $this->urls = array();
112 foreach ($val->url as $url) {
113 $urlAttr = (string) $url->attributes()->desc;
114 $this->urls[$urlAttr] = (string) $url;
115 }
116 ksort($this->urls);
117 }
118 else {
119 $this->$attr = CRM_Utils_XML::xmlObjToArray($val);
120 }
121 }
122 }
123
124 }