Magento 2にCLIコマンドを追加するための正しい、サポートされている方法

magento.stackexchange https://magento.stackexchange.com/questions/112022

質問

Magento 2モジュールにCLIコマンドを追加するには、正しい、正式にサポートされている方法がありますか?私があなたのオプションを集めたものから

  1. commandsファイル

  2. を介してMagento\Framework\Console\CommandListdi.xml引数にコマンドクラスを追加します。

  3. \Magento\Framework\Console\CommandLocator::registerファイルまたはregistration.phpファイルのcli_commands.phpを介してコマンドを登録する

  4. これらのオプションのどれも@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で定義されなければならないため)のみでも使用できます。 https://github.com/magento/magento2/blob / 6352F8FCA2CBF21DE88DB0CF7F4555BFC60451C / LIB / Internal/Magento / Framework/Console/Cli.Php#L124

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)

https:// github.com/magento/magento2/blob/6352F8FBCA2CBF21DE88DB0CF7F4555BFC60451C / LIB / Internal/Magento/Framework/Console/Cli.Php#L130

https:// github.com/magento/magento2/blob/6352F8FBCA2CBF21DE88DB0CF7F4555BFC60451C/LIB / Internal/Magento/Framework/Console/Cli.Php#L146

だから私は両方の方法が必要であり、存在する権利があると思います

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top