跳至主要内容
版本:5.1

控制台插件 - Hello World

简介

控制台应用程序是在 Joomla 4 中引入的,现在是 Joomla 编写命令行应用程序的战略方式。您可能也听说过它们被称为 CLI 应用程序。它们是利用 Joomla 框架的 PHP 应用程序,并在托管 Joomla 实例的命令行中运行,因此您可以运行这些应用程序

  • 从服务器上的终端会话
  • 从服务器上的 cron 作业
  • 从远程终端会话 - 例如使用 PuTTY

Joomla 附带了一些已存在的这些应用程序,因此在终端会话中,您可以导航到您的 Joomla 实例并执行,例如

php cli/joomla.php user:list

这将列出 Joomla 实例上的用户。(您可能需要将 php 变量设置为指向相应的 php.exe 文件。)

您可以通过以下方式显示可用的控制台应用程序集

php cli/joomla.php

您可以编写自己的控制台应用程序,这将扩展可用的应用程序集。为此,您编写一个控制台插件,本节介绍如何编写“hello world”应用程序,以便您可以执行

php cli/joomla.php hello:world

该应用程序将输出“Hello World”。

您可能会发现观看此视频 向 CLI 更新添加命令 很有用,但请注意,此视频(针对 Joomla 核心开发人员)介绍了通过更改库文件来创建命令,而您必须使用插件来代替。

您需要做的事情

您需要编写 2 个类

  • 一个类(下面的 ConsolePlugin)处理与 Joomla 插件机制相关的方面
  • 一个类(下面的 Command)包含命令的代码

此序列图显示了这两个命令如何与 Joomla 代码交互

在此示例中,这两个类是

  • HelloworldConsolePlugin - 处理插件方面,以及
  • RunHelloCommand - 输出“Hello World”,并包含有关命令的信息。

控制台插件代码

按照上面的序列图,HelloworldConsolePlugin 的代码是

public static function getSubscribedEvents(): array
{
return [
\Joomla\Application\ApplicationEvents::BEFORE_EXECUTE => 'registerCommands',
];
}

public function registerCommands(): void
{
$app = Factory::getApplication();
$app->addCommand(new RunHelloCommand());
}

Command configure() 调用

protected function configure(): void
{
$this->setDescription('This command prints hello world');
$this->setHelp(
<<<EOF
The <info>%command.name%</info> command prints hello world. To use enter:
<info>php %command.full_name%</info>
EOF
);
}

RunHelloCommand 上调用 configure() 的目的是获取有关命令的信息,Joomla 然后可以在终端上显示这些信息。在此方法中,您应该调用以下内容

  • setDescription - 用于定义命令功能的简短描述。当用户使用以下命令列出可用的命令时,会显示此内容
php cli/joomla.php
  • setHelp - 用于定义命令的帮助文本。当用户输入以下内容时,会显示此内容
php cli/joomla.php hello:world -h

如您所见,您可以将 %command.name%%command.full_name% 变量嵌入到此文本中。

此外,命令的名称取自您需要设置的变量

protected static $defaultName = 'hello:world';

这也将在用户输入以下内容时显示

php cli/joomla.php

Command doExecute() 调用

最后,在 RunHelloCommand 上调用 doExecute() 以执行命令,代码输出“Hello World”

protected function doExecute(InputInterface $input, OutputInterface $output): int
{
$symfonyStyle = new SymfonyStyle($input, $output);
$symfonyStyle->text('Hello World');
return 0;
}

为了处理 I/O,Joomla 已合并了 Symfony Style 包,它提供了几种方法,您可以轻松地调用这些方法来在终端会话上提供输入和输出功能。

该函数返回一个退出代码,在成功完成时应该为整数零。此代码是 Joomla php 应用程序将退出的代码,因此如果您在批处理过程中运行命令,则可以捕获它。

插件代码

本节包含控制台插件的完整源代码。您可以通过复制下面的代码手动编写插件,也可以从 下载控制台插件 Helloworld 下载 zip 文件。如果您是手动编写它,则将以下文件包含在文件夹中,例如 plg_helloworld_cli

此处 所述,在开发插件时,您需要确保在源代码文件中有一系列内容保持一致。该示例还包括如何使用语言文件使您的插件与语言无关。为简单起见,此 helloworld 示例仅支持英语。

清单文件

plg_helloworld_cli/helloworld.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" group="console" method="upgrade">
<name>Helloworld Console Application</name>
<version>1.0.0</version>
<creationDate>today</creationDate>
<author>Me</author>
<description>A basic Hello World console application</description>
<namespace path="src">My\Plugin\Console\Helloworld</namespace>
<files>
<folder plugin="helloworld">services</folder>
<folder>src</folder>
</files>
</extension>

服务提供商文件

services/provider.php 文件是相当标准的样板代码;您只需要正确编码与您的插件相关的 3 行代码,以及注入应用程序,因为它在控制台插件代码中被访问。

plg_helloworld_cli/services/provider.php
<?php
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 Joomla\CMS\Factory;
use My\Plugin\Console\Helloworld\Extension\HelloworldConsolePlugin;

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 HelloworldConsolePlugin(
$dispatcher,
(array) PluginHelper::getPlugin('console', 'helloworld')
);
$plugin->setApplication(Factory::getApplication());

return $plugin;
}
);
}
};

控制台插件文件

下面的文件处理与 Joomla 插件框架的交互

plg_helloworld_cli/src/Extension/HelloworldConsolePlugin.php
<?php
namespace My\Plugin\Console\Helloworld\Extension;

\defined('_JEXEC') or die;

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\SubscriberInterface;
use Joomla\Application\ApplicationEvents;
use My\Plugin\Console\Helloworld\CliCommand\RunHelloCommand;

class HelloworldConsolePlugin extends CMSPlugin implements SubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
\Joomla\Application\ApplicationEvents::BEFORE_EXECUTE => 'registerCommands',
];
}

public function registerCommands(): void
{
$app = $this->getApplication();
$app->addCommand(new RunHelloCommand());
}
}

命令文件

下面的文件处理执行 hello:world命令。

plg_helloworld_cli/src/CliCommand/RunHelloworldCommand.php
<?php
namespace My\Plugin\Console\Helloworld\CliCommand;

defined('_JEXEC') or die;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Joomla\Console\Command\AbstractCommand;

class RunHelloCommand extends AbstractCommand
{
/**
* The default command name
*
* @var string
* @since 4.0.0
*/
protected static $defaultName = 'hello:world';

/**
* Configure the command.
*
* @return void
*
* @since 4.0.0
*/
protected function configure(): void
{
$this->setDescription('This command prints hello world');
$this->setHelp(
<<<EOF
The <info>%command.name%</info> command prints hello world. To use enter:
<info>php %command.full_name%</info>
EOF
);
}

/**
* 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->text("Hello World");

return 0;
}
}

安装

从文件夹中生成一个 zip 文件,并以通常的方式安装插件。请记住启用插件!

然后在终端会话中,导航到 Joomla 实例的顶层并输入

php cli/joomla.php

(您可能需要将 php 变量设置为指向相应的 php.exe 文件。)这将按字母顺序显示命令列表,其中现在应该包含 hello:world 命令。

要运行 hello:world 命令,您应该输入

php cli/joomla.php hello:world

这应该在终端上打印“Hello World”。

要显示帮助文本

php cli/joomla.php hello:world -h