From c7f45e3717525aedd0457979caf379d34f80691e Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 24 Oct 2022 13:04:21 +1100 Subject: [PATCH] dev/joomla#41 [REF] Amend Joomla Bootstrapping code and hook code to get cron to work on Joomla 4 --- CRM/Utils/Hook/Joomla.php | 2 +- CRM/Utils/System/Joomla.php | 47 +++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/CRM/Utils/Hook/Joomla.php b/CRM/Utils/Hook/Joomla.php index 77377e3c6d..e844c4abbb 100644 --- a/CRM/Utils/Hook/Joomla.php +++ b/CRM/Utils/Hook/Joomla.php @@ -74,7 +74,7 @@ class CRM_Utils_Hook_Joomla extends CRM_Utils_Hook { $app = JCli::getInstance(); } else { - $app = JApplicationCli::getInstance(); + $app = \Joomla\CMS\Factory::getApplication(); } } diff --git a/CRM/Utils/System/Joomla.php b/CRM/Utils/System/Joomla.php index 9bedbdeff2..e2773fe2f2 100644 --- a/CRM/Utils/System/Joomla.php +++ b/CRM/Utils/System/Joomla.php @@ -520,6 +520,10 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { $version = new JVersion(); return $version->getShortVersion(); } + elseif (class_exists('Version')) { + $version = new Version(); + return $version->getShortVersion(); + } else { return 'Unknown'; } @@ -535,7 +539,11 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { $versionPhp = $joomlaBase . '/libraries/cms/version/version.php'; } require $versionPhp; - $jversion = new JVersion(); + $class = 'jVersion'; + if (!class_exists('jVersion')) { + $class = 'Version'; + } + $jversion = new $class(); define('JVERSION', $jversion->getShortVersion()); } } @@ -578,10 +586,17 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { } // Get the framework. - if (file_exists($joomlaBase . '/libraries/import.legacy.php')) { + if (file_exists($joomlaBase . '/libraries/import.legacy.php') && !file_exists($joomlaBase . '/libraries/bootstrap.php')) { require $joomlaBase . '/libraries/import.legacy.php'; } - require $joomlaBase . '/libraries/cms.php'; + + if (!file_exists($joomlaBase . '/libraries/bootstrap.php')) { + require $joomlaBase . '/libraries/cms.php'; + } + else { + require $joomlaBase . '/libraries/bootstrap.php'; + require_once $joomlaBase . '/includes/framework.php'; + } self::getJVersion($joomlaBase); if (version_compare(JVERSION, '3.8', 'lt')) { @@ -614,11 +629,35 @@ class CRM_Utils_System_Joomla extends CRM_Utils_System_Base { date_default_timezone_set($timezone); CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone(); } + if (version_compare(JVERSION, '4.0', '>=')) { + // Boot the DI container + $container = \Joomla\CMS\Factory::getContainer(); + /* + * Alias the session service keys to the web session service as that is the primary session backend for this application + * + * In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects + * is supported. This includes aliases for aliased class names, and the keys for aliased class names should be considered + * deprecated to be removed when the class name alias is removed as well. + */ + $container->alias('session', 'session.cli') + ->alias('JSession', 'session.cli') + ->alias(\Joomla\CMS\Session\Session::class, 'session.cli') + ->alias(\Joomla\Session\Session::class, 'session.cli') + ->alias(\Joomla\Session\SessionInterface::class, 'session.cli'); + // Instantiate the application. + if (PHP_SAPI == 'cli') { + $app = $container->get(\Joomla\CMS\Application\ConsoleApplication::class); + } + else { + $app = $container->get(\Joomla\CMS\Application\AdministratorApplication::class); + } + // Set the application as global app + \Joomla\CMS\Factory::$application = $app; + } // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes. $config = CRM_Core_Config::singleton(); CRM_Utils_Hook::config($config); - return TRUE; } -- 2.25.1