الطريقة الصحيحة والمدعومة لإضافة أوامر CLI إلى Magento 2

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

سؤال

هل هناك طريقة صحيحة ومدعومة رسميًا لإضافة أوامر CLI الخاصة بك إلى وحدة Magento 2؟مما جمعته من خياراتك هي

  1. أضف فئة الأوامر الخاصة بك إلى commands حجة Magento\Framework\Console\CommandList عبر أ di.xml ملف

  2. سجل أمرك عبر \Magento\Framework\Console\CommandLocator::register في registration.php ملف أو أ cli_commands.php ملف

لا ينعم أي من هذه الخيارات بـ @api.ليس من الواضح، كمطوري الإضافات، كيف ينبغي لنا إضافة نصوص برمجية لسطر الأوامر بحيث تلتزم بإصدار إلى إصدار.

هل يعرف أحد ما إذا كانت هناك سياسة Magento رسمية بشأن الطريقة الصحيحة ™ للقيام بذلك؟

هل كانت مفيدة؟

المحلول

cli_commands.php يجب استخدامه في حالة إضافة الأمر في حزمة غير معيارية.لذا، إذا كان الأمر موجودًا في الوحدة وكان من المقبول (المتوقع) أن يكون متاحًا فقط عند تمكين الوحدة، di.xml يجب أن تستخدم.إذا كنت لا ترغب في إضافة وحدة نمطية وترغب في الحصول على حزمة Composer عشوائية فقط، فيمكنك استخدام 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.

إذا كنت بحاجة إلى السماح لهم متوافق فقط استخدام واجهة 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 متوفرة فقط في مثيل Magento مثبت فقط وأيضا لوحدات Magento فقط (نظرا لتعريفها في di.xml): https://github.com/magento/magento2/blob / 6352F8FBCA2CB21DE88DB0CF7F4555BFC60451C/LIB/Internal/magento/framework/console/cli.php#l124

magento \ framework \ app \ deeplymentconfigig :: IsVailable () في الطريقة المذكورة أعلاه يتحقق من تاريخ التثبيت في التكوين للتحقق من Magento2: https://github.com/magento/magento2/blob/6352f8fbca2cbf21de88db0cf7f4555bfc60451c/lib/internal/Magento/ Framework / App / DeploymentConfig.php # L83 ).

الأوامر المحددة في Magento \ Framework \ Console \ CommandLocator من ناحية أخرى متاحة دائما وحتى يمكن تعريفها بواسطة وحدات غير Magento من خلال Magento Modules من خلال OrgentLocator STATCALCATION :: Register Method في ملف Autoloaded بواسطة COMPOSER (على سبيل المثال CLI_COMMANMANDS. PHP)

https:// github.com/magento/magento2/blob/6352f8fbca2cb21de88db0cf7f4555bfcc60451c/lib/internal/magento/framework/console/cli.php#l130

href="https://github.com/magento/magento2/blob/6352f8fbca2cbf21de88db0cf7f4555bfcc60451c/lib/internal/magento/frameworks/console/cli.php#l146" rel="nofollowol"> https:// github.com/magento/magento2/blob/6352f8fbca2cbf21de88db0cf7f4555bfcc60451c/lib/internal/magento/framework/console/cli.php#l146

لذلك أعتقد أن كلتا الطريقتين مطلوبة ولها حق في الوجود

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top