While using Magento 2, you are probably familiar with tons commands and executed multiple times. By default, Magento comes with preloaded SSH features that allows you to securely log into remote server or system and execute commands. Basically, SSH was designed to provide secure communications between two untrusted networks. But have you ever wondered where how these commands were created and how to create your own command?
In today’s blog, I will show you how to create your own SSH command in Magento 2 with simple steps. We need to create two files to override default Magento files using a custom Magento 2 extension. Here, I have demonstrated simple SSH command to see extension is enabled or not?!?!
Step 1: First, we need to create “di.xml” file inside extension the below directory
app\code\Vendor\Extension\etc
<?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="Symfony\Component\Console\Application">
<arguments>
<argument name="commands" xsi:type="array">
<item name="extension_enable" xsi:type="object">Vendor\Extension\Console\Command\Enable</item>
</argument>
</arguments>
</type>
</config>
In our custom class, we need to override two default methods among that one for execution and second for configuration. That’s why we have created another file ‘enable.php‘ .
Step 2: after that, you need to create “Enable.php” file inside extension the below directory
app\code\Vendor\Extension\Console\Command
<?php
namespace Vendor\Extension\Console\Command;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Enable extends Command
{
protected $scopeConfig;
protected $orderCollectionFactory;
public function __construct(
CollectionFactory $orderCollectionFactory,
ScopeConfigInterface $scopeConfig
) {
$this->orderCollectionFactory = $orderCollectionFactory;
$this->scopeConfig = $scopeConfig;
parent::__construct();
}
protected function configure()
{
$this->setName('extension:enable');
$this->setDescription('Check if extension is enabled via config');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Extensions checking Start...');
$enabled = $this->scopeConfig->getValue('section/group/field', ScopeInterface::SCOPE_STORE);
if ($enabled) {
$output->writeln('Extension is Enabled');
} else {
$output->writeln('Extension is Disabled');
}
$output->writeln('Extensions checking finished.');
return Command::SUCCESS;
}
}
Now run your command:
php bin/magento extension:enable
Don’t forget to tell us, how you have used this code to create your own command. If you are looking for more help regarding this code kindly comment down below. Happy SSH!
