From 3d0e24ec26fda7015081abcbdab29cd34e86ebc6 Mon Sep 17 00:00:00 2001 From: vivekarora Date: Thu, 21 Nov 2013 18:12:07 +0530 Subject: [PATCH] Post install hook --- CRM/Case/Info.php | 1 + CRM/Core/Component.php | 4 ++++ CRM/Extension/Manager.php | 22 +++++++++++++++++++ CRM/Extension/Manager/Module.php | 4 ++++ .../moduletest.php | 7 ++++++ .../CRM/Extension/Manager/ModuleTest.php | 8 +++++++ 6 files changed, 46 insertions(+) diff --git a/CRM/Case/Info.php b/CRM/Case/Info.php index 02bb0e043c..6d28cc622f 100644 --- a/CRM/Case/Info.php +++ b/CRM/Case/Info.php @@ -172,6 +172,7 @@ class CRM_Case_Info extends CRM_Core_Component_Info { $config = CRM_Core_Config::singleton(); CRM_Admin_Form_Setting_Component::loadCaseSampleData($config->dsn, $config->sqlDir . 'case_sample.mysql'); CRM_Admin_Form_Setting_Component::loadCaseSampleData($config->dsn, $config->sqlDir . 'case_sample1.mysql'); + CRM_Core_Component::flushEnabledComponents(); if (!CRM_Case_BAO_Case::createCaseViews()) { $msg = ts("Could not create the MySQL views for CiviCase. Your mysql user needs to have the 'CREATE VIEW' permission"); CRM_Core_Error::fatal($msg); diff --git a/CRM/Core/Component.php b/CRM/Core/Component.php index f10fe34c97..48046ce8e6 100644 --- a/CRM/Core/Component.php +++ b/CRM/Core/Component.php @@ -98,6 +98,10 @@ class CRM_Core_Component { static public function &getEnabledComponents($force = FALSE) { return self::_info($force); } + + static public function &flushEnabledComponents() { + self::getEnabledComponents(TRUE); + } public static function &getNames($translated = FALSE) { $allComponents = self::getComponents(); diff --git a/CRM/Extension/Manager.php b/CRM/Extension/Manager.php index e7b74facca..8c89a7a4a7 100644 --- a/CRM/Extension/Manager.php +++ b/CRM/Extension/Manager.php @@ -229,6 +229,28 @@ class CRM_Extension_Manager { $this->statuses = NULL; $this->mapper->refresh(); CRM_Core_Invoke::rebuildMenuAndCaches(TRUE); + + foreach ($keys as $key) { + list ($info, $typeManager) = $this->_getInfoTypeHandler($key); // throws Exception + //print_r(array('post post?', $info, 'k' => $key, 'os'=> $origStatuses[$key])); + + switch ($origStatuses[$key]) { + case self::STATUS_INSTALLED: + // ok, nothing to do + break; + case self::STATUS_DISABLED: + // re-enable it + break; + case self::STATUS_UNINSTALLED: + // install anew + $typeManager->onPostPostInstall($info); + break; + case self::STATUS_UNKNOWN: + default: + throw new CRM_Extension_Exception("Cannot install or enable extension: $key"); + } + } + } /** diff --git a/CRM/Extension/Manager/Module.php b/CRM/Extension/Manager/Module.php index 0d6a9633df..109832d3c9 100644 --- a/CRM/Extension/Manager/Module.php +++ b/CRM/Extension/Manager/Module.php @@ -45,6 +45,10 @@ class CRM_Extension_Manager_Module extends CRM_Extension_Manager_Base { $this->callHook($info, 'enable'); } + public function onPostPostInstall(CRM_Extension_Info $info) { + $this->callHook($info, 'postInstall'); + } + private function callHook(CRM_Extension_Info $info, $hookName) { try { $file = $this->mapper->keyToPath($info->key); diff --git a/tests/extensions/test.extension.manager.moduletest/moduletest.php b/tests/extensions/test.extension.manager.moduletest/moduletest.php index 94a68d26d7..b08e1e7d2f 100644 --- a/tests/extensions/test.extension.manager.moduletest/moduletest.php +++ b/tests/extensions/test.extension.manager.moduletest/moduletest.php @@ -7,6 +7,13 @@ function moduletest_civicrm_install() { CRM_Extension_Manager_ModuleTest::incHookCount('moduletest', 'install'); } +/** + * Implemenation of hook_civicrm_postInstall + */ +function moduletest_civicrm_postInstall() { + CRM_Extension_Manager_ModuleTest::incHookCount('moduletest', 'postInstall'); +} + /** * Implemenation of hook_civicrm_uninstall */ diff --git a/tests/phpunit/CRM/Extension/Manager/ModuleTest.php b/tests/phpunit/CRM/Extension/Manager/ModuleTest.php index beb23c69d2..b81969494c 100644 --- a/tests/phpunit/CRM/Extension/Manager/ModuleTest.php +++ b/tests/phpunit/CRM/Extension/Manager/ModuleTest.php @@ -36,6 +36,7 @@ class CRM_Extension_Manager_ModuleTest extends CiviUnitTestCase { $manager->install(array('test.extension.manager.moduletest')); $this->assertHookCounts('moduletest', array( 'install' => 1, + 'postInstall' => 1, 'enable' => 1, 'disable' => 0, 'uninstall' => 0, @@ -46,6 +47,7 @@ class CRM_Extension_Manager_ModuleTest extends CiviUnitTestCase { $manager->disable(array('test.extension.manager.moduletest')); $this->assertHookCounts('moduletest', array( 'install' => 1, + 'postInstall' => 1, 'enable' => 1, 'disable' => 1, 'uninstall' => 0, @@ -56,6 +58,7 @@ class CRM_Extension_Manager_ModuleTest extends CiviUnitTestCase { $manager->uninstall(array('test.extension.manager.moduletest')); $this->assertHookCounts('moduletest', array( 'install' => 1, + 'postInstall' => 1, 'enable' => 1, 'disable' => 1, 'uninstall' => 1, @@ -249,6 +252,7 @@ class CRM_Extension_Manager_ModuleTest extends CiviUnitTestCase { $basedir = $this->basedir; mkdir("$basedir/$key"); file_put_contents("$basedir/$key/info.xml", "$file"); + print_r(array('creating extension', strtr($template, array('_FILE_' => $file)))); file_put_contents("$basedir/$key/$file.php", strtr($template, array('_FILE_' => $file))); $this->system->getCache()->flush(); $this->system->getManager()->refresh(); @@ -265,6 +269,10 @@ function _FILE__civicrm_install() { CRM_Extension_Manager_ModuleTest::incHookCount('_FILE_', 'install'); } +function _FILE__civicrm_postInstall() { + CRM_Extension_Manager_ModuleTest::incHookCount('_FILE_', 'postInstall'); +} + function _FILE__civicrm_uninstall() { CRM_Extension_Manager_ModuleTest::incHookCount('_FILE_', 'uninstall'); } -- 2.25.1