I have been using this doc to convert a client's CLi news import script from Joomla 3 to 4. The code has been stripped of all the JSON to article code for brevity.
I have gone over all of my code and files, but cannot figure out why the issue is occurring. I had tested the plugin using the Hello World example and it worked without issues. I used the same code with updated names and other details, but this is when the issues started to occur.
Error output from the terminal when running php joomla.php from within the cli directory
Symfony\Component\ErrorHandler\Error\ClassNotFoundError^ {#997
#message: """
Attempted to load class "DtnnewsConsolePlugin" from namespace "Ploszmarketing\Plugin\Console\Dtnnews\Extension".\n
Did you forget a "use" statement for another namespace?
"""
#code: 0
#file: "/var/www/upgrade/plugins/console/dtnnews_cli/services/provider.php"
#line: 29
trace: {
/var/www/upgrade/plugins/console/dtnnews_cli/services/provider.php:29 {
Joomla\DI\ServiceProviderInterface@anonymous\x00/var/www/upgrade/plugins/console/dtnnews_cli/services/provider.php:17$aa->{closure}^
› $dispatcher = $container->get(DispatcherInterface::class);
› $plugin = new DtnnewsConsolePlugin(
› $dispatcher,
}
/var/www/upgrade/libraries/vendor/joomla/di/src/ContainerResource.php:182 { …}
/var/www/upgrade/libraries/vendor/joomla/di/src/Container.php:96 { …}
/var/www/upgrade/libraries/src/Extension/ExtensionManagerTrait.php:177 { …}
/var/www/upgrade/libraries/src/Extension/ExtensionManagerTrait.php:99 { …}
/var/www/upgrade/libraries/src/Plugin/PluginHelper.php:232 { …}
/var/www/upgrade/libraries/src/Plugin/PluginHelper.php:192 { …}
/var/www/upgrade/libraries/src/Application/ConsoleApplication.php:257 { …}
/var/www/upgrade/cli/joomla.php:76 { …}
}
}
plg_dtnnews_cli > services > provider.php
<?php
/**
* @package dtnnews_cli
* @copyright Copyright (c) 2007 - Now Imagine Creative. All rights reserved.
* @license GNU General Public License version 3, or later
*/
defined('_JEXEC') or die;
use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Ploszmarketing\Plugin\Console\Dtnnews\Extension\DtnnewsConsolePlugin;
return new class implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
* @param Container $container The DI container.
* @return void
* @since 4.2.0
*/
public function register(Container $container) {
$container->set(
PluginInterface::class,
function (Container $container) {
$dispatcher = $container->get(DispatcherInterface::class);
$plugin = new DtnnewsConsolePlugin(
$dispatcher,
(array) PluginHelper::getPlugin('console', 'dtnnews_cli')
);
return $plugin;
}
);
}
};
plg_dtnnews_cli > src > CliCommand > RunDtnnewsCommand.php
<?php
/**
* @package dtnnews_cli
* @copyright Copyright (c) 2007 - Now Imagine Creative. All rights reserved.
* @license GNU General Public License version 3, or later
*/
namespace Ploszmarketing\Plugin\Console\Dtnnews\CliCommand;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Joomla\Console\Command\AbstractCommand;
class RunDtnnewsCommand extends AbstractCommand {
/**
* The default command name
*
* @var string
* @since 4.0.0
*/
protected static $defaultName = 'dtn:import';
/**
* Internal function to execute the command.
*
* @param InputInterface $input The input to inject into the command.
* @param OutputInterface $output The output to inject into the command.
*
* @return integer The command exit code
*
* @since 4.0.0
*/
protected function doExecute(InputInterface $input, OutputInterface $output): int {
$symfonyStyle = new SymfonyStyle($input, $output);
$symfonyStyle->title('DTN News Command Title');
// You might want to do some stuff here in Joomla
$symfonyStyle->success('Successful import!');
return 0;
}
/**
* Configure the command.
*
* @return void
*
* @since 4.0.0
*/
protected function configure(): void {
$this->setDescription('This command prints hello world to whoever calls it');
$this->setHelp(
<<<EOF
The <info>%command.name%</info> command prints hello world to whoever calls it
<info>php %command.full_name%</info>
EOF
);
}
}
plg_dtnnews_cli > src > Extension > DtnnewsConsolePlugin.php
<?php
/**
* @package dtnnews_cli
* @copyright Copyright (c) 2007 - Now Imagine Creative. All rights reserved.
* @license GNU General Public License version 3, or later
*/
namespace Ploszmarketing\Plugin\Console\Dtnnews\Extension;
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\SubscriberInterface;
use Joomla\Application\ApplicationEvents;
use Joomla\CMS\Factory;
use Ploszmarketing\Plugin\Console\Dtnnews\CliCommand\RunDtnnewsCommand;
class DtnnewsConsolePlugin extends CMSPlugin implements SubscriberInterface {
public static function getSubscribedEvents(): array {
return [
\Joomla\Application\ApplicationEvents::BEFORE_EXECUTE => 'registerCommands',
];
}
public function registerCommands(): void {
$app = Factory::getApplication();
$app->addCommand(new RunDtnnewsCommand());
}
}
plg_dtnnews_cli > dtnnews_cli.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
@package dtnnews_cli
@copyright Copyright (c) 2007 - Now Imagine Creative. All rights reserved.
@license GNU General Public License version 3, or later
-->
<extension type="plugin" group="console" method="upgrade">
<name>DTN News Import</name>
<version>1.0.0</version>
<creationDate>2023-09-07</creationDate>
<author>Michael Hermary</author>
<authorEmail>hidden</authorEmail>
<authorUrl>hidden</authorUrl>
<copyright>Copyright (c) 2007 - Now Imagine Creative</copyright>
<license>GNU General Public License version 3, or later</license>
<description>This console plugin uses the CLi and the DTN News API to import items into the Article Manager</description>
<namespace path="src">Ploszmarketing\Plugin\Console\Dtnnews</namespace>
<files>
<folder plugin="dtnnews_cli">services</folder>
<folder>src</folder>
<filename>.htaccess</filename>
</files>
</extension>
PHP 8.2 for the CLi.