Skip to content
Christian Münch edited this page Sep 8, 2021 · 4 revisions

Modules are a simple ways to add new commands to the system. With modules it's also easy to share commands within your developer team.

Module Structure

A module is a folder with at least a config file with the name n98-magerun2.yaml. Inside your config you can define a command by using the same structure as defining a single custom command. See Add-custom-commands.

Example n98-magerun2.yaml:

autoloaders_psr4:
  MyNamespace\: %module%/src

commands:
  customCommands:
    - MyNamespace\FooCommand
    - MyNamespace\BarCommand

%module% will be replaced with your current module folder path. It's not possible to place modules inside of modules.

.
└── test-module
    ├── n98-magerun2.yaml
    └── src
          ├── BarCommand.php
          └── FooCommand.php

Example Command:

<?php

namespace MyNamespace;

use N98\Magento\Command\AbstractMagentoCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FooCommand extends AbstractMagentoCommand
{
    protected function configure()
    {
      $this
          ->setName('mynamespace:foo')
          ->setDescription('Test command registered in a module')
      ;
    }

   /**
    * @param \Symfony\Component\Console\Input\InputInterface $input
    * @param \Symfony\Component\Console\Output\OutputInterface $output
    * @return int|void
    */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->detectMagento($output);
        if ($this->initMagento()) {
           // .. do something 
        }
    }
}

Where can modules be placed?

There are currently three possible base folders where you can place your modules.

  • /usr/local/share/n98-magerun2/modules
  • ~/.n98-magerun2/modules
  • MAGENTO_ROOT/lib/n98-magerun2/modules
  • %windir%\n98-magerun2\modules (only Microsoft Windows)
  • %userprofile%\n98-magerun2\modules (only Microsoft Windows)

These folders do not always exist and may need to be created. It is also wise to update n98-magerun2.phar self-update before adding modules.

Module Best Practices

If your Command needs a minimal n98-magerun2 version...

Add this method to your command:

i.e. 2.0.9

<?php

// ...

/**
 * @return bool
 */
public function isEnabled()
{
    return version_compare($this->getApplication()->getVersion(), '2.0.9', '>=');
}