From 8d1863c4ebdd6ea32f64ad0debe25f370267e520 Mon Sep 17 00:00:00 2001 From: Phil Date: Mon, 6 Jan 2025 15:33:13 +0100 Subject: [PATCH] CiviX Upgrade #77 Civix upgrade should fix install error --- de.systopia.civiproxy/civiproxy.civix.php | 123 ++---------------- de.systopia.civiproxy/civiproxy.php | 45 ------- de.systopia.civiproxy/info.xml | 10 +- .../mixin/entity-types-php@2.0.0.mixin.php | 40 ++++++ .../mixin/menu-xml@1.0.0.mixin.php | 31 ----- de.systopia.civiproxy/mixin/polyfill.php | 101 -------------- .../mixin/setting-php@1.0.0.mixin.php | 32 ----- .../mixin/smarty-v2@1.0.3.mixin.php | 78 +++++++++++ 8 files changed, 134 insertions(+), 326 deletions(-) create mode 100644 de.systopia.civiproxy/mixin/entity-types-php@2.0.0.mixin.php delete mode 100644 de.systopia.civiproxy/mixin/menu-xml@1.0.0.mixin.php delete mode 100644 de.systopia.civiproxy/mixin/polyfill.php delete mode 100644 de.systopia.civiproxy/mixin/setting-php@1.0.0.mixin.php create mode 100644 de.systopia.civiproxy/mixin/smarty-v2@1.0.3.mixin.php diff --git a/de.systopia.civiproxy/civiproxy.civix.php b/de.systopia.civiproxy/civiproxy.civix.php index 389f2ae..7ca1eb0 100644 --- a/de.systopia.civiproxy/civiproxy.civix.php +++ b/de.systopia.civiproxy/civiproxy.civix.php @@ -75,44 +75,27 @@ class CRM_Civiproxy_ExtensionUtil { return self::CLASS_PREFIX . '_' . str_replace('\\', '_', $suffix); } + } use CRM_Civiproxy_ExtensionUtil as E; -function _civiproxy_civix_mixin_polyfill() { - if (!class_exists('CRM_Extension_MixInfo')) { - $polyfill = __DIR__ . '/mixin/polyfill.php'; - (require $polyfill)(E::LONG_NAME, E::SHORT_NAME, E::path()); - } -} - /** * (Delegated) Implements hook_civicrm_config(). * * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config */ -function _civiproxy_civix_civicrm_config(&$config = NULL) { +function _civiproxy_civix_civicrm_config($config = NULL) { static $configured = FALSE; if ($configured) { return; } $configured = TRUE; - $template = CRM_Core_Smarty::singleton(); - $extRoot = __DIR__ . DIRECTORY_SEPARATOR; - $extDir = $extRoot . 'templates'; - - if (is_array($template->template_dir)) { - array_unshift($template->template_dir, $extDir); - } - else { - $template->template_dir = [$extDir, $template->template_dir]; - } - $include_path = $extRoot . PATH_SEPARATOR . get_include_path(); set_include_path($include_path); - _civiproxy_civix_mixin_polyfill(); + // Based on , this does not currently require mixin/polyfill.php. } /** @@ -122,36 +105,7 @@ function _civiproxy_civix_civicrm_config(&$config = NULL) { */ function _civiproxy_civix_civicrm_install() { _civiproxy_civix_civicrm_config(); - if ($upgrader = _civiproxy_civix_upgrader()) { - $upgrader->onInstall(); - } - _civiproxy_civix_mixin_polyfill(); -} - -/** - * Implements hook_civicrm_postInstall(). - * - * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall - */ -function _civiproxy_civix_civicrm_postInstall() { - _civiproxy_civix_civicrm_config(); - if ($upgrader = _civiproxy_civix_upgrader()) { - if (is_callable([$upgrader, 'onPostInstall'])) { - $upgrader->onPostInstall(); - } - } -} - -/** - * Implements hook_civicrm_uninstall(). - * - * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall - */ -function _civiproxy_civix_civicrm_uninstall() { - _civiproxy_civix_civicrm_config(); - if ($upgrader = _civiproxy_civix_upgrader()) { - $upgrader->onUninstall(); - } + // Based on , this does not currently require mixin/polyfill.php. } /** @@ -159,59 +113,9 @@ function _civiproxy_civix_civicrm_uninstall() { * * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable */ -function _civiproxy_civix_civicrm_enable() { +function _civiproxy_civix_civicrm_enable(): void { _civiproxy_civix_civicrm_config(); - if ($upgrader = _civiproxy_civix_upgrader()) { - if (is_callable([$upgrader, 'onEnable'])) { - $upgrader->onEnable(); - } - } - _civiproxy_civix_mixin_polyfill(); -} - -/** - * (Delegated) Implements hook_civicrm_disable(). - * - * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable - * @return mixed - */ -function _civiproxy_civix_civicrm_disable() { - _civiproxy_civix_civicrm_config(); - if ($upgrader = _civiproxy_civix_upgrader()) { - if (is_callable([$upgrader, 'onDisable'])) { - $upgrader->onDisable(); - } - } -} - -/** - * (Delegated) Implements hook_civicrm_upgrade(). - * - * @param $op string, the type of operation being performed; 'check' or 'enqueue' - * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks - * - * @return mixed - * based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending) - * for 'enqueue', returns void - * - * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade - */ -function _civiproxy_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { - if ($upgrader = _civiproxy_civix_upgrader()) { - return $upgrader->onUpgrade($op, $queue); - } -} - -/** - * @return CRM_Civiproxy_Upgrader - */ -function _civiproxy_civix_upgrader() { - if (!file_exists(__DIR__ . '/CRM/Civiproxy/Upgrader.php')) { - return NULL; - } - else { - return CRM_Civiproxy_Upgrader_Base::instance(); - } + // Based on , this does not currently require mixin/polyfill.php. } /** @@ -230,8 +134,8 @@ function _civiproxy_civix_insert_navigation_menu(&$menu, $path, $item) { if (empty($path)) { $menu[] = [ 'attributes' => array_merge([ - 'label' => CRM_Utils_Array::value('name', $item), - 'active' => 1, + 'label' => $item['name'] ?? NULL, + 'active' => 1, ], $item), ]; return TRUE; @@ -295,14 +199,3 @@ function _civiproxy_civix_fixNavigationMenuItems(&$nodes, &$maxNavID, $parentID) } } } - -/** - * (Delegated) Implements hook_civicrm_entityTypes(). - * - * Find any *.entityType.php files, merge their content, and return. - * - * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes - */ -function _civiproxy_civix_civicrm_entityTypes(&$entityTypes) { - $entityTypes = array_merge($entityTypes, []); -} diff --git a/de.systopia.civiproxy/civiproxy.php b/de.systopia.civiproxy/civiproxy.php index a368693..07c0eea 100644 --- a/de.systopia.civiproxy/civiproxy.php +++ b/de.systopia.civiproxy/civiproxy.php @@ -32,13 +32,6 @@ function civiproxy_civicrm_install() { return _civiproxy_civix_civicrm_install(); } -/** - * Implementation of hook_civicrm_uninstall - */ -function civiproxy_civicrm_uninstall() { - return _civiproxy_civix_civicrm_uninstall(); -} - /** * Implementation of hook_civicrm_enable */ @@ -46,47 +39,9 @@ function civiproxy_civicrm_enable() { return _civiproxy_civix_civicrm_enable(); } -/** - * Implementation of hook_civicrm_disable - */ -function civiproxy_civicrm_disable() { - return _civiproxy_civix_civicrm_disable(); -} - -/** - * Implementation of hook_civicrm_upgrade - * - * @param $op string, the type of operation being performed; 'check' or 'enqueue' - * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks - * - * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending) - * for 'enqueue', returns void - */ -function civiproxy_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { - return _civiproxy_civix_civicrm_upgrade($op, $queue); -} - /** * Implementation of hook_civicrm_alterSettingsFolders * * Scan for settings in custom folder and import them * */ - -/** - * Implements hook_civicrm_postInstall(). - * - * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall - */ -function civiproxy_civicrm_postInstall() { - _civiproxy_civix_civicrm_postInstall(); -} - -/** - * Implements hook_civicrm_entityTypes(). - * - * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes - */ -function civiproxy_civicrm_entityTypes(&$entityTypes) { - _civiproxy_civix_civicrm_entityTypes($entityTypes); -} diff --git a/de.systopia.civiproxy/info.xml b/de.systopia.civiproxy/info.xml index 7baa989..6fe8c07 100644 --- a/de.systopia.civiproxy/info.xml +++ b/de.systopia.civiproxy/info.xml @@ -18,15 +18,21 @@ 1.0.0-alpha2 dev - 5.0 + 5.45 This is the companion extension to SYSTOPIA's CiviProxy security system CRM/Civiproxy - 22.05.2 + 24.09.1 menu-xml@1.0.0 setting-php@1.0.0 + smarty-v2@1.0.3 + entity-types-php@2.0.0 + + + + diff --git a/de.systopia.civiproxy/mixin/entity-types-php@2.0.0.mixin.php b/de.systopia.civiproxy/mixin/entity-types-php@2.0.0.mixin.php new file mode 100644 index 0000000..54616e6 --- /dev/null +++ b/de.systopia.civiproxy/mixin/entity-types-php@2.0.0.mixin.php @@ -0,0 +1,40 @@ +addListener('hook_civicrm_entityTypes', function ($e) use ($mixInfo) { + // When deactivating on a polyfill/pre-mixin system, listeners may not cleanup automatically. + if (!$mixInfo->isActive() || !is_dir($mixInfo->getPath('schema'))) { + return; + } + + $files = (array) glob($mixInfo->getPath('schema/*.entityType.php')); + foreach ($files as $file) { + $entity = include $file; + $entity['module'] = $mixInfo->longName; + $e->entityTypes[$entity['name']] = $entity; + } + }); + +}; diff --git a/de.systopia.civiproxy/mixin/menu-xml@1.0.0.mixin.php b/de.systopia.civiproxy/mixin/menu-xml@1.0.0.mixin.php deleted file mode 100644 index 4c0b227..0000000 --- a/de.systopia.civiproxy/mixin/menu-xml@1.0.0.mixin.php +++ /dev/null @@ -1,31 +0,0 @@ -addListener('hook_civicrm_xmlMenu', function ($e) use ($mixInfo) { - if (!$mixInfo->isActive()) { - return; - } - - $files = (array) glob($mixInfo->getPath('xml/Menu/*.xml')); - foreach ($files as $file) { - $e->files[] = $file; - } - }); - -}; diff --git a/de.systopia.civiproxy/mixin/polyfill.php b/de.systopia.civiproxy/mixin/polyfill.php deleted file mode 100644 index f57c5eb..0000000 --- a/de.systopia.civiproxy/mixin/polyfill.php +++ /dev/null @@ -1,101 +0,0 @@ -')) { - $mixinVers[$name] = $ver; - } - } - $mixins = []; - foreach ($mixinVers as $name => $ver) { - $mixins[] = "$name@$ver"; - } - - // Imitate CRM_Extension_MixInfo. - $mixInfo = new class() { - - /** - * @var string - */ - public $longName; - - /** - * @var string - */ - public $shortName; - - public $_basePath; - - public function getPath($file = NULL) { - return $this->_basePath . ($file === NULL ? '' : (DIRECTORY_SEPARATOR . $file)); - } - - public function isActive() { - return \CRM_Extension_System::singleton()->getMapper()->isActiveModule($this->shortName); - } - - }; - $mixInfo->longName = $longName; - $mixInfo->shortName = $shortName; - $mixInfo->_basePath = $basePath; - - // Imitate CRM_Extension_BootCache. - $bootCache = new class() { - - public function define($name, $callback) { - $envId = \CRM_Core_Config_Runtime::getId(); - $oldExtCachePath = \Civi::paths()->getPath("[civicrm.compile]/CachedExtLoader.{$envId}.php"); - $stat = stat($oldExtCachePath); - $file = Civi::paths()->getPath('[civicrm.compile]/CachedMixin.' . md5($name . ($stat['mtime'] ?? 0)) . '.php'); - if (file_exists($file)) { - return include $file; - } - else { - $data = $callback(); - file_put_contents($file, '<' . "?php\nreturn " . var_export($data, 1) . ';'); - return $data; - } - } - - }; - - // Imitate CRM_Extension_MixinLoader::run() - // Parse all live mixins before trying to scan any classes. - global $_CIVIX_MIXIN_POLYFILL; - foreach ($mixins as $mixin) { - // If the exact same mixin is defined by multiple exts, just use the first one. - if (!isset($_CIVIX_MIXIN_POLYFILL[$mixin])) { - $_CIVIX_MIXIN_POLYFILL[$mixin] = include_once $basePath . '/mixin/' . $mixin . '.mixin.php'; - } - } - foreach ($mixins as $mixin) { - // If there's trickery about installs/uninstalls/resets, then we may need to register a second time. - if (!isset(\Civi::$statics[__FUNCTION__][$mixin])) { - \Civi::$statics[__FUNCTION__][$mixin] = 1; - $func = $_CIVIX_MIXIN_POLYFILL[$mixin]; - $func($mixInfo, $bootCache); - } - } -}; diff --git a/de.systopia.civiproxy/mixin/setting-php@1.0.0.mixin.php b/de.systopia.civiproxy/mixin/setting-php@1.0.0.mixin.php deleted file mode 100644 index 7195af4..0000000 --- a/de.systopia.civiproxy/mixin/setting-php@1.0.0.mixin.php +++ /dev/null @@ -1,32 +0,0 @@ -addListener('hook_civicrm_alterSettingsFolders', function ($e) use ($mixInfo) { - // When deactivating on a polyfill/pre-mixin system, listeners may not cleanup automatically. - if (!$mixInfo->isActive()) { - return; - } - - $settingsDir = $mixInfo->getPath('settings'); - if (!in_array($settingsDir, $e->settingsFolders) && is_dir($settingsDir)) { - $e->settingsFolders[] = $settingsDir; - } - }); - -}; diff --git a/de.systopia.civiproxy/mixin/smarty-v2@1.0.3.mixin.php b/de.systopia.civiproxy/mixin/smarty-v2@1.0.3.mixin.php new file mode 100644 index 0000000..f8718c8 --- /dev/null +++ b/de.systopia.civiproxy/mixin/smarty-v2@1.0.3.mixin.php @@ -0,0 +1,78 @@ +getPath('templates'); + if (!file_exists($dir)) { + return; + } + + $register = function($newDirs) { + $smarty = CRM_Core_Smarty::singleton(); + $v2 = isset($smarty->_version) && version_compare($smarty->_version, 3, '<'); + $templateDirs = (array) ($v2 ? $smarty->template_dir : $smarty->getTemplateDir()); + $templateDirs = array_merge($newDirs, $templateDirs); + $templateDirs = array_unique(array_map(function($v) { + $v = str_replace(DIRECTORY_SEPARATOR, '/', $v); + $v = rtrim($v, '/') . '/'; + return $v; + }, $templateDirs)); + if ($v2) { + $smarty->template_dir = $templateDirs; + } + else { + $smarty->setTemplateDir($templateDirs); + } + }; + + // Let's figure out what environment we're in -- so that we know the best way to call $register(). + + if (!empty($GLOBALS['_CIVIX_MIXIN_POLYFILL'])) { + // Polyfill Loader (v<=5.45): We're already in the middle of firing `hook_config`. + if ($mixInfo->isActive()) { + $register([$dir]); + } + return; + } + + if (CRM_Extension_System::singleton()->getManager()->extensionIsBeingInstalledOrEnabled($mixInfo->longName)) { + // New Install, Standard Loader: The extension has just been enabled, and we're now setting it up. + // System has already booted. New templates may be needed for upcoming installation steps. + $register([$dir]); + return; + } + + // Typical Pageview, Standard Loader: Defer the actual registration for a moment -- to ensure that Smarty is online. + // We need to bundle-up all dirs -- Smarty 3/4/5 is inefficient with processing repeated calls to `getTemplateDir()`+`setTemplateDir()` + if (!isset(Civi::$statics[__FILE__]['event'])) { + Civi::$statics[__FILE__]['event'] = 'civi.smarty-v2.addPaths.' . md5(__FILE__); + Civi::dispatcher()->addListener('hook_civicrm_config', function() use ($register) { + $dirs = []; + $event = \Civi\Core\Event\GenericHookEvent::create(['dirs' => &$dirs]); + Civi::dispatcher()->dispatch(Civi::$statics[__FILE__]['event'], $event); + $register($dirs); + }); + } + + Civi::dispatcher()->addListener(Civi::$statics[__FILE__]['event'], function($event) use ($mixInfo, $dir) { + if ($mixInfo->isActive()) { + array_unshift($event->dirs, $dir); + } + }); + +};