문제

가 올바른 공식적으로 지원되는 방법을 사용하여,귀하의 추가 CLI 명령을 Magento2 모듈?에서 내가 모여 귀하의 옵션

  1. 추가 명령 클래스 commands 의 인 Magento\Framework\Console\CommandList 를 통해 di.xml 파일

  2. 등록 명령을 통해 \Magento\Framework\Console\CommandLocator::registerregistration.php 파일이나 cli_commands.php 파일

이 옵션은 축복과 @api.그것은 명확하지 않으로 확장을 개발자들은 우리가 어떻게 추가 명령 라인에 스크립트는 그들이 주위에 충실 버전 버전입니다.

사람이 알고 있는 경우 공식 젠토의 정책에 맞™방법으로 하면 이렇게 할 수 있습니까?

도움이 되었습니까?

해결책

cli_commands.php 에 사용되어야 하는 경우의 명령을 추가 비 모듈식 패키지입니다.그렇다면 명령이 모듈에서의 확인(예)그것을 사용할 수 있는 경우에만 이 모듈이 활성화, di.xml 을 사용해야 합니다.당신이 원하지 않는 경우 추가 모듈을 임의적인 작곡가 패키지를 사용할 수 있습니다 cli_commands.php 등록 명령이 있다.물론 그것이 있어야 그 다음에 정말로부터 독립 Magento.또한,이 방법을 사용할 수 있습니다 명령을 등록(s)필요한 경우에도 모듈을 사용할 수 없(하는지 확인에 의존하지 않는 모든 모듈의 논리로만 작동되는 경우 그것의 사용)입니다.

다른 팁

올바른 방법입니다:

을 만들의 모듈에 대해 수행하는 것과 같은 방법으로 어떤 종류의 모듈

다음 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>

Create your 령 등급:

<?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.');
    }
}

는 경우가 바로 그것을 가지고,명령에서 정의 CommandList 통해 DI 에서만 사용할 수 있습니다 설치된 Magento 인스턴스도에 대해서만 젠토모듈(될 수 있기 때문에서 정의 di.xml): https://github.com/magento/magento2/blob/6352f8fbca2cbf21de88db0cf7f4555bfc60451c/lib/internal/Magento/Framework/Console/Cli.php#L124

젠토\Framework\App\DeploymentConfig::사용할 수 있()상기에서 방법을 검사한 위치에서 날짜 구성을 확인하는 설치 Magento2: https://github.com/magento/magento2/blob/6352f8fbca2cbf21de88db0cf7f4555bfc60451c/lib/internal/Magento/Framework/App/DeploymentConfig.php#L83).

명령에서 정의 젠토\Framework\콘솔\CommandLocator 다른 한편으로는 항상 사용할 수 있도에 의해 정의 될 수 있는 비 젠토을 통해 모듈을 정적 CommandLocator::등록 방법에 파일이 자동으로 작곡가(예를 들어 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