Skip to main content

Magento 2 - Shell script or Commands


In magento 2, the shell folder is taken off and instead commands has been introduced. Now, for any shell script, we need to use the commands via php bin/magento commandname. You can find the sample code in the git repo.

Add the files as per the blog post. First of all we need to add a dependency injection, which direct the command file to the desired location.

The di.xml file inside the etc folder will have the info regarding the command class.
<?xml version="1.0" encoding="UTF-8"?>
<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="mySampleCommand" xsi:type="object">VendorName\SampleModule\Console\Command\SampleCommand</item>
            </argument>
        </arguments>
    </type>
</config>


The class VendorName\SampleModule\Console\Command\SampleCommand, will have the code to execute the command.
<?php

namespace VendorName\SampleModule\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class SampleCommand extends Command {

    //params
    protected $_limit;
    protected $_mylimit;

    protected function configure() {
        $this->setName('mysample:update')
                ->setDescription('Update the data using command.')
                ->setDefinition($this->getInputList());
        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $this->addArguments($input);
        $output->writeln("Limit = {$this->_limit}");
        $output->writeln("Option Limit = {$this->_mylimit}");

        try {
            //code to execute
            $output->writeln('Updated the command.');
        } catch (LocalizedException $e) {
            $output->writeln($e->getMessage());
        } catch (\Exception $e) {
            $output->writeln('Not able to update');
            $output->writeln($e->getMessage());
        }
    }

    public function addArguments($input) {
        $this->_limit = intval($input->getArgument("limit"));
        $this->_mylimit = intval($input->getOption("mylimit"));
    }

    public function getInputList() {
        $inputList = [];
        $inputList[] = new InputArgument('limit', InputArgument::OPTIONAL, 'Collection Limit as Argument', 100);
        $inputList[] = new InputOption('mylimit', null, InputOption::VALUE_OPTIONAL, 'Collection Limit as Option', 100);
        return $inputList;
    }

}


The configure function adds the command name and description. Also, we can enter the input list, which can be entered as parameters. The below command is used to get the info for the command, where "-h" is short form of help. Remove -h and excecute the command.
php bin/magento mysample:update -h


Comments

Popular posts from this blog

Magento 2 - Create Attributes in Setup

Sometimes in our module we require to create product or category attributes automatically on module install. Also, we might require to add extra fields to quote, sales order, invoice, creditmemo tables. We will go through how to do that in magento 2. You can find the sample code in the git repo .