| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 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-2017 |
| 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 | * @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 | |
| 54 | /** |
| 55 | * Load extension info an XML file. |
| 56 | * |
| 57 | * @param $file |
| 58 | * |
| 59 | * @throws CRM_Extension_Exception_ParseException |
| 60 | * @return CRM_Extension_Info |
| 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 | /** |
| 74 | * Load extension info a string. |
| 75 | * |
| 76 | * @param string $string |
| 77 | * XML content. |
| 78 | * |
| 79 | * @throws CRM_Extension_Exception_ParseException |
| 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 | |
| 93 | /** |
| 94 | * @param null $key |
| 95 | * @param null $type |
| 96 | * @param null $name |
| 97 | * @param null $label |
| 98 | * @param null $file |
| 99 | */ |
| 100 | public function __construct($key = NULL, $type = NULL, $name = NULL, $label = NULL, $file = NULL) { |
| 101 | $this->key = $key; |
| 102 | $this->type = $type; |
| 103 | $this->name = $name; |
| 104 | $this->label = $label; |
| 105 | $this->file = $file; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Copy attributes from an XML document to $this |
| 110 | * |
| 111 | * @param SimpleXMLElement $info |
| 112 | */ |
| 113 | public function parse($info) { |
| 114 | $this->key = (string) $info->attributes()->key; |
| 115 | $this->type = (string) $info->attributes()->type; |
| 116 | $this->file = (string) $info->file; |
| 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 | } |
| 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 | } |
| 144 | else { |
| 145 | $this->$attr = CRM_Utils_XML::xmlObjToArray($val); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | } |