Use better function for retrieving payment processor for recurring
[civicrm-core.git] / CRM / Extension / Info.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2015
33 */
34 class CRM_Extension_Info {
35
36 /**
37 * Extension info file name.
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 /**
48 * Load extension info an XML file.
49 *
50 * @param $file
51 *
52 * @throws CRM_Extension_Exception_ParseException
53 * @return CRM_Extension_Info
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 /**
67 * Load extension info a string.
68 *
69 * @param string $string
70 * XML content.
71 *
72 * @throws CRM_Extension_Exception_ParseException
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
86 /**
87 * @param null $key
88 * @param null $type
89 * @param null $name
90 * @param null $label
91 * @param null $file
92 */
93 public function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) {
94 $this->key = $key;
95 $this->type = $type;
96 $this->name = $name;
97 $this->label = $label;
98 $this->file = $file;
99 }
100
101 /**
102 * Copy attributes from an XML document to $this
103 *
104 * @param SimpleXMLElement $info
105 */
106 public function parse($info) {
107 $this->key = (string) $info->attributes()->key;
108 $this->type = (string) $info->attributes()->type;
109 $this->file = (string) $info->file;
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 }