From f21a105f51a273f3e280fab79f53b66e58ca074b Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 24 Mar 2016 20:44:08 -0700 Subject: [PATCH] CRM-16243 - CRM_Extension_Info - Parse `` tag --- CRM/Extension/Info.php | 33 ++++++++++++++++++++++++ tests/phpunit/CRM/Extension/InfoTest.php | 16 ++++++++++++ 2 files changed, 49 insertions(+) diff --git a/CRM/Extension/Info.php b/CRM/Extension/Info.php index 53aff153da..0f672d33bb 100644 --- a/CRM/Extension/Info.php +++ b/CRM/Extension/Info.php @@ -51,6 +51,12 @@ class CRM_Extension_Info { */ public $classloader = array(); + /** + * @var array + * Each item is they key-name of an extension required by this extension. + */ + public $requires = array(); + /** * Load extension info an XML file. * @@ -90,6 +96,27 @@ class CRM_Extension_Info { return $instance; } + /** + * Build a reverse-dependency map. + * + * @param array $infos + * The universe of available extensions. + * Ex: $infos['org.civicrm.foobar'] = new CRM_Extension_Info(). + * @return array + * If "org.civicrm.api" is required by "org.civicrm.foo", then return + * array('org.civicrm.api' => array(CRM_Extension_Info[org.civicrm.foo])). + * Array(string $key => array $requiredBys). + */ + public static function buildReverseMap($infos) { + $revMap = array(); + foreach ($infos as $info) { + foreach ($info->requires as $key) { + $revMap[$key][] = $info; + } + } + return $revMap; + } + /** * @param null $key * @param null $type @@ -141,6 +168,12 @@ class CRM_Extension_Info { ); } } + elseif ($attr === 'requires') { + $this->requires = array(); + foreach ($val->ext as $ext) { + $this->requires[] = (string) $ext; + } + } else { $this->$attr = CRM_Utils_XML::xmlObjToArray($val); } diff --git a/tests/phpunit/CRM/Extension/InfoTest.php b/tests/phpunit/CRM/Extension/InfoTest.php index 469b895465..0ae7fba1c0 100644 --- a/tests/phpunit/CRM/Extension/InfoTest.php +++ b/tests/phpunit/CRM/Extension/InfoTest.php @@ -49,6 +49,22 @@ class CRM_Extension_InfoTest extends CiviUnitTestCase { $this->assertEquals('test.foo', $info->key); $this->assertEquals('foo', $info->file); $this->assertEquals('zamboni', $info->typeInfo['extra']); + $this->assertEquals(array(), $info->requires); + } + + public function testGood_string_extras() { + $data = "testbar + + org.civicrm.aorg.civicrm.b + + "; + + $info = CRM_Extension_Info::loadFromString($data); + $this->assertEquals('test.bar', $info->key); + $this->assertEquals('testbar', $info->file); + $this->assertEquals('Civi\\', $info->classloader[0]['prefix']); + $this->assertEquals('Civi', $info->classloader[0]['path']); + $this->assertEquals(array('org.civicrm.a', 'org.civicrm.b'), $info->requires); } public function testBad_string() { -- 2.25.1