Skip to content

Add custom commands

cmuench edited this page Nov 30, 2014 · 2 revisions

You can assign your own symfony2 commands to n98-magerun.

  1. Add the folder which contains your custom commands to the autoloader.
  2. Register Commands

The config file must be placed in your home directory with the name ~/.n98-magerun.yaml.

It's also possible (since version 1.36.0) to place a config in your magento installation to add custom project based command. Create config and save it as app/etc/n98-magerun.yaml. The project config can use the variable %root% to get magento root folder dynamically.

Since version 1.72.0 it's possible to stucture commands in modules. See Modules.

Config

autoloaders:
  # Namespace => path to your libs
  MyCommandNamespace: /home/myuser/lib

  # or in project based config i.e.:
  # MyCommandNamespace: %root%/lib

commands:
  customCommands:
    - MyCommandNamespace\FooCommand

  # optional command config
  MyCommandNamespace\FooCommand:
    bar: zoz

Example Command

<?php

namespace MyCommandNamespace;

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('foo')
          ->setDescription('Foo test command')
      ;
    }

   /**
    * @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()) {
         var_dump($this->getCommandConfig());
         $output->writeln(__DIR__ . ' -> ' . __CLASS__);
      }
    }

}

Run an existing command in a command

<?php

$input = new StringInput('cache:flush');

// ensure that n98-magerun doesn't stop after first command
$this->getApplication()->setAutoExit(false);

// without output
$this->getApplication()->run($input, new NullOutput());

// with output
$this->getApplication()->run($input, $output);

// reactivate auto-exit
$this->getApplication()->setAutoExit(true);

Get Magento Root-Folder

<?php

$rootFolder = $this->getApplication()->getMagentoRootFolder();

Enterprise Edition Only Commands

Add isEnable method to your command.

<?php

/**
 * @return bool
 */
public function isEnabled()
{
    return $this->getApplication()->isMagentoEnterprise();
}