Tip: Create a Console Helper

Since version 1.77.0 we provide an easy registration of console helpers.
Console Helpers are way to share functionality with other commands.

I will show you how easy it is to create such a helper.
Out example helper will return the current system time.

Create a new module

Create a new folder in your home directory with the name “.n98-magerun/modules/example-module”.

mkdir -p ~/.n98-magerun/modules/example-module


Now we need a module config file.

cd ~/.n98-magerun/modules/example-module

Create a file “n98-magerun.yaml” with the following content inside the module folder.
The config defines a new namespace “Example” in the autoloader of n98-magerun, the new helper “datetime”
and an example command “ExampleCommand”.
We will create the new files in this tutorial.

autoloaders:
  Example: %module%/src

helpers:
  datetime: Example\DateTimeHelper

commands:
  customCommands:
    - Example\ExampleCommand

Create the helper

Create a new folder src/Example inside the module folder.

mkdir -p src/Example

Inside the src folder we will place our helper with the filename “DateTimeHelper.php”.

<?php

namespace Example;

use Symfony\Component\Console\Helper\Helper as AbstractHelper;

class DateTimeHelper extends AbstractHelper
{
    /** 
    * Returns the canonical name of this helper.
    *
    * @return string The canonical name
    *
    * @api
    */
    public function getName()
    {   
        return 'datetime';
    }   

    /** 
    * Returns the current time
    * 
    * @return string
    */
    public function getCurrentTime()
    {   
        return date('H:i:s');
    }   
}

Create an example command to test new helper

Now we create a command in file ExampleCommand.php with this content:

<?php

namespace Example;

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

class ExampleCommand extends AbstractMagentoCommand
{
    protected function configure()
    {   
        $this
        ->setName('example')
        ->setDescription('Example 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)
    {   
        $time = $this->getHelper('datetime')->getCurrentTime();
        $output->writeln($time);
    }   
}

If you have created all the files you should have this file structure:

.n98-magerun
└── modules
    └── example-module
        ├── n98-magerun.yaml
        └── src
            └── Example
                ├── DateTimeHelper.php
                └── ExampleCommand.php

You should now be able to run the new command with this command line call:

n98-magerun.phar example

The command should show the current system time.
I hope you like the console helper.
n98-magerun is already bundles with some build-in helpers which are described in our
wiki. https://github.com/netz98/n98-magerun/wiki/Helpers

Happy Coding!

Download Example Module: example-module.tar.gz