I'm working with a JoomlaCli Application, called from a external php command via a cronjob. It worked in Joomla 3, but in Joomla 4 some classes have changed.
Our class name is import
Namespaces:
use GreenCape\Import\Core\Logger; use GreenCape\Import\Core\Timer; use GreenCape\Import\Exception\BusyException; use GreenCape\Import\Reader\ReaderInterface; use GreenCape\Import\Writer\WriterInterface; use Joomla\Registry\Registry; use Psr\Log\LoggerInterface; // Import namespaced classes use Joomla\CMS\Factory; use Joomla\CMS\Language\LanguageHelper; use Joomla\CMS\Http\HttpFactory; use Joomla\CMS\Version; use Joomla\Console\Application; use Joomla\Console\Command\AbstractCommand;
some inits
// Set flag that this is a parent file. const _JEXEC = 1; error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', 1); date_default_timezone_set('UTC'); // Load system defines if (file_exists(dirname(__DIR__) . '/defines.php')) { require_once dirname(__DIR__) . '/defines.php'; } if (!defined('_JDEFINES')) { define('JPATH_BASE', dirname(__DIR__)); require_once JPATH_BASE . '/includes/defines.php'; } require_once JPATH_LIBRARIES . '/import.legacy.php'; require_once JPATH_LIBRARIES . '/cms.php'; // Load the configuration require_once JPATH_CONFIGURATION . '/configuration.php'; require_once __DIR__ . '/import/autoload.php';
the crucial class definition with the execute definition
class Import extends JApplicationCli { /** @var JInputCli The input */ public $input = null; /** @var LoggerInterface */ protected $logger; private $update = true; private $delete = false; private $quiet = false; private $debug = true; private $verbose = false; private $importConfig; private $type; /** @var Lock */ private $lock; private $force; public function __construct(JInputCli $input = null, Registry $config = null, JEventDispatcher $dispatcher = null) { parent::__construct($input, $config, $dispatcher); } /** * Entry point for the script * * Exit codes: * * - 0: Everything went as expected. * - 1: An exception occurred. * - 2: Import is already running. * * @return void */ public function doExecute() { try { if (!defined('JDEBUG')) { define('JDEBUG', false); } JFactory::$application = $this; $this->handleOptions(); $this->hello(); #$this->logger->debug("Config: " . print_r($this->importConfig, true), ['import']); if (count($this->input->args) != 1) { $this->quiet = false; $this->usage(); } $this->type = $this->input->args[0]; $this->lock = new Lock($this->type); if (!isset($this->importConfig[$this->type])) { $available = implode(', ', array_keys($this->importConfig)); throw new \RuntimeException("Don't know how to import {$this->type}. Known data types are {$available}"); } $this->importConfig[$this->type]['logger'] = $this->logger; $readerClass = $this->importConfig[$this->type]['reader_class']; $writerClass = $this->importConfig[$this->type]['writer_class']; $this->lock->lock($this->force); $this->logger->info("Importing {$this->type}", ['import']); $returnValue = $this->import( new $readerClass($this->importConfig[$this->type]), new $writerClass($this->importConfig[$this->type]) ); $this->lock->unlock(); } catch (BusyException $e) { $this->out($this->type . " import is already running."); $returnValue = 2; } catch (\Exception $e) { $this->out('Exception: ' . $e->getMessage()); $this->logger->error($e->getMessage()); if ($this->debug) { $this->out($e->getTraceAsString()); } $returnValue = 1; $this->lock->unlock(); } $this->close($returnValue); } ...
and finally the instance called
JApplicationCli::getInstance('Import')->execute();
I got a static error,
Class Import contains 1 abstract method and must therefore be declared abstract or implement the remaining methods
so I defined the missing class with an empty function.
Some hours down the line I still can't call the instance, asking myself if the script can be saved anywhere or if it will be necessary to switch from extends JApplicationCli to extends Application for Joomla 5 anyways.
Any suggestions? I can provide the full code if any helpful, just didn't want to spam the thread.
Thank you!