Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 32 | * @copyright CiviCRM LLC (c) 2004-2017 |
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 | ||
4025c773 TO |
47 | /** |
48 | * @var array | |
49 | * Each item is a specification like: | |
50 | * array('type'=>'psr4', 'namespace'=>'Foo\Bar', 'path'=>'/foo/bar'). | |
51 | */ | |
52 | public $classloader = array(); | |
53 | ||
6a488035 | 54 | /** |
fe482240 | 55 | * Load extension info an XML file. |
6a488035 | 56 | * |
6c8f6e67 EM |
57 | * @param $file |
58 | * | |
59 | * @throws CRM_Extension_Exception_ParseException | |
6a488035 | 60 | * @return CRM_Extension_Info |
6a488035 TO |
61 | */ |
62 | public static function loadFromFile($file) { | |
63 | list ($xml, $error) = CRM_Utils_XML::parseFile($file); | |
64 | if ($xml === FALSE) { | |
65 | throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $error"); | |
66 | } | |
67 | ||
68 | $instance = new CRM_Extension_Info(); | |
69 | $instance->parse($xml); | |
70 | return $instance; | |
71 | } | |
72 | ||
73 | /** | |
fe482240 | 74 | * Load extension info a string. |
6a488035 | 75 | * |
f41911fd TO |
76 | * @param string $string |
77 | * XML content. | |
77b97be7 EM |
78 | * |
79 | * @throws CRM_Extension_Exception_ParseException | |
6a488035 TO |
80 | * @return CRM_Extension_Info |
81 | */ | |
82 | public static function loadFromString($string) { | |
83 | list ($xml, $error) = CRM_Utils_XML::parseString($string); | |
84 | if ($xml === FALSE) { | |
85 | throw new CRM_Extension_Exception_ParseException("Failed to parse info XML: $string"); | |
86 | } | |
87 | ||
88 | $instance = new CRM_Extension_Info(); | |
89 | $instance->parse($xml); | |
90 | return $instance; | |
91 | } | |
92 | ||
e0ef6999 EM |
93 | /** |
94 | * @param null $key | |
95 | * @param null $type | |
96 | * @param null $name | |
97 | * @param null $label | |
98 | * @param null $file | |
99 | */ | |
00be9182 | 100 | public function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) { |
353ffa53 TO |
101 | $this->key = $key; |
102 | $this->type = $type; | |
103 | $this->name = $name; | |
104 | $this->label = $label; | |
105 | $this->file = $file; | |
6a488035 TO |
106 | } |
107 | ||
108 | /** | |
109 | * Copy attributes from an XML document to $this | |
110 | * | |
111 | * @param SimpleXMLElement $info | |
6a488035 TO |
112 | */ |
113 | public function parse($info) { | |
353ffa53 TO |
114 | $this->key = (string) $info->attributes()->key; |
115 | $this->type = (string) $info->attributes()->type; | |
116 | $this->file = (string) $info->file; | |
6a488035 TO |
117 | $this->label = (string) $info->name; |
118 | ||
119 | // Convert first level variables to CRM_Core_Extension properties | |
120 | // and deeper into arrays. An exception for URLS section, since | |
121 | // we want them in special format. | |
122 | foreach ($info as $attr => $val) { | |
123 | if (count($val->children()) == 0) { | |
124 | $this->$attr = (string) $val; | |
125 | } | |
126 | elseif ($attr === 'urls') { | |
127 | $this->urls = array(); | |
128 | foreach ($val->url as $url) { | |
129 | $urlAttr = (string) $url->attributes()->desc; | |
130 | $this->urls[$urlAttr] = (string) $url; | |
131 | } | |
132 | ksort($this->urls); | |
133 | } | |
4025c773 TO |
134 | elseif ($attr === 'classloader') { |
135 | $this->classloader = array(); | |
136 | foreach ($val->psr4 as $psr4) { | |
137 | $this->classloader[] = array( | |
138 | 'type' => 'psr4', | |
139 | 'prefix' => (string) $psr4->attributes()->prefix, | |
140 | 'path' => (string) $psr4->attributes()->path, | |
141 | ); | |
142 | } | |
143 | } | |
6a488035 TO |
144 | else { |
145 | $this->$attr = CRM_Utils_XML::xmlObjToArray($val); | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | } |