Magento 2にCLIコマンドを追加するための正しい、サポートされている方法
質問
Magento 2モジュールにCLIコマンドを追加するには、正しい、正式にサポートされている方法がありますか?私があなたのオプションを集めたものから
-
commands
ファイル を介して -
\Magento\Framework\Console\CommandLocator::register
ファイルまたはregistration.php
ファイルのcli_commands.php
を介してコマンドを登録する
Magento\Framework\Console\CommandList
のdi.xml
引数にコマンドクラスを追加します。
これらのオプションのどれも@api
に恵まれていません。拡張開発者として、それらがバージョンをバージョンに固執するようなコマンドラインスクリプトを追加する必要があります。
これを行うために正しい™方法で公式マゼントポリシーがあるかどうかを知っていますか?
解決
cli_commands.php
コマンドが非モジュール式パッケージに追加されている場合に使用する必要があります。
そのため、コマンドがモジュールに入っていて、モジュールが有効になっている場合にのみ使用可能であることがOK(予想される)である場合は、di.xml
を使用する必要があります。モジュールを追加して任意の作曲家パッケージを持ちたい場合は、cli_commands.php
を使用してコマンドを登録できます。もちろん、それはMagentoから本当に独立しているはずです。または、今のところ、この方法を使用して、モジュールが無効になっていても必要なコマンドを登録することができます(有効な場合にのみ機能するモジュールのロジックに依存しないことを確認します)。
他のヒント
正しい方法は次のとおりです。
どんなモジュールのどんなモジュールのためにそうするようにあなたのモジュールを作成します
registration.php
ファイルを作成するただ
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'My_Module',
__DIR__
);
.
とmodule.xml
ファイルを作成します。
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="My_Module" setup_version="0.1.0">
</module>
</config>
.
di.xml
にエントリを追加する:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="my_command" xsi:type="object">My\Module\Command\Mycommand</item>
</argument>
</arguments>
</type>
</config>
.
コマンドクラスを作成する:
<?php
namespace My\Module\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Mycommand extends Command
{
protected function configure()
{
$this->setName('my:command');
$this->setDescription('Run some task');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Hello world!');
}
}
.
あなたのタスクを実行するためだけにタイプ:
php bin/magento my:command
.
互換性について:
@APIはコマンドには不要です、それはサービス契約 afaikに使用されます。
互換性がある場合は、ロジックを内部に置く代わりに、スクリプト内に Interface API を使用するだけです。
例えば:
<?php
use My\Module\Api\TaskInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends Command
{
protected $taskInterface;
public function __construct(
TaskInterface $taskInterface
) {
$this->taskInterface= $taskInterface;
parent::__construct();
}
protected function configure()
{
$this->setName('my:command');
$this->setDescription('Run some task');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->taskInterface->runTask();
$output->writeln('Done.');
}
}
. それを正しく持っていれば、DI over over DIで定義されているコマンドは、インストールされているMagentoインスタンスでのみ利用可能であり、Magentoモジュール(DI.XMLで定義されなければならないため)のみでも使用できます。
Magento \ Framework \ App \ DeploymentConfig :: Isavailable()上記の方法では、インストールされているMagento2をチェックするための設定のインストール日を確認します。 https://github.com/magento/magento2/blob/6352f8fbca2cbf21de88db0cf7f4555bfc60451c/lib/internal/Magento/フレームワーク/ app / deploymentConfig.php#l83 )
一方、Magento \ Framework \ Console \ ConfidLococatorで定義されているコマンドは常に利用可能であり、Composerによって自動ロードされたファイル内のstatic commandlocator :: registerメソッドを介して非Magentoモジュールによって定義することもできます(たとえば、CLI_COMMANDS。 PHP)
だから私は両方の方法が必要であり、存在する権利があると思います